forked from tkrclient/tkrclient.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
71 lines (60 loc) · 1.5 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var Client = function(server, room, onMessage)
{
this.send = function(type, msg)
{
msg.type = type;
socket.send(JSON.stringify(msg));
};
this.chat =
{
name : function(name)
{
Cookies.set("name", name, { expires : 3650 });
$("#name").val(name);
},
color : function(color)
{
Cookies.set("color", color, { expires : 3650 });
$("#colorpicker").css("color", color);
}
};
let socket = null;
function connect()
{
if(socket !== null)
{
socket.onopen = function(){};
socket.onclose = function(){};
socket.onerror = function(){};
socket.onmessage = function(){};
socket = null;
}
socket = new WebSocket(server + "/?room=" + room);
socket.onopen = function()
{
console.log("WebSocket Connected");
};
socket.onclose = function(event)
{
console.log("WebSocket Closed");
setTimeout(connect, 3000);
};
socket.onerror = function(error)
{
console.log("WebSocket Error");
setTimeout(connect, 3000);
};
socket.onmessage = function(event)
{
try
{
onMessage(JSON.parse(event.data));
}
catch(e)
{
console.error(e.stack);
}
};
}
connect();
};