-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.js
More file actions
90 lines (78 loc) · 2.78 KB
/
Copy pathapp.js
File metadata and controls
90 lines (78 loc) · 2.78 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env node
/*global process, require */
/*jslint devel: true */
var express = require('express');
var http = require('http');
var atomize = require('atomize-server');
var path = require('path');
var app = express();
var httpServer = http.createServer(app);
var port = 9999;
var port_index = process.argv.indexOf('--port');
if (port_index > -1) {
port = process.argv[port_index + 1];
}
app.configure(function(){
app.use(express.logger('dev'));
app.use(app.router);
app.use(express.static(__dirname));
});
function serveJS (fileName, packageName) {
var p;
try {
p = require.resolve(packageName);
p = path.dirname(p);
p = path.join(path.join(p, 'lib'), fileName);
} catch (err) {
p = require.resolve('atomize-server');
p = path.dirname(p);
p = path.join(
path.join(
path.join(
path.join(p, 'node_modules'),
packageName),
'lib'),
fileName);
}
app.get('/' + fileName, function (req, res) {
res.sendfile(p);
});
}
serveJS('atomize.js', 'atomize-client');
serveJS('cereal.js', 'cereal');
serveJS('compat.js', 'atomize-client');
var atomizeServer = atomize.create(httpServer, '[/]atomize');
var atomizeClient = atomizeServer.client();
atomizeClient.atomically(function () {
atomizeClient.root.clients = atomizeClient.lift({server: {}});
}, function () {
atomizeServer.on('connection', function (client) {
client.on('close', function (client) {
atomizeClient.atomically(function () {
delete atomizeClient.root.clients[client.connection.id];
}, function () {
console.log("Connection death: " + client.connection.id);
});
});
// Example of how to implement authentication
/* client.on('data', function (message, client) {
var text = JSON.parse(message).text;
if (text === "wibble") {
client.connection.write(JSON.stringify({text: "wobble"}));
atomizeClient.atomically(function () {
atomizeClient.root.clients[client.connection.id] = atomizeClient.lift({});
}, function () {
console.log("New connection id: " + client.connection.id);
client.isAuthenticated = true;
});
} else {
client.connection.write(JSON.stringify({text: "denied"}));
}
}); */
console.log("New connection id: " + client.connection.id);
// disable this next line if you want to do some sort of real auth
client.isAuthenticated = true;
});
});
console.log(" [*] Listening on 0.0.0.0:" + port);
httpServer.listen(port);