-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
49 lines (42 loc) · 1.39 KB
/
socket.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
/* global Avatar */
/* global Input */
/* global Player */
class Socket {
static init(username) {
Socket.ws = new WebSocket(Socket.host);
Socket.ws.onopen = () => {
console.log("Connected");
var data = username.split(",");
Avatar.username = data[0];
//Authenticate with server by sending username
Socket.ws.send(`{"command":"username","data":"${username}"}`);
};
Socket.ws.onmessage = (msg) => {
var json = JSON.parse(msg.data);
switch(json.command) {
case "auth":
if (json.data === "true") {
console.log("Authenticated by server");
Avatar.init();
Input.init();
Avatar.send();
}
break;
case "playerGone":
Player.remove(json.data);
break;
case "playerMoved":
Player.move(json.data);
break;
}
};
Socket.ws.onclose = () => {
alert("Connection closed");
};
Socket.ws.onerror = (e) => {
console.log("Error");
};
}
};
Socket.host = "ws://localhost:5000"; // LOCAL
//Socket.host = "wss://www.cspears.site:5000"; // SERVER