-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
38 lines (31 loc) · 1.02 KB
/
main.js
File metadata and controls
38 lines (31 loc) · 1.02 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
var http = require('http');
var sockjs = require('sockjs');
var node_static = require('node-static');
// 1. Echo sockjs server
var sockjs_opts = {sockjs_url: "http://cdn.sockjs.org/sockjs-0.3.min.js"};
var sockjs_echo = sockjs.createServer(sockjs_opts);
var connections = [];
sockjs_echo.on('connection', function(conn) {
connections.push(conn);
conn.on('data', function(message) {
for (var i = 0; i < connections.length; i++) {
connections[i].write(message);
};
});
conn.on('close', function() {
connections.splice(connections.indexOf(conn), 1);
});
});
// 2. Static files server
var static_directory = new node_static.Server(__dirname + '/public');
// 3. Usual http stuff
var server = http.createServer();
server.addListener('request', function(req, res) {
static_directory.serve(req, res);
});
server.addListener('upgrade', function(req,res){
res.end();
});
sockjs_echo.installHandlers(server, {prefix:'/echo'});
console.log(' [*] Listening on 127.0.0.1:8080' );
server.listen(8080, '127.0.0.1');