UNPKG

1.23 kBtext/coffeescriptView Raw
1class SocketIORemoteEndpoint
2
3 initialize: (options = {}, callback = ->) ->
4 if options.ioInstance
5 @_io = options.ioInstance
6 else
7 @_io = require('socket.io')()
8 @_io.listen 3000
9
10 @_io.sockets.on 'connection', (socket) =>
11 socket.on 'RPC_Request', (RPC_Request) =>
12 @_handleRPCRequest RPC_Request, (err, response) =>
13 rpcId = RPC_Request.rpcId
14 socket.emit 'RPC_Response',
15 rpcId: rpcId
16 err: err
17 data: response
18
19
20 socket.on 'JoinRoom', (roomName) ->
21 socket.join roomName
22
23
24 socket.on 'LeaveRoom', (roomName) ->
25 socket.leave roomName
26
27
28 callback()
29
30
31 close: ->
32 @_io.close()
33
34
35 setRPCHandler: (@_handleRPCRequest) ->
36
37
38 publish: (context, [domainEventName, aggregateId]..., payload) ->
39 fullEventName = @_getFullEventName context, domainEventName, aggregateId
40 @_io.to(fullEventName).emit fullEventName, payload
41
42
43 _getFullEventName: (context, domainEventName, aggregateId) ->
44 fullEventName = context
45 if domainEventName
46 fullEventName += "/#{domainEventName}"
47 if aggregateId
48 fullEventName += "/#{aggregateId}"
49 fullEventName
50
51
52module.exports = new SocketIORemoteEndpoint
\No newline at end of file