-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
73 lines (60 loc) · 2.08 KB
/
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
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
67
68
69
70
71
72
73
const fs = require('fs');
const path = require('path');
const os = require('os');
const express = require('express');
const app = express();
const socketIO = require('socket.io');
app.use( express.static(path.resolve(__dirname, 'work')))
app.get('/:page', (req, res) => {
const page = req.params.page || 'index';
if (!page.includes('favicon') && !page.includes('robots')) {
res.sendFile(path.resolve(__dirname, 'work', `${page}.html`));
}
})
app.use( express.static(path.resolve(__dirname)))
app.get('/codelab-examples/:step/:page', (req, res) => {
const { step, page } = req.params;
res.sendFile(path.resolve(__dirname, 'codelab-examples', step, `${page ? page : 'index'}.html`));
});
const server = app.listen(5000, () => {
console.log('running on port 5000');
})
// socket
const io = socketIO.listen(server);
io.sockets.on('connection', (client) => {
// receiving messages
client.on('message', (message) => {
console.log(`client said:${message}`);
client.emit('message', message);
})
client.on('create or join', (room) => {
const clientsInRoom = io.sockets.adapter.rooms[room];
const clientsCount = clientsInRoom ? Object.keys(clientsInRoom.sockets).length : 0;
console.log(`creating or join room: ${room}`);
console.log(clientsCount);
// join or create stuff
if (clientsCount < 2) {
console.log(`${room} has ${clientsCount} client(s)`)
client.join(room)
if (clientsCount == 0) {
client.emit('created', room, client.id)
} else {
client.emit('joined', room, client.id)
client.send().in(room).emit('ready')
console.log(` client id ${client.id} has joined ${room}`)
}
} else {
client.emit('full', room);
}
})
client.on('ipAddr', () => {
const iFaces = os.networkInterfaces();
for (let dev in iFaces) {
iFaces[dev].forEach((details) => {
if (details.family === 'IPv4' && details.address !== '127.0.0.1') {
client.emit('ipAddr', details.address)
}
})
}
})
})