-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
62 lines (53 loc) · 1.83 KB
/
server.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
var express = require('express');
var app = express();
var socket = require('socket.io');
app.configure(function(){
app.use(express.static(__dirname + '/'));
});
var server = app.listen(8000);
var io = socket.listen(server);
io.set('log level', 2);
players = [];
function findWithAttr(array, attr, value) {
for(var i = 0; i < array.length; i += 1) {
if(array[i][attr] === value) {
return i;
}
}
}
io.sockets.on('connection', function (socket) {
console.log('Client connected from: ' + socket.handshake.address.address);
// setTimeout(function () {
// socket.send(JSON.stringify({'text': 'waited 2 seconds'}) );
// }, 2000);
socket.on('disconnect', function () {
console.log("disconnect");
if (socket.username) {
socket.broadcast.emit('m5', JSON.stringify( {'name': socket.username} ));
players.splice(players.indexOf(socket.username), 1);
}
});
socket.on('m1', function (data) {
// get coordinates and rotation
socket.broadcast.emit('m2', data);
});
socket.on('m5', function (data) {
// new listener, send connected players
console.log('--new listener, send player list' + JSON.stringify(players));
socket.emit('m6', JSON.stringify(players) );
});
socket.on('m3', function (data) {
// new player, get name from client
obj = JSON.parse(data);
if (obj.name == undefined) obj.name = 'user'+players.length
console.log('-- new player: '+obj.name)
// send all players
socket.emit('m6', JSON.stringify(players) );
if (! (players.indexOf(obj.name) > -1) ) {
players.push(obj.name)
socket.username = obj.name
console.log(players)
socket.broadcast.emit('m4', JSON.stringify(obj));
}
});
});