-
Notifications
You must be signed in to change notification settings - Fork 185
/
WebChannels.jl
executable file
·275 lines (215 loc) · 7.6 KB
/
WebChannels.jl
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""
Handles WebSockets communication logic.
"""
module WebChannels
import HTTP, Distributed, Logging, JSON3
import Genie, Genie.Renderer
const ClientId = UInt # web socket hash
const ChannelName = String
struct ChannelNotFoundException <: Exception
name::ChannelName
end
mutable struct ChannelClient
client::HTTP.WebSockets.WebSocket
channels::Vector{ChannelName}
end
const ChannelClientsCollection = Dict{ClientId,ChannelClient} # { id(ws) => { :client => ws, :channels => ["foo", "bar", "baz"] } }
const ChannelSubscriptionsCollection = Dict{ChannelName,Vector{ClientId}} # { "foo" => ["4", "12"] }
const MessagePayload = Union{Nothing,Dict}
mutable struct ChannelMessage
channel::ChannelName
client::ClientId
message::String
payload::MessagePayload
end
function JSON3.StructTypes.StructType(::Type{T}) where {T<:ChannelMessage}
JSON3.StructTypes.Struct()
end
const CLIENTS = ChannelClientsCollection()
const SUBSCRIPTIONS = ChannelSubscriptionsCollection()
clients() = collect(values(CLIENTS))
subscriptions() = SUBSCRIPTIONS
websockets() = map(c -> c.client, clients())
channels() = collect(keys(SUBSCRIPTIONS))
function connected_clients(channel::ChannelName) :: Vector{ChannelClient}
clients = ChannelClient[]
for client_id in SUBSCRIPTIONS[channel]
! (CLIENTS[client_id].client.txclosed && CLIENTS[client_id].client.rxclosed) && push!(clients, CLIENTS[client_id])
end
clients
end
function connected_clients() :: Vector{ChannelClient}
clients = ChannelClient[]
for ch in channels()
clients = vcat(clients, connected_clients(ch))
end
clients
end
function disconnected_clients(channel::ChannelName) :: Vector{ChannelClient}
clients = ChannelClient[]
for client_id in SUBSCRIPTIONS[channel]
CLIENTS[client_id].client.txclosed && CLIENTS[client_id].client.rxclosed && push!(clients, CLIENTS[client_id])
end
clients
end
function disconnected_clients() :: Vector{ChannelClient}
clients = ChannelClient[]
for ch in channels()
clients = vcat(clients, disconnected_clients(ch))
end
clients
end
"""
Subscribes a web socket client `ws` to `channel`.
"""
function subscribe(ws::HTTP.WebSockets.WebSocket, channel::ChannelName) :: ChannelClientsCollection
if haskey(CLIENTS, id(ws))
in(channel, CLIENTS[id(ws)].channels) || push!(CLIENTS[id(ws)].channels, channel)
else
CLIENTS[id(ws)] = ChannelClient(ws, ChannelName[channel])
end
push_subscription(id(ws), channel)
CLIENTS
end
function id(ws::HTTP.WebSockets.WebSocket) :: UInt
hash(ws)
end
"""
Unsubscribes a web socket client `ws` from `channel`.
"""
function unsubscribe(ws::HTTP.WebSockets.WebSocket, channel::ChannelName) :: ChannelClientsCollection
haskey(CLIENTS, id(ws)) && deleteat!(CLIENTS[id(ws)].channels, CLIENTS[id(ws)].channels .== channel)
pop_subscription(id(ws), channel)
CLIENTS
end
function unsubscribe(channel_client::ChannelClient, channel::ChannelName) :: ChannelClientsCollection
unsubscribe(channel_client.client, channel)
end
"""
Unsubscribes a web socket client `ws` from all the channels.
"""
function unsubscribe_client(ws::HTTP.WebSockets.WebSocket) :: ChannelClientsCollection
if haskey(CLIENTS, id(ws))
for channel_id in CLIENTS[id(ws)].channels
pop_subscription(id(ws), channel_id)
end
delete!(CLIENTS, id(ws))
end
CLIENTS
end
function unsubscribe_client(client_id::ClientId) :: ChannelClientsCollection
unsubscribe_client(CLIENTS[client_id].client)
CLIENTS
end
function unsubscribe_client(channel_client::ChannelClient) :: ChannelClientsCollection
unsubscribe_client(channel_client.client)
CLIENTS
end
"""
unsubscribe_disconnected_clients() :: ChannelClientsCollection
Unsubscribes clients which are no longer connected.
"""
function unsubscribe_disconnected_clients() :: ChannelClientsCollection
for channel_client in disconnected_clients()
unsubscribe_client(channel_client)
end
CLIENTS
end
function unsubscribe_disconnected_clients(channel::ChannelName) :: ChannelClientsCollection
for channel_client in disconnected_clients(channel)
unsubscribe(channel_client, channel)
end
CLIENTS
end
"""
Adds a new subscription for `client` to `channel`.
"""
function push_subscription(client_id::ClientId, channel::ChannelName) :: ChannelSubscriptionsCollection
if haskey(SUBSCRIPTIONS, channel)
! in(client_id, SUBSCRIPTIONS[channel]) && push!(SUBSCRIPTIONS[channel], client_id)
else
SUBSCRIPTIONS[channel] = ClientId[client_id]
end
SUBSCRIPTIONS
end
function push_subscription(channel_client::ChannelClient, channel::ChannelName) :: ChannelSubscriptionsCollection
push_subscription(id(channel_client.client), channel)
end
"""
Removes the subscription of `client` to `channel`.
"""
function pop_subscription(client::ClientId, channel::ChannelName) :: ChannelSubscriptionsCollection
if haskey(SUBSCRIPTIONS, channel)
filter!(SUBSCRIPTIONS[channel]) do (client_id)
client_id != client
end
isempty(SUBSCRIPTIONS[channel]) && delete!(SUBSCRIPTIONS, channel)
end
SUBSCRIPTIONS
end
function pop_subscription(channel_client::ChannelClient, channel::ChannelName) :: ChannelSubscriptionsCollection
pop_subscription(id(channel_client.client), channel)
end
"""
Removes all subscriptions of `client`.
"""
function pop_subscription(channel::ChannelName) :: ChannelSubscriptionsCollection
if haskey(SUBSCRIPTIONS, channel)
delete!(SUBSCRIPTIONS, channel)
end
SUBSCRIPTIONS
end
"""
Pushes `msg` (and `payload`) to all the clients subscribed to the channels in `channels`, with the exception of `except`.
"""
function broadcast(channels::Union{ChannelName,Vector{ChannelName}},
msg::String,
payload::Union{Dict,Nothing} = nothing;
except::Union{HTTP.WebSockets.WebSocket,Nothing,UInt} = nothing) :: Bool
isa(channels, Array) || (channels = ChannelName[channels])
for channel in channels
haskey(SUBSCRIPTIONS, channel) || throw(ChannelNotFoundException(channel))
for client in SUBSCRIPTIONS[channel]
except !== nothing && client == id(except) && continue
CLIENTS[client].client.txclosed && CLIENTS[client].client.rxclosed && continue
try
payload !== nothing ?
message(client, ChannelMessage(channel, client, msg, payload) |> Renderer.Json.JSONParser.json) :
message(client, msg)
catch
end
end
end
true
end
"""
Pushes `msg` (and `payload`) to all the clients subscribed to the channels in `channels`, with the exception of `except`.
"""
function broadcast(msg::String;
channels::Union{Union{ChannelName,Vector{ChannelName}},Nothing} = nothing,
payload::Union{Dict,Nothing} = nothing,
except::Union{HTTP.WebSockets.WebSocket,Nothing,UInt} = nothing) :: Bool
channels === nothing && (channels = collect(keys(SUBSCRIPTIONS)))
broadcast(channels, msg, payload; except = except)
end
"""
Pushes `js_code` (a JavaScript piece of code) to be executed by all the clients subscribed to the channels in `channels`,
with the exception of `except`.
"""
function jscomm(js_code::String, channels::Union{Union{ChannelName,Vector{ChannelName}},Nothing} = nothing;
except::Union{HTTP.WebSockets.WebSocket,Nothing,UInt} = nothing)
broadcast(string(Genie.config.webchannels_eval_command, js_code); channels = channels, except = except)
end
"""
Writes `msg` to web socket for `client`.
"""
function message(ws::HTTP.WebSockets.WebSocket, msg::String) :: Int
write(ws, msg)
end
function message(client::ClientId, msg::String) :: Int
message(CLIENTS[client].client, msg)
end
function message(client::ChannelClient, msg::String) :: Int
message(client.client, msg)
end
end