-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (31 loc) · 1.12 KB
/
index.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
/**
* @file Entry point to the app
*/
const express = require('express');
const socketIO = require('socket.io');
const registerService = require('./services/register');
const { Game, Events } = require('./controllers/game');
const PORT = process.env.PORT || 3000;
// Set up web server with express and socket.io
const app = express();
app.use(express.static('views'));
// Start the server
const server = app.listen(PORT, (err) => {
err ? console.error(err)
: console.log(`Server running at ${PORT}`);
});
const io = socketIO(server);
// Middleware to authenticate the user before socket connection
io.use(registerService);
// Socket routes
io.on('connection', socket => {
socket.on('disconnect', () => {
console.log('User disconnected: ', socket.handshake.query.username);
});
socket.on(Events.NEW_GAME, Game.new.bind(null, io, socket));
socket.on(Events.JOIN_GAME, Game.join.bind(null, io, socket));
socket.on(Events.DONE, Game.done.bind(null, io, socket));
console.log('User connected: ', socket.handshake.query.username);
// Send initial list of games to new users
Game.sendAllGames(io, socket);
});