-
Notifications
You must be signed in to change notification settings - Fork 11
/
p5.multiplayer.js
150 lines (120 loc) · 3.2 KB
/
p5.multiplayer.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
////////////
// COMMON
// Initialize Network related variables
let socket;
let roomId = null;
let id = null;
// Process URL
// Used to process the room ID. In order to specify a room ID,
// include ?=uniqueName, where uniqueName is replaced with the
// desired unique room ID.
function _processUrl() {
const parameters = location.search.substring(1).split("&");
const temp = parameters[0].split("=");
roomId = unescape(temp[1]);
console.log("id: " + roomId);
}
// Send data from client to host via server
function sendData(datatype, data) {
data.type = datatype;
data.roomId = roomId;
socket.emit('sendData', data);
}
// Displays a message while attempting connection
function _displayWaiting() {
push();
fill(200);
textAlign(CENTER, CENTER);
textSize(20);
text("Attempting connection...", width/2, height/2-10);
pop();
}
////////////
// HOST
// Initialize Network related variables
let hostConnected = false;
function setupHost() {
_processUrl();
let addr = serverIp;
if (local) { addr = serverIp + ':' + serverPort; }
socket = io.connect(addr);
socket.emit('join', {name: 'host', roomId: roomId});
socket.on('id', function(data) {
id = data;
console.log("id: " + id);
});
socket.on('hostConnect', onHostConnect);
socket.on('clientConnect', onClientConnect);
socket.on('clientDisconnect', onClientDisconnect);
socket.on('receiveData', onReceiveData);
}
function isHostConnected(display=false) {
if (!hostConnected) {
if (display) { _displayWaiting(); }
return false;
}
return true;
}
function onHostConnect (data) {
console.log("Host connected to server.");
hostConnected = true;
if (roomId === null || roomId === 'undefined') {
roomId = data.roomId;
}
}
// Displays server address in lower left of screen
function displayAddress() {
push();
fill(255);
textSize(50);
text(serverIp+"/?="+roomId, 10, height-50);
pop();
}
////////////
// CLIENT
// Initialize Network related variables
let waiting = true;
let connected = false;
function setupClient() {
_processUrl();
// Socket.io - open a connection to the web server on specified port
let addr = serverIp;
if (local) { addr = serverIp + ':' + serverPort; }
socket = io.connect(addr);
socket.emit('join', {name: 'client', roomId: roomId});
socket.on('id', function(data) {
id = data;
console.log("id: " + id);
});
socket.on('found', function(data) {
connected = data.status;
waiting = false;
console.log("connected: " + connected);
})
socket.emit('clientConnect', {
roomId: roomId
});
socket.on('receiveData', onReceiveData);
}
function isClientConnected(display=false) {
if (waiting) {
if (display) { _displayWaiting(); }
return false;
}
else if (!connected) {
if (display) { _displayInstructions(); }
return false;
}
return true;
}
// Displays a message instructing player to look at host screen
// for correct link.
function _displayInstructions() {
push();
fill(200);
textAlign(CENTER, CENTER);
textSize(20);
text("Please enter the link at the", width/2, height/2-10);
text("bottom of the host screen.", width/2, height/2+10);
pop();
}