forked from espruino/EspruinoDocs
/
wss.js
278 lines (251 loc) · 9.19 KB
/
wss.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* Copyright (c) 2016 Pablo Rodiz, Sameh Hady, Gordon Williams. See the file LICENSE for copying permission. */
/*
Simple WebSocket protocol wrapper for Espruino sockets.
* KEYWORDS: Module,websocket,wss,socket,secure
Websocket implementation on Espruino, it let you control your Espruino from the cloud without the need to know it's IP.
You will need to use it with a websocket server.
Implementation for plain and encrypted WebSockets. For using secure Websockets over SSL just include ca, key and
certificate information in the options structure.
Limitations: The module only accept messages less than 127 character.
How to use the wss module:
```javascript
// Connect to WiFi, then...
// =============================== CLIENT
var WebSocket = require("wss");
var wss = new WebSocket("HOST",{
port: 8080,
protocolVersion: 13,
origin: 'Espruino',
keepAlive: 60 // Ping Interval in seconds.
});
wss.on('open', function() {
console.log("Connected to server");
});
wss.on('message', function(msg) {
console.log("MSG: " + msg);
});
wss.on('close', function() {
console.log("Connection closed");
});
wss.on('error', function(e) {
console.log("Error " + e);
});
//Send message to server
wss.send("Hello Server");
// =============================== SECURE CLIENT
//See this thread http://forum.espruino.com/conversations/277780/?offset=50 on how to create and use the certificates
var WebSocket = require("wss");
var wss = new WebSocket("HOST",{
port: 8080,
protocolVersion: 13,
origin: 'Espruino',
keepAlive: 60, // Ping Interval in seconds.
key : okey,
ca : oca,
cert : ocert
});
wss.on('open', function() {
console.log("Connected to server");
});
wss.on('message', function(msg) {
console.log("MSG: " + msg);
});
wss.on('close', function() {
console.log("Connection closed");
});
wss.on('error', function(e) {
console.log("Error " + e);
});
//Send message to server
wss.send("Hello Server");
// =============================== SERVER
var page = '<html><body><script>var ws;setTimeout(function(){';
page += 'ws = new WebSocket("ws://" + location.host + "/my_websocket", "protocolOne");';
page += 'ws.onmessage = function (event) { console.log("MSG:"+event.data); };';
page += 'setTimeout(function() { ws.send("Hello to Espruino!"); }, 1000);';
page += '},1000);</script></body></html>';
function onPageRequest(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(page);
}
var server = require('ws').createServer(onPageRequest);
server.listen(8000);
server.on("websocket", function(ws) {
ws.on('message',function(msg) { print("[WS] "+JSON.stringify(msg)); });
ws.send("Hello from Espruino!");
});
// =============================== SECURE SERVER
// There is no implementation yet available for https server, so I think it is not yet possible to
//create a secure websocket implementation... at least easily :)
```
*/
/** Minify String.fromCharCode() call */
var strChr = String.fromCharCode;
function WebSocket(host, options) {
this.socket = null;
options = options || {};
this.host = host;
this.port = options.port || 80;
this.protocolVersion = options.protocolVersion || 13;
this.origin = options.origin || 'Espruino';
this.keepAlive = options.keepAlive * 1000 || 60000;
this.key = options.key;
this.ca = options.ca;
this.cert = options.cert;
this.websocketKey = btoa(Array(16+1).join(((E.hwRand()*E.hwRand()*E.hwRand()).toString(36)+'00000000000000000').slice(2, 18)).slice(0, 16));
}
WebSocket.prototype.initializeConnection = function () {
if(typeof this.key !== 'undefined' && typeof this.ca !== 'undefined' && typeof this.cert !== 'undefined') {
require("tls").connect({
host: this.host,
port: this.port,
key : this.key,
ca : this.ca,
cert : this.cert,
}, this.onConnect.bind(this));
} else {
require("net").connect({
host: this.host,
port: this.port,
}, this.onConnect.bind(this));
}
};
WebSocket.prototype.onConnect = function (socket) {
this.socket = socket;
var ws = this;
this.socket.on('data', function(data) {
try {
ws.parseData(data);
} catch (e) {
if(e!="Request to close connection received") {
ws.emit('error', e );
}
//We need to exit ths function before ending the socket or the execution will stop
setTimeout(function() {ws.socket.end();}, 0);
}
});
this.socket.on('close', function () {
ws.emit('close');
});
this.socket.on('error', function (err) {
//this.emit('error', 'Socket error');
});
this.emit('open');
this.handshake();
};
WebSocket.prototype.parseData = function (data) {
// see https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers
// Note, docs specify bits 0-7, etc - but BIT 0 is the MSB, 7 is the LSB
// TODO: handle >1 data packet, or packets split over multiple parseData calls
var ws = this;
this.emit('rawData', data);
if (data.indexOf('HTTP/1.1') > -1) {
if (data.indexOf('101 Switching Protocols') > -1 && data.indexOf(btoa(require("crypto").SHA1(this.websocketKey+'258EAFA5-E914-47DA-95CA-C5AB0DC85B11'))) > -1) {
this.emit('handshake');
var ping = setInterval(function () {
ws.send('ping', 0x89);
}, this.keepAlive);
} else {
// Handshake answer from the server was not correct
//ws.socket.end();
// We can not finish the socket while executing parseData callback or program
//will break when returning. We fire an exception to ask the parent function to close the socket instead
throw "Handshake error";
}
} else {
var opcode = data.charCodeAt(0)&15;
if (opcode == 0xA) {
this.emit('pong');
return;
}
if (opcode == 0x9) {
this.send('pong', 0x8A);
this.emit('ping');
return;
}
if (opcode == 0x8) {
// connection close request
//ws.socket.end();
// We can not finish the socket while executing parseData callback or program
//will break when returning. We fire an exception to ask the parent function to close the socket instead
// we'll emit a 'close' when the socket itself closes
throw "Request to close connection received";
}
if (opcode == 1 /* text - all we're supporting */) {
var dataLen = data.charCodeAt(1)&127;
if (dataLen>126) throw "Messages >125 in length unsupported";
var offset = 2;
var mask = [ 0,0,0,0 ];
if (data.charCodeAt(1)&128 /* mask */)
mask = [ data.charCodeAt(offset++), data.charCodeAt(offset++),
data.charCodeAt(offset++), data.charCodeAt(offset++)];
var message = "";
for (var i = 0; i < dataLen; i++) {
message += String.fromCharCode(data.charCodeAt(offset++) ^ mask[i&3]);
}
this.emit('message', message);
}
}
};
WebSocket.prototype.handshake = function () {
var socketHeader = [
"GET / HTTP/1.1",
"Upgrade: websocket",
"Connection: Upgrade",
"Sec-WebSocket-Key: " + this.websocketKey,
"Sec-WebSocket-Version: " + this.protocolVersion,
"Sec-WebSocket-Protocol: echo-protocol",
"Origin: " + this.origin,
"Host: " + this.host,
"",""
];
this.socket.write(socketHeader.join("\r\n"));
};
WebSocket.prototype.close = function () {
this.socket.write(strChr(0x88)+strChr(0x02)+strChr(0x10)+strChr(0x00));
};
/** Send message based on opcode type */
WebSocket.prototype.send = function (msg, opcode) {
opcode = opcode === undefined ? 0x81 : opcode;
this.socket.write(strChr(opcode, msg.length));
this.socket.write(msg);
};
/** Create a WebSocket client */
exports = function (host, options) {
var ws = new WebSocket(host, options);
ws.initializeConnection();
return ws;
};
/** Create a WebSocket server */
exports.createServer = function(callback, wscallback) {
var server = require('http').createServer(function (req, res) {
if (req.headers.Connection=="Upgrade") {
var key = req.headers["Sec-WebSocket-Key"];
var accept = btoa(E.toString(require("crypto").SHA1(key+"258EAFA5-E914-47DA-95CA-C5AB0DC85B11")));
res.writeHead(101, {
'Upgrade': 'websocket',
'Connection': 'Upgrade',
'Sec-WebSocket-Accept': accept,
'Sec-WebSocket-Protocol': req.headers["Sec-WebSocket-Protocol"]
});
res.write(""); /** Completes the webSocket handshake on pre-1v85 builds **/
var ws = new WebSocket(undefined, {});
ws.socket = res;
req.on('data', ws.parseData.bind(ws) );
req.on('close', function() {
// if srvPing is undefined, we already emitted a 'close'
clearInterval(ws.srvPing);
ws.srvPing = undefined;
// emit websocket close event
ws.emit('close');
});
/** Start a server ping at the keepAlive interval **/
ws.srvPing = setInterval(function () {
ws.emit('ping', true); // true: indicates a server ping
ws.send('ping', 0x89);
}, ws.keepAlive);
server.emit("websocket", ws);
} else callback(req, res);
});
return server;
};