-
Notifications
You must be signed in to change notification settings - Fork 2
/
socket.js
159 lines (127 loc) · 3.89 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
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
151
152
153
154
155
156
157
158
var arrayBufferToBuffer = require('arraybuffer-to-buffer');
module.exports = function(uriPath, opts) {
opts = opts || {};
this.opts = opts;
var wsProtocol = 'ws://';
if(window.location.protocol.match(/^https/i)) {
wsProtocol = 'wss://';
}
this.url = wsProtocol + window.document.location.host + uriPath;
this.connected = false;
this._listeners = [];
this._sentCallbacks = {};
this.curId = 0;
this._id = function() {
var b = Buffer.alloc(2);
b.writeUInt16LE(this.curId);
if(this.curId >= (Math.pow(2, 16) - 1)) {
this.curId = 0;
} else {
this.curId++;
}
return b;
}
this._toHex = function(b) {
var s = ''
var c;
var i;
for(i=0; i < b.length; i++) {
s += (b[i] <= 16) ? '0'+b[i].toString(16) : b[i].toString(16);
}
return s;
}
this._stateChange = function(err) {
var isConnected = (this.socket.readyState === 1) ? true : false;
if(isConnected !== this.connected) {
this.connected = isConnected;
if(isConnected) err = undefined;
this.connectCb(err, isConnected);
if(!isConnected) {
this.reconnect();
} else {
this.attempt = 0;
}
}
};
// route incoming messages to the correct listeners
this._gotMessage = function(event) {
if(!event.data) {
console.warn("Received message with no data");
return;
}
console.log("msg length:", event.data.length)
var data = arrayBufferToBuffer(event.data);
// is the message shorter than the message ID?
if(data.length <= 2) {
console.warn("Received invalid message (too short)");
return;
}
var id = data.slice(0, 2);
data = data.slice(2);
if(opts.debug) console.log("[websocket rx]", this._toHex(id), data.toString('utf8'))
// is this an ACK?
if((data.slice(0, 1).toString('utf8') === '!') && (data.length === 1)) {
var sentCb = this._sentCallbacks[id];
if(!sentCb) {
console.warn("Got ACK for unknown message ID: " + id);
return;
}
clearTimeout(sentCb.timeout);
sentCb.callback();
delete this._sentCallbacks[id];
return;
}
var i, l, namespace, split;
for(i=0; i < this._listeners.length; i++) {
l = this._listeners[i]
if(data.indexOf(l.namespace) !== 0) continue;
if(!(split = data.indexOf('|'))) {
console.error("invalid message received: no namespace")
continue;
}
if(data.length < split + 2) {
console.error("invalid message received: empty message")
continue;
}
namespace = data.slice(0, split);
data = data.slice(split + 1);
l.callback(namespace, data);
}
};
this.connect = function(connectCb) {
this.connectCb = this.connectCb || connectCb;
this.socket = new WebSocket(this.url);
this.socket.binaryType = 'arraybuffer';
this.socket.onopen = this._stateChange.bind(this);
this.socket.onerror = this._stateChange.bind(this);
this.socket.onclose = this._stateChange.bind(this);
this.socket.onmessage = this._gotMessage.bind(this);
};
this.reconnect = function() {
this.attempt++;
// exponential backoff
var delay = Math.floor(Math.pow(2, this.attempt) * 1000);
setTimeout(this.connect.bind(this), delay);
};
this.send = function(namespace, msg, cb) {
var msgID = this._id()
msg = Buffer.concat([msgID, Buffer.from(namespace + '|' + msg, 'utf8')])
this.socket.send(msg)
this._sentCallbacks[msgID] = {
callback: cb,
timeout: setTimeout(function() {
if(!this._sentCallbacks[msgID]) return
cb(new Error("Timed out while waiting for ack"));
}.bind(this), 5000)
};
};
this.addListener = function(namespace, cb) {
if(!Buffer.isBuffer(namespace)) {
namespace = Buffer.from(namespace, 'utf8');
}
this._listeners.push({
namespace: namespace,
callback: cb
});
};
}