forked from agentcooper/Box2d-networking
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
55 lines (43 loc) · 1.17 KB
/
app.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
var http = require('http'), io = require('socket.io'), fs = require('fs');
var Box2D = require('./box2d.js');
eval(fs.readFileSync('common.js') + '');
var clients = [];
function update() {
world.Step(1 / 60, 10, 10);
world.ClearForces();
}
setInterval(update, 1000 / 60);
function jointsToClients(data) {
for (var i = 0; i < clients.length; i++) {
clients[i].send(JSON.stringify(data));
}
}
setupWorld();
// SOCKETS
console.log("Starting socket server on port %d ...", xport);
var server = http.createServer(
function(req, res){
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<h1>Hello world</h1>');
}
);
server.listen(xport, xhost);
var socket = io.listen(server);
socket.on('connection', function(client) {
clients.push(client);
console.log("Total clients: " + clients.length);
client.send(JSON.stringify({"startId" : clients.length}));
client.on('message', function(data){
data = JSON.parse(data);
if (data.hasOwnProperty("destroyId")) {
deleteJoint(data.destroyId);
console.log('destroyed');
} else {
updateJoints(data);
}
jointsToClients(data);
});
client.on('disconnect', function(){
console.log("disconnect");
});
});