-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
137 lines (112 loc) · 3.76 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const events = require('events');
class IOSessionHandler {
constructor() {
this.socketIO
this.sessions = []
this.eventEmitter = new events.EventEmitter()
}
from(io, opts = { timeout: 5000 }) {
this.socketIO = io;
io.sockets.on('connection', (socket) => {
const token = socket.handshake.query.token
const id = socket.id
// Connected
this.onConnect(id, token, (data) => {
this.emitConnectionEvent(data, 1);
})
socket.on('disconnect', () => {
// Disconnected
this.onDisconnect(id, token, opts.timeout, (data) => {
this.emitConnectionEvent(data, 0);
})
})
socket.on('client_data', (client_data) => {
client_data = JSON.parse(client_data)
this.addData(client_data.token, client_data.data)
this.eventEmitter.emit('onClientData', client_data);
})
socket.on('push_message_delivery', (data) => {
data = JSON.parse(data)
this.eventEmitter.emit('onMessageDelivered', data);
})
})
return this
}
emitConnectionEvent(connection, status) {
connection.status = status;
this.eventEmitter.emit('connectionListener', connection);
}
connectionListener(listener) {
this.eventEmitter.on('connectionListener', (connection) => listener(connection));
}
onMessageDelivered(listener) {
this.eventEmitter.on('onMessageDelivered', (data) => listener(data));
}
onClientData(listener) {
this.eventEmitter.on('onClientData', (data) => listener(data));
}
pushToken(id, token) {
if (this.getSession(token)) {
this.sessions.forEach((session) => {
if (session.token === token) {
session.connections.push(id)
}
})
} else {
this.sessions.push({ token, connections: [id] })
}
}
getSession(token) {
return this.sessions.filter((session) => session.token === token)[0]
}
disconnectConnection(id, token) {
this.sessions.forEach((session, index) => {
if (session.token === token) {
session.connections.splice(session.connections.indexOf(id), 1)
if (session.connections.length == 0) {
this.sessions.splice(index, 1)
}
}
})
}
onConnect(id, token, callback) {
if (!this.getSession(token)) {
this.pushToken(id, token)
callback({ id, token })
} else {
this.pushToken(id, token)
}
}
onDisconnect(id, token, timeout, callback) {
setTimeout(() => {
this.disconnectConnection(id, token)
if (!this.getSession(token)) callback({ id, token })
}, timeout)
}
onLogout() {
this.sessions.forEach((session) => {
if (session.token === token) {
session.connections.forEach((connection) => {
io.to(connection).emit('logout')
})
}
})
}
addData(token, data) {
this.getSession(token).data = data
}
broadcastMessage(data) {
this.socketIO.sockets.emit('push_message', data)
}
pushMessage(token, data) {
let session = this.getSession(token);
if (session) {
let connections = session.connections
connections.forEach((connection) => {
this.socketIO.to(connection).emit('push_message', data)
})
}
return session != undefined
}
}
module.exports = new IOSessionHandler()