server.js
1.36 KB
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
var express = require('express');
var sockjs = require('sockjs');
var websocket_multiplex = require('websocket-multiplex');
// 1. Setup SockJS server
var sockjs_opts = {sockjs_url: "http://cdn.jsdelivr.net/sockjs/1.0.1/sockjs.min.js"};
var service = sockjs.createServer(sockjs_opts);
// 2. Setup multiplexing
var multiplexer = new websocket_multiplex.MultiplexServer(service);
var ann = multiplexer.registerChannel('ann');
ann.on('connection', function(conn) {
conn.write('Ann says hi!');
conn.on('data', function(data) {
conn.write('Ann nods: ' + data);
});
});
var bob = multiplexer.registerChannel('bob');
bob.on('connection', function(conn) {
conn.write('Bob doesn\'t agree.');
conn.on('data', function(data) {
conn.write('Bob says no to: ' + data);
});
});
var carl = multiplexer.registerChannel('carl');
carl.on('connection', function(conn) {
conn.write('Carl says goodbye!');
// Explicitly cancel connection
conn.end();
});
// 3. Express server
var app = express.createServer();
service.installHandlers(app, {prefix:'/multiplex'});
console.log(' [*] Listening on 0.0.0.0:9999' );
app.listen(9999, '0.0.0.0');
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
app.get('/multiplex.js', function (req, res) {
res.sendfile(__dirname + '/multiplex.js');
});