-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (49 loc) · 1.19 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
const express = require('express');
const http = require('http');
const app = express();
const server = http.createServer(app);
const io = require('socket.io')(server);
const port = process.env.PORT || 5000;
app.use(express.json());
app.get('/', (req, res) => {
res.send(`Node server is running on port ${port}!`);
});
var users = {};
io.on('connection', (socket) => {
socket.on('join', (id) => {
users[id] = socket;
console.log('Connected', id, socket.id);
});
socket.on('disconnect', () => {
console.log('Disconnected', socket.id);
});
socket.on('send', (id, data) => {
if (users[id]) {
users[id].emit('receive', data);
}
});
socket.on('delivered', (id, data) => {
if (users[id]) {
users[id].emit('delivered', data);
}
});
socket.on('read', (id, data) => {
if (users[id]) {
users[id].emit('read', data);
}
});
socket.on('typing', (id, data) => {
if (users[id]) {
users[id].emit('typing', data);
}
});
socket.on('delete', (id, data) => {
if (users[id]) {
users[id].emit('delete', data);
}
});
socket.on('error', (err) => {
console.log(err);
});
});
server.listen(port);