-
Notifications
You must be signed in to change notification settings - Fork 3
/
ws.js
52 lines (52 loc) · 1.58 KB
/
ws.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
var frWs = {
conn: null,
clientId: null,
init: function() {
frWs.conn = new WebSocket(rescueConfig.WSUrl);
frWs.conn.onmessage = frWs.onMessage;
frWs.conn.onerror = frWs.onError;
frWs.conn.onclose = frWs.onClose;
frWs.conn.onopen = frWs.onOpen;
},
onOpen: function(dc) { frWs.subscribe('0xDEADBEEF'); },
onClose: function(dc) {
if(dc.wasClean === false) {
setTimeout(frWs.init, 5000);
}
},
onMessage: function(data) {
var _data = JSON.parse(data.data);
if(_data.meta.event == 'welcome') {
frWs.clientId = _data.meta.id;
}
rescuePlugin.HandleTPA(_data);
},
onError: function(error) {
console.log(error);
},
send: function(action, data, meta) {
if(frWs.conn.readyState != 1) {
if(frWs.conn.readyState == 0) {
} else if (frWs.conn.readyState == 2 || frWs.conn.readyState == 3) {
frWs.init();
}
setTimeout(function() {frWs.send(action, data, meta); }, 1000);
return;
}
frWs.conn.send(JSON.stringify({ "action": action, "applicationId": frWs.clientId, "data": data, "meta": meta }));
},
subscribe: function(stream) {
frWs.conn.send(JSON.stringify({ 'action': 'stream:subscribe', 'applicationId': stream }))
},
searchNickName: function(nickname, meta) {
if(frWs.conn.readyState != 1) {
if(frWs.conn.readyState == 0) {
} else if (frWs.conn.readyState == 2 || frWs.conn.readyState == 3) {
frWs.init();
}
setTimeout(function() {frWs.searchNickName(nickname, meta); }, 1000);
return;
}
frWs.conn.send(JSON.stringify({ "action": 'nicknames:search', "applicationId": frWs.clientId, "nickname": nickname, "meta": meta }));
}
};