Skip to content

Instantly share code, notes, and snippets.

@cheriimoya
Created June 3, 2017 18:17
Show Gist options
  • Save cheriimoya/a618716c5a78e50cc3cfbb0627ba126d to your computer and use it in GitHub Desktop.
Save cheriimoya/a618716c5a78e50cc3cfbb0627ba126d to your computer and use it in GitHub Desktop.
import websockets.*;
WebsocketClient wsc;
WebsocketServer ws;
int x, y;
int role = 2;
void setup() {
size(400, 400);
if (role == 1)
ws= new WebsocketServer(this, 8090, "/game");
if (role == 2)
wsc= new WebsocketClient(this, "ws://localhost:8090/game");
x = 200;
y = 200;
}
void draw() {
background(0);
if (role == 2) wsc.sendMessage("client");
if (role == 1) {
ws.sendMessage(String.valueOf(x)+","+String.valueOf(y));
x = mouseX;
y = mouseY;
}
ellipse(x, y, 10, 10);
}
void webSocketEvent(String msg) {
println(msg);
String[] str = msg.split(",");
x=Integer.parseInt(str[0]);
y=Integer.parseInt(str[1]);
}
void webSocketServerEvent(String msg) {
println(msg);
String[] str = msg.split(",");
x=Integer.parseInt(str[0]);
y=Integer.parseInt(str[1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment