-
Notifications
You must be signed in to change notification settings - Fork 15
/
ws.js
51 lines (35 loc) · 1.03 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
'use strict';
var jschan = require('jschan');
var inherits = require('inherits');
var Server = require('./lib/server');
var Client = require('./lib/client');
function WSServer(opts) {
if (!(this instanceof WSServer)) {
return new WSServer(opts);
}
Server.call(this, opts);
}
inherits(WSServer, Server);
WSServer.prototype._buildServer = function(opts) {
var server = jschan.websocketServer(opts.server);
if (!opts.server) {
server.listen(opts.port || 0);
}
this.address = server.address.bind(server);
server.on('listening', this.emit.bind(this, 'ready', this));
return server;
};
module.exports.server = WSServer;
function WSClient(opts) {
if (!(this instanceof WSClient)) {
return new WSClient(opts);
}
Client.call(this, opts);
}
inherits(WSClient, Client);
WSClient.prototype._buildSession = function(opts) {
var client = jschan.websocketClientSession(opts);
client._inStream.on('connect', this.emit.bind(this, 'ready', this));
return client;
};
module.exports.client = WSClient;