-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
76 lines (65 loc) · 2.12 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.use(express.static('./public_html/blockland/'));
app.use(express.static('./public_html/libs'));
app.get('/',function(req, res) {
res.sendFile(__dirname + './public_html/blockland/v3/index.html');
});
io.sockets.on('connection', function(socket){
socket.userData = { x:0, y:0, z:0, heading:0 };//Default values;
console.log(`${socket.id} connected`);
socket.emit('setId', { id:socket.id });
socket.on('disconnect', function(){
socket.broadcast.emit('deletePlayer', { id: socket.id });
});
socket.on('init', function(data){
console.log(`socket.init ${data.model}`);
socket.userData.model = data.model;
socket.userData.colour = data.colour;
socket.userData.x = data.x;
socket.userData.y = data.y;
socket.userData.z = data.z;
socket.userData.heading = data.h;
socket.userData.pb = data.pb,
socket.userData.action = "Idle";
});
socket.on('update', function(data){
socket.userData.x = data.x;
socket.userData.y = data.y;
socket.userData.z = data.z;
socket.userData.heading = data.h;
socket.userData.pb = data.pb,
socket.userData.action = data.action;
});
socket.on('chat message', function(data){
console.log(`chat message:${data.id} ${data.message}`);
io.to(data.id).emit('chat message', { id: socket.id, message: data.message });
})
});
http.listen(process.env.PORT || 2002, function(){
console.log(`listening on *:${process.env.PORT || 2002}`);
});
setInterval(function(){
const nsp = io.of('/');
let pack = [];
for(let id in io.sockets.sockets){
const socket = nsp.connected[id];
//Only push sockets that have been initialised
if (socket.userData.model!==undefined){
pack.push({
id: socket.id,
model: socket.userData.model,
colour: socket.userData.colour,
x: socket.userData.x,
y: socket.userData.y,
z: socket.userData.z,
heading: socket.userData.heading,
pb: socket.userData.pb,
action: socket.userData.action
});
}
}
if (pack.length>0) io.emit('remoteData', pack);
}, 40);