forked from dhbaird/easywsclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example-server.js
37 lines (30 loc) · 959 Bytes
/
example-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
/*
Prerequisites:
1. Install node.js and npm
2. npm install ws
See also,
http://einaros.github.com/ws/
To run,
node example-server.js
*/
"use strict"; // http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
var WebSocketServer = require('ws').Server;
var http = require('http');
var server = http.createServer();
var wss = new WebSocketServer({server: server, path: '/foo'});
wss.on('connection', function(ws) {
console.log('/foo connected');
ws.on('message', function(data, flags) {
if (flags.binary) { return; }
console.log('>>> ' + data);
if (data == 'goodbye') { console.log('<<< galaxy'); ws.send('galaxy'); }
if (data == 'hello') { console.log('<<< world'); ws.send('world'); }
});
ws.on('close', function() {
console.log('Connection closed!');
});
ws.on('error', function(e) {
});
});
server.listen(8126);
console.log('Listening on port 8126...');