-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (58 loc) · 1.69 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const express = require('express');
const logger = require('morgan');
const bodyParser = require('body-parser');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
let users = {};
const PORT = process.env.PORT || 3001;
app.use(express.static(`${__dirname}/connect/build`));
io.on('connection', socket => {
//new connection
socket.emit('new', socket.id);
//receives user name and sends out player 1 and player 2
socket.on('user-info', function(data) {
let code = data.code;
let name = data.name;
if (name && code) {
users[socket.id] = name;
socket.join(code, function() {
io.of('/')
.in(code)
.clients(function(err, clients) {
if (err) {
console.log(err);
} else if (clients.length < 3) {
let players = {
player1: users[clients[0]],
player2: users[clients[1]]
};
// console.log('emitting');x
io.in(code).emit('users', players);
}
});
});
}
});
//update current board to both players
socket.on('state', function(data) {
// console.log(data);
let code = data.player.code;
io.in(code).emit('board', data);
});
//update current player
socket.on('players', function(data) {
io.emit('info', data);
});
//receives winner
socket.on('winner', function(data) {
let code = data.player.code;
io.in(code).emit('winning player', data);
});
});
app.get('*', (req, res) => {
res.sendFile(`${__dirname}/connect/build/index.html`);
});
server.listen(PORT, err => {
console.log(err || 'server listening on port 3001');
});