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
|
# File 'lib/moonrelay/server.rb', line 8
def self.run!
EM.run do
$clients = {}
$channels = {}
WebSocket::EventMachine::Server.start(host: "0.0.0.0", port: 8080) do |ws|
ws.onopen do |handshake|
$channels[handshake.path] ||= {}
$channels[handshake.path][ws.object_id] = ws
end
ws.onmessage do |msg, type|
handshake = ws.instance_eval("@handshake")
delivered = false
$channels[handshake.path].each do |object_id, subscriber|
unless subscriber == ws
delivered = true
p [type, msg]
subscriber.send msg, type: type
end
end
unless delivered
ws.close
end
end
ws.onclose do
handshake = ws.instance_eval("@handshake")
end
end
puts "🌗 listening at 0.0.0.0:8080"
end
end
|