-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.coffee
More file actions
55 lines (45 loc) · 2.08 KB
/
index.coffee
File metadata and controls
55 lines (45 loc) · 2.08 KB
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
ws = require 'ws'
somata = require 'somata'
{log} = somata
setup_ws = ({port}) ->
server = new ws.Server {port}
client = new somata.Client
server.on 'connection', (socket) ->
log.i "[ws.on connection] New connection"
subscriptions = {}
socket.id = somata.helpers.randomString()
socket.on 'message', (data) ->
message = JSON.parse data
switch message.kind
when 'remote'
{service, method, args, id} = message
console.log "[ws.on remote] <#{ socket.id }> #{ service } : #{ method }"
client.remote service, method, args..., (err, response) ->
socket.send JSON.stringify {kind: 'response', response, id}
when 'subscribe'
# Forward subscriptions by emitting events back over socket
{service, type} = message
console.log "[ws.on subscribe] <#{ socket.id }> #{ service } : #{ type }"
handler = client.on service, type, (event) ->
socket.send JSON.stringify {kind: 'event', service, type, event}
subscriptions[service] ||= {}
subscriptions[service][type] ||= []
subscriptions[service][type].push handler
when 'unsubscribe'
{service, type} = message
console.log '[ws.on unsubscribe]', service, type
subscriptions[service][type].map (sub_id) ->
client.unsubscribe sub_id
delete subscriptions[service][type]
# Unsubscribe from all of a socket's subscriptions
socket.on 'close', ->
console.log "[ws.on disconnect] <#{ socket.id }>"
for service, types of subscriptions
for type, subs of types
subs.map (sub_id) ->
client.unsubscribe sub_id
console.log "Listening on :#{port}"
if require.main == module
setup_ws {port: 5555}
else
module.exports = setup_ws