-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_socket.js
108 lines (87 loc) · 2.52 KB
/
web_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
var crypto = require('crypto');
var EventEmitter = require('events').EventEmitter;
var http = require('http');
var https = require('https');
var url = require('url');
var util = require('util');
var revolt = require('revolt');
var WebSocket = module.exports = function(address, httpOptions) {
EventEmitter.call(this);
this.options = {
method: 'GET',
headers: {
'Connection': 'Upgrade',
'Upgrade': 'websocket',
'Sec-WebSocket-Version': '13',
}
};
this.isClosed = false;
this._requestExtensions = [];
this._responseExtensions = [];
var self = this;
if (httpOptions) {
for (k in httpOptions) {
self.options[k] = httpOptions[k];
}
}
this.setAddress(address);
this.request = revolt();
};
util.inherits(WebSocket, EventEmitter);
WebSocket.prototype.setAddress = function(address) {
if (address.substr(0, 2) === 'ws') {
address = 'http' + address.substr(2);
}
this.options.uri = address;
var parsed = url.parse(address);
this.options.headers['Host'] = parsed.host;
};
WebSocket.prototype.extendRequest = function(extensions) {
this._requestExtensions = extensions;
};
WebSocket.prototype.extendResponse = function(extensions) {
this._responseExtensions = extensions;
};
WebSocket.prototype.close = function() {
if (this.isClosed) {
return;
}
this.isClosed = true;
if(this.socket) {
this.socket.end();
// this.emit('close', null, null, true);
}
};
WebSocket.prototype.start = function() {
var key = new Buffer('13' + '-' + Date.now()).toString('base64');
var shasum = crypto.createHash('sha1');
shasum.update(key + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11');
var expectedServerKey = shasum.digest('base64');
this.options.headers['Sec-WebSocket-Key'] = key;
var self = this;
var req = this.request;
this._requestExtensions.forEach(function(ext) {
ext(req);
});
req = req.request(self.options);
this._responseExtensions.forEach(function(ext) {
req = ext(req);
});
var subscription = req.subscribe(function(env) {
var serverKey = env.response.headers['sec-websocket-accept'];
if (typeof serverKey == 'undefined' || serverKey !== expectedServerKey) {
self.emit('error', 'invalid server key');
return;
}
self.isClosed = false;
self.socket = env.request.connection;
self.socket.on('close', function() {
self.emit('close');
env.request.abort();
subscription.dispose();
});
self.emit('open', env.request.connection);
}, function(err) {
self.emit('error', err);
});
};