forked from soyuka/signalhubws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (139 loc) · 3.4 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const events = require('events')
const through2 = require('through2')
const inherits = require('inherits')
function SignalhubWs (app, urls) {
this.opened = false
this.sockets = []
this.app = app
const channels = this.channels = new Map()
this.subscribers = {
get length () {
return channels.size
}
}
if (!Array.isArray(urls)) {
urls = [urls]
}
urls = urls.map(function (url) {
url = url.replace(/\/$/, '')
return url.indexOf('://') === -1 ? 'ws://' + url : url
})
let countOpen = 0
for (let index = 0; index < urls.length; index++) {
const socket = new window.WebSocket(`${urls[index]}/${app}`)
this.sockets.push(socket)
socket.addEventListener('open', () => {
if (++countOpen === urls.length) {
this.opened = true
this.emit('open')
for (let channel of this.channels.values()) {
channel.emit('open')
}
}
})
socket.addEventListener('message', (message) => {
this.onMessage(message)
})
}
}
inherits(SignalhubWs, events.EventEmitter)
SignalhubWs.prototype.subscribe = function (channel) {
if (this.closed) {
throw new Error('Cannot subscribe after close')
}
if (this.channels.has(channel)) {
return this.channels.get(channel)
}
// use a stream for channel
this.channels.set(channel, through2.obj())
this.channels.get(channel).on('close', function () {
this.channels.remove(channel)
})
if (this.opened) {
process.nextTick(() => {
if (this.channels.has(channel)) {
this.channels.get(channel).emit('open')
}
})
}
return this.channels.get(channel)
}
SignalhubWs.prototype.broadcast = function (channel, message, cb) {
if (this.closed) {
throw new Error('Cannot broadcast after close')
}
const data = {
app: this.app,
channel: channel,
message: message
}
this.sockets.forEach((socket) => {
socket.send(JSON.stringify(data))
})
cb && cb()
}
SignalhubWs.prototype.onMessage = function (message) {
message = JSON.parse(message.data)
for (let key of this.channels.keys()) {
if (message.channel === key) {
this.channels.get(key).write(message.message)
continue
}
if (!Array.isArray(key)) {
continue
}
for (let i = 0; i < key.length; i++) {
if (key[i] === message.channel) {
this.channels.get(key).write(message.message)
}
}
}
}
SignalhubWs.prototype.close = function (cb) {
if (this.closed) return
this.once('close:socket', () => {
this._closeChannels(cb)
})
const len = this.sockets.length
if (len === 0) {
this.emit('close')
return
}
let closed = 0
this.sockets.forEach((socket) => {
socket.addEventListener('close', () => {
if (++closed === len) {
this.emit('close:socket')
}
})
process.nextTick(function () {
socket.close()
})
})
}
SignalhubWs.prototype._closeChannels = function (cb) {
if (this.closed) return
this.closed = true
if (cb) {
this.on('close', cb)
}
const len = this.channels.size
if (len === 0) {
this.emit('close')
return
}
let closed = 0
for (let channel of this.channels.values()) {
process.nextTick(() => {
channel.end(() => {
if (++closed === len) {
this.channels.clear()
this.emit('close')
}
})
})
}
}
module.exports = function (app, urls) {
return new SignalhubWs(app, urls)
}