-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.js
32 lines (26 loc) · 862 Bytes
/
io.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
// Middlewares
const userAuthMiddleware = require("./app/io/middlewares/userAuth");
// Handlers
const gameHandler = require("./app/io/handlers/Game/gameHandler");
const disconnectionHandler = require('./app/io/handlers/disconnectionHandler');
module.exports = (server) => {
const io = require('socket.io')(server, { // Options:
// For later use. Probably will be needed a change (doesn't have any purpose now)
pingTimeout: 30000,
pingInterval: 10000
});
io.use(userAuthMiddleware);
io.on('connection', async (socket) => {
console.log("Connected. User: ", socket.user.username);
try {
await gameHandler(io, socket);
await disconnectionHandler(io, socket);
} catch (err) {
console.log("socket error: ", err);
}
});
io.on('error', async (err) => {
console.log("ERROR: ", err);
});
return io;
}