-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
47 lines (38 loc) · 1.11 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
var express = require('express')
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
var inLobby = {};
app.use(express.static('public'));
app.get('/' , function(req, res) {
res.sendFile(__dirname + '/public/game.html');
});
io.on('connection', function(socket) {
console.log('a user connected');
socket.on('disconnect', function() {
delete inLobby[socket.id];
io.emit('LobbyChange', inLobby);
io.emit('leftMatch' , socket.id);
});
socket.on('move', function(move) {
io.to(move.opponent).emit('move', move);
});
socket.on('login', function(user) {
inLobby[user.id] = user;
io.emit('LobbyChange', inLobby);
});
socket.on('invite', function(opponent) {
delete inLobby[socket.id];
delete inLobby[opponent];
io.to(opponent).emit('gameStart', socket.id);
io.emit('LobbyChange', inLobby);
});
socket.on('message', function(message) {
io.to(message.dest).emit('message', message);
});
});
console.log(port);
http.listen(port, function() {
console.log('listening on *:' + port);
});