-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (38 loc) · 1.41 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
//SteamVR Social Nodejs server
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(process.env.PORT || 3000);
var shortid = require('shortid');
console.log('server started');
var players=[];
io.sockets.on('connection', function(socket){
var thePlayerId = shortid.generate();
players.push(thePlayerId);
console.log('client connected, broadcasting spawn:', { id: thePlayerId });
io.emit('spawn', { id: thePlayerId });
io.emit('requestPosition');
// players.forEach(function(playerId){
// if(playerId == thePlayerId)
// return;
//
// socket.emit('spawn', { id: playerId });
// console.log('sending spawn to connected clients', playerId);
// });
socket.on('move', function(data) {
data.id = thePlayerId;
console.log('Player moved',JSON.stringify(data));
socket.broadcast.emit('move', data);
});
socket.on('updatePosition', function(data) {
data.id = thePlayerId;
console.log('update position', data);
socket.broadcast.emit('updatePosition', data);
});
socket.on('disconnect', function() {
console.log('client disconnected: ', { id: thePlayerId });
players.splice(players.indexOf(thePlayerId),1);
socket.broadcast.emit('disconnected',{ id: thePlayerId });
});
});