-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
39 lines (32 loc) · 1.15 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
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server, {
pingTimeout: 60000,
});
app.use(express.static('public'));
app.get('/', (req, res) => {
console.log('has reached to app get');
res.sendFile(`${__dirname}/public/index.html`);
});
io.on('connection', (socket) => {
socket.on('join_room', (data) => {
console.log(io.rooms);
socket.join(data.roomname);
console.log(`${data.username} has joined the room called ${data.roomname}`);
// socket.broadcast.emit('user_connect', `${data.username} has joined the room`);
io.to(data.roomname).emit('user_connect', `${data.username} has joined the room`);
io.to(data.roomname).emit('join_room', data.username);
socket.on('disconnect', () => {
console.log('has disconnected');
socket.broadcast.emit('user_disconnect', `${data.username} has left the room`);
});
});
// socket.broadcast.emit('user_connect', 'a user has joined the room');
socket.on('chat message', msg => (
io.emit('chat message', msg)
));
});
server.listen(8000, () => (
console.log('listening on *:8000')
));