-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
executable file
·57 lines (50 loc) · 1.72 KB
/
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
52
53
54
55
56
57
var WebSocketServer = require('ws').Server;
var http = require('http');
var ipaddr = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 3000
/*var io = require('socket.io').listen(16000,ipaddr);
io.sockets.on('connection', function (socket) {
console.log('io connection');
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
console.log('io listening on port 16000');
*/
var server = http.createServer();
//var wss = new WebSocketServer({server: server, path: '/connect'});
var wss = new WebSocketServer({server:server})
console.log(wss);
wss.on('connection', function(ws) {
console.log('/connection connected');
ws.on('message', function(data, flags) {
if (flags.binary) { return; }
console.log('>>> ' + data);
if (data == 'test') { console.log('test'); ws.send('got test'); }
if (data == 'hello') { console.log('hello'); ws.send('WAZZZUP!'); }
});
ws.on('close', function() {
console.log('Connection closed!');
});
ws.on('error', function(e) {
console.log(e);
});
});
wss.on('connect', function(ws) {
console.log('/connect connected');
ws.on('message', function(data, flags) {
if (flags.binary) { return; }
console.log('>>> ' + data);
if (data == 'test') { console.log('test'); ws.send('got test'); }
if (data == 'hello') { console.log('hello'); ws.send('WAZZZUP!'); }
});
ws.on('close', function() {
console.log('Connection closed!');
});
ws.on('error', function(e) {
console.log(e);
});
});
console.log('Listening at IP ' + ipaddr +' on port '+port);
server.listen(port,ipaddr);