-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathserver-common.js
More file actions
58 lines (46 loc) · 1.01 KB
/
Copy pathserver-common.js
File metadata and controls
58 lines (46 loc) · 1.01 KB
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
/**
* Common dependencies.
*/
var ws = require('../lib/websocket.io')
, Client = require('ws')
/**
* Creates a http.Server that listens on an ephemeral port.
*
* @api public
*/
listen = function (options, fn) {
if ('function' == typeof options) {
fn = options;
options = {};
}
var w = new ws.Server(options)
, server = require('http').createServer()
server.listen(function (err) {
if (err) throw err;
fn(server.address(), w);
});
server.on('upgrade', function (req, socket, head) {
w.handleUpgrade(req, socket, head);
});
// shortcut for closing the actual server
w.close = function () {
server.close();
}
return w;
}
/**
* Creates a client for the given address.
*
* @api public
*/
client = function (addr, path) {
var address = addr.address;
if (addr.family == 'IPv6') {
address = '[' + address + ']';
}
var cl = new Client('ws://' + address + ':' + addr.port + (path || ''));
cl.on('error', function (e) {
throw e;
});
return cl;
}