1 |
|
2 | (function() {
|
3 | var SocketIORemoteEndpoint,
|
4 | __slice = [].slice;
|
5 |
|
6 | SocketIORemoteEndpoint = (function() {
|
7 | function SocketIORemoteEndpoint() {}
|
8 |
|
9 | SocketIORemoteEndpoint.prototype.initialize = function(options, callback) {
|
10 | if (options == null) {
|
11 | options = {};
|
12 | }
|
13 | if (callback == null) {
|
14 | callback = function() {};
|
15 | }
|
16 | if (options.ioInstance) {
|
17 | this._io = options.ioInstance;
|
18 | } else {
|
19 | this._io = require('socket.io')();
|
20 | this._io.listen(3000);
|
21 | }
|
22 | this._io.sockets.on('connection', (function(_this) {
|
23 | return function(socket) {
|
24 | socket.on('RPC_Request', function(RPC_Request) {
|
25 | return _this._handleRPCRequest(RPC_Request, function(err, response) {
|
26 | var rpcId;
|
27 | rpcId = RPC_Request.rpcId;
|
28 | return socket.emit('RPC_Response', {
|
29 | rpcId: rpcId,
|
30 | err: err,
|
31 | data: response
|
32 | });
|
33 | });
|
34 | });
|
35 | socket.on('JoinRoom', function(roomName) {
|
36 | return socket.join(roomName);
|
37 | });
|
38 | return socket.on('LeaveRoom', function(roomName) {
|
39 | return socket.leave(roomName);
|
40 | });
|
41 | };
|
42 | })(this));
|
43 | return callback();
|
44 | };
|
45 |
|
46 | SocketIORemoteEndpoint.prototype.close = function() {
|
47 | return this._io.engine.close();
|
48 | };
|
49 |
|
50 | SocketIORemoteEndpoint.prototype.setRPCHandler = function(_handleRPCRequest) {
|
51 | this._handleRPCRequest = _handleRPCRequest;
|
52 | };
|
53 |
|
54 | SocketIORemoteEndpoint.prototype.publish = function() {
|
55 | var aggregateId, context, domainEventName, fullEventName, payload, _arg, _i;
|
56 | context = arguments[0], _arg = 3 <= arguments.length ? __slice.call(arguments, 1, _i = arguments.length - 1) : (_i = 1, []), payload = arguments[_i++];
|
57 | domainEventName = _arg[0], aggregateId = _arg[1];
|
58 | fullEventName = this._getFullEventName(context, domainEventName, aggregateId);
|
59 | return this._io.to(fullEventName).emit(fullEventName, payload);
|
60 | };
|
61 |
|
62 | SocketIORemoteEndpoint.prototype._getFullEventName = function(context, domainEventName, aggregateId) {
|
63 | var fullEventName;
|
64 | fullEventName = context;
|
65 | if (domainEventName) {
|
66 | fullEventName += "/" + domainEventName;
|
67 | }
|
68 | if (aggregateId) {
|
69 | fullEventName += "/" + aggregateId;
|
70 | }
|
71 | return fullEventName;
|
72 | };
|
73 |
|
74 | return SocketIORemoteEndpoint;
|
75 |
|
76 | })();
|
77 |
|
78 | module.exports = new SocketIORemoteEndpoint;
|
79 |
|
80 | }).call(this);
|