-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
37 lines (29 loc) · 908 Bytes
/
server.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
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
http.listen(3000, () => {
console.log('listening on *:3000');
});
io.on('connection', (socket) => {
console.log('connected');
socket.on('register', (id) => {
console.log('register: ' + id);
socket.join(id);
socket.join(id, () => {
let rooms = Object.keys(socket.rooms);
console.log(rooms);
socket.broadcast.in(id).emit('registered', id);
});
socket.on('pair', (data) => {
socket.broadcast.in(id).emit('pair', data);
});
});
socket.on('disconnect', () => {
console.log('user disconnected');
});
});