-
Notifications
You must be signed in to change notification settings - Fork 42
/
app.js
64 lines (40 loc) · 1.28 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
const express = require('express');
const path = require('path');
const socketIO = require('socket.io');
const PORT = process.env.PORT || 5050;
///////////
/// APP
///////////
const app = express()
.use(express.static('public'))
.get('/', (req, res)=> {
res.sendFile(path.join(__dirname + '/public/index.html'));
})
.listen(PORT, ()=> {
console.log('App listening on port ' + PORT);
})
//////////////////
/// SOCKET.IO
//////////////////
const io = socketIO( app );
io.on( 'connection', async (client)=> {
client.on('playerInfo', (message)=> {
// join the room with the requested game name
io.sockets.sockets[ client.id ].join( message.pass );
client.roomId = message.pass ;
// record the ID created on client side.
// when the client quit, its game ID will be broadcasted to
// every other player in the same room.
client.gameId = message.id ;
// broadcast player position to every player in the same socket io "room"
client.broadcast.to( message.pass ).emit( 'playerInfo', message );
});
//
client.on( 'disconnect', async ()=> {
// broadcast the disconnection information to the other
// players of the same room
if ( client.roomId ) {
client.broadcast.to( client.roomId ).emit( 'playerLeft', client.gameId );
};
});
})