forked from dylanbeattie/KaboomJS
-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.js
50 lines (39 loc) · 981 Bytes
/
client.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
var socket,
levelData;
$(function() {
$("#join").click(function() {
initSocket();
});
});
var initSocket = function() {
socket = new io.Socket("localhost", {port: 5678, transports: ["websocket", "flashsocket"]});
// EVENTS
// connect, connecting, connect_failed, message, close,
// disconnect, reconnect, reconnecting, reconnect_failed
// WebSocket connection successful
socket.on("connect", function() {
console.log("Connected");
var msg = JSON.stringify({type: "join"});
socket.send(msg);
});
// WebSocket connection failed
socket.on("connect_failed", function() {
console.log("Connect failed");
});
// WebSocket disconnection
socket.on("disconnect", function() {
console.log("Disconnected");
});
// WebSocket message received
socket.on("message", function(data) {
var msg = JSON.parse(data);
if (msg.type) {
switch (msg.type) {
case "welcome":
levelData = msg.level;
break;
};
};
});
socket.connect();
};