-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
36 lines (30 loc) · 1.14 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
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
port = process.env.PORT || 8181,
scrape = require('scrape');
app.configure(function() {
app.use(express.static(__dirname + '/generated'));
});
var appState = {}
io.sockets.on('connection', function(socket) {
socket.on('createParty', function(data) {
appState[data.room] = data.state;
socket.join(data.room);
socket.emit('onPartyCreated', {'room':data.room, 'state':appState[data.room]});
});
socket.on('joinParty', function(room) {
socket.join(room);
io.sockets.in(room).emit('onPartyJoined', room);
});
socket.on('syncState', function(data){
appState[data.room] = data.state;
//send back to all clients in room except host
socket.broadcast.to(data.room).emit('onSyncState', appState[data.room]);
});
socket.on('playerAction', function(data){
io.sockets.in(data.room).emit('onPlayerAction', {'action':data.action, 'args':data.args});
});
});
server.listen(port);