-
Notifications
You must be signed in to change notification settings - Fork 113
/
echo-server.js
51 lines (40 loc) · 889 Bytes
/
echo-server.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 http = require('http')
var websocket = require('./')
var server = null
var port = module.exports.port = 8343
var url = module.exports.url = 'ws://localhost:' + module.exports.port
module.exports.start = function(opts, cb) {
if (server) {
cb(new Error('already started'));
return;
}
if (typeof opts == 'function') {
cb = opts;
opts = {};
}
server = http.createServer()
opts.server = server
websocket.createServer(opts, echo)
server.listen(port, cb)
function echo(stream) {
stream.pipe(stream)
}
}
module.exports.stop = function(cb) {
if (!server) {
cb(new Error('not started'))
return
}
server.close(cb)
server = null
}
if (!module.parent) {
module.exports.start(function(err) {
if (err) {
console.error(err);
return;
}
console.log('Echo server started on port ' + port);
});
}