-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathws-vs-si.js
executable file
·64 lines (59 loc) · 2.15 KB
/
ws-vs-si.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
58
59
60
61
62
63
64
#!/usr/bin/env node
// WebSocket vs. Socket.IO example - common backend - from:
// https://github.com/rsp/node-websocket-vs-socket.io
// Copyright (c) 2015, 2016 Rafał Pocztarski
// Released under MIT License (Expat) - see:
// https://github.com/rsp/node-websocket-vs-socket.io/blob/master/LICENSE.md
/*eslint-disable no-loop-func*/
var path = require('path');
var express = require('express');
var log = function (m) {
console.error(new Date().toISOString()+' '+this.pre+m);
}
console.error("node-websocket-vs-socket.io\n"
+"WebSocket vs. Socket.IO on Node.js with Express.js - see:\n"
+"https://github.com/rsp/node-websocket-vs-socket.io#readme\n"
+"Copyright (c) 2015, 2016 Rafał Pocztarski\n"
+"Released under MIT License (Expat) - see:\n"
+"https://github.com/rsp/node-websocket-vs-socket.io/blob/master/LICENSE.md\n");
// WebSocket:
var ws = {app: express(), pre: "websocket app: ", log: log};
ws.ws = require('express-ws')(ws.app);
ws.app.get('/', (req, res) => {
ws.log('express connection - sending html');
res.sendFile(path.join(__dirname, 'ws.html'));
});
ws.app.ws('/', (s, req) => {
ws.log('incoming websocket connection');
for (var t = 0; t < 3; t++)
setTimeout(() => {
ws.log('sending message to client');
s.send(ws.pre+'message from server', ()=>{});
}, 1000*t);
});
ws.app.listen(3001, () =>
ws.log('listening on http://localhost:3001/'));
ws.log('starting server');
// Socket.IO:
var si = {app: express(), pre: "socket.io app: ", log: log};
si.http = require('http').Server(si.app);
si.io = require('socket.io')(si.http);
si.app.get('/', (req, res) => {
si.log('express connection - sending html');
res.sendFile(path.join(__dirname, 'si.html'));
});
si.app.get('/forced', (req, res) => {
si.log('express connection - sending html');
res.sendFile(path.join(__dirname, 'si-forced.html'));
});
si.io.on('connection', s => {
si.log('incoming socket.io connection');
for (var t = 0; t < 3; t++)
setTimeout(() => {
si.log('sending message to client');
s.emit('message', si.pre+'message from server');
}, 1000*t);
});
si.http.listen(3002, () =>
si.log('listening on http://localhost:3002/'));
si.log('starting server');