-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (39 loc) · 1.07 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
// setup express
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
// setup socket.io
const socketIO = require("socket.io");
const io = socketIO(server, {
cors: {
origin: '*',
methods: ['GET', 'POST']
}
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
console.log(`user ${socket.id} is connected!`);
socket.on('messageRemove', data => {
console.log('messageRemove - data:', data);
socket.broadcast.emit('message:chat-remove', data)
})
socket.on('message', data => {
socket.broadcast.emit('message:chat', data)
})
socket.on('typing', data => {
socket.broadcast.emit('message:typing', data)
})
socket.on('stopTyping', data => {
socket.broadcast.emit('message:stopTyping', data)
})
// disconnect socket when user is close the browser or tab
socket.on('disconnect', () => {
console.log(`user ${socket.id} has left!`)
})
});
server.listen(3000, () => {
console.log('listening on 3000');
});