-
Notifications
You must be signed in to change notification settings - Fork 1
/
socketTest.pde
127 lines (97 loc) · 2.46 KB
/
socketTest.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//Rob Seward 2011
//robseward.com
import processing.net.*;
import processing.opengl.*;
int port = 6780;
Server myServer;
HashMap ipToId;
int maxUsers = 10;
int activeUsers = 0;
color[] colors = {#FF0000, #00FF00, #0000FF, #66FFFF, #66FFCC, #66FF99, #66FF66, #66FF33, #66FF00, #6699FF};
Point[] userPositions = new Point[maxUsers];
void setup()
{
//size(320, 480);
size(1920, 1280,OPENGL);
myServer = new Server(this, port);
ipToId = new HashMap();
}
void draw()
{
background(50,50,50);
while(checkSocket()){
;
}
drawUsers();
}
boolean checkSocket(){
Client thisClient = myServer.available();
if (thisClient !=null) {
String whatClientSaid = thisClient.readString();
if (whatClientSaid != null) {
if(!ipToId.containsKey(thisClient.ip())){
ipToId.put(thisClient.ip(), new Integer(activeUsers));
activeUsers++;
}
String clientId = ipToId.get(thisClient.ip()) + "";
parseClientData(whatClientSaid, int(clientId));
}
return true;
}
return false;
}
void parseClientData(String data, int clientId){
String[] coords = split(data, ":");
for(int i=0; i < coords.length - 1; i++){
String[] coord = split(coords[i], ",");
if(coord.length > 2){
int y = height - int(coord[1]) * 4;
int x = int(coord[2]) * 4;
if(coord[0].equals("b")){
touchBegan(x, y, clientId);
} else if (coord[0].equals("m")){
touchMoved(x, y, clientId);
} else if (coord[0].equals("e")){
touchEnded(x, y, clientId);
}
}
}
}
void drawUsers(){
for(int i=0; i < activeUsers; i++){
fill(colors[i]);
stroke(colors[i]);
Point p = userPositions[i];
ellipse(p.x, p.y, 40, 40);
}
}
//////////
void mousePressed() {
touchBegan(mouseX, mouseY, 0);
}
void mouseDragged() {
touchMoved(mouseX, mouseY, 0);
}
void mouseReleased(){
touchEnded(mouseX, mouseY, 0);
}
void touchBegan(int x, int y, int id){
println("TOUCH BEGAN " + x + ", " + y);
if(id < maxUsers) userPositions[id] = new Point(x, y);
}
void touchMoved(int x, int y, int id){
println(" TM: " + x + ", " + y);
if(id < maxUsers) userPositions[id] = new Point(x, y);
}
void touchEnded(int x, int y, int id){
if(id < maxUsers) userPositions[id] = new Point(x, y);
}
///////////
class Point {
public int x;
public int y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
}