-
Notifications
You must be signed in to change notification settings - Fork 0
/
servers.js
executable file
·57 lines (47 loc) · 1.33 KB
/
servers.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
const app = require('express')();
const cors = require('cors');
const sslConfig = require('./ssl-config');
keysOpt = {
key: sslConfig.keySSL,
cert: sslConfig.crtSSL,
};
const server = require('https').Server(keysOpt, app);
const io = require('socket.io')(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
transports: ['websocket', 'polling'],
credentials: true
},
allowEIO3: true
});
const port = 3000;
server.listen(port, () => {
// eslint-disable-next-line no-console
console.info('listening on %d', port);
});
io.on('connection', (socket) => {
// console.log(socket.rooms,'ss');
socket.on('register',(id) =>{
socket.join(id);
});
console.log('a user connected');
socket.on('disconnect', () => {
console.log('user disconnected');
});
socket.on('sendMsg', (data) => {
io.to(data.receiver_id+"").emit('receiveMsg', data);
});
socket.on('deleteMsg', (data) => {
console.log(data);
io.to(data.receiver_id+"").emit('receiveDeleteMsg', data);
});
socket.on('updateMsg', (data) => {
console.log(data);
io.to(data.receiver_id+"").emit('receiveUpdateMsg', data);
});
socket.on('readMsg', (data) => {
console.log(data);
io.to(data.id+"").emit('receiveReadMsg', data);
});
});