myxi 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f04936ddd50485e99372aa1248f99bf41fbc6c61
4
- data.tar.gz: 16b9d860964f5f8c733697cc13001977949ca541
3
+ metadata.gz: a50043a96e96ff44cef317e03314866dc254a39c
4
+ data.tar.gz: e2a1f27a1bc4a5b839d22ecc64a1bbedb78ddef8
5
5
  SHA512:
6
- metadata.gz: 0aabc52d678b70c8fc7d929bb00d9b876b5fc8a7c847cc9d7f8be0f3350a21579f7cb309cc88d1b25f9f57dc6f2effe74de9e59278ae97143a7932822ae265f8
7
- data.tar.gz: 4e9b22ef5ba86dc807a8f8240a510c40ae2602fde0f47f9cc177e9a1857724b0114ab0fc2e21395d87c6595b483718dca8bd9a2e05b90924198f58f7ab63b5d7
6
+ metadata.gz: b5befc8b9663554974828df4676d57a4f92faacba6038d50449e5577dce6ea25eba1ea5778a42561c86e87d3e89b7e6a3e137e1592f35ce4f97c6d9257562071
7
+ data.tar.gz: e5be4621e3703edb1ae32f5bd4bc1910a37c135573761d63afb11110085e80d72a38af250dc5d9b4c4945650d2f257bad36fd4b1e8e76cfdb29dbdc0dfe4b8f7
@@ -1,3 +1,3 @@
1
1
  module Myxi
2
- VERSION = '1.1.0'
2
+ VERSION = '1.1.1'
3
3
  end
@@ -0,0 +1,2 @@
1
+ ##= require myxi/connection
2
+ ##= require myxi/subscription
@@ -0,0 +1,112 @@
1
+ window.Myxi ||= {}
2
+ class Myxi.Connection
3
+
4
+ constructor: (serverURL)->
5
+ @serverURL = serverURL
6
+ @connected = false
7
+ @authenticated = false
8
+ @subscriptions = {}
9
+ @callbacks = {}
10
+ @reconnect = true
11
+ @reconnectTimer = null
12
+ @authentication_callback = null
13
+ @connect()
14
+
15
+ connect: ->
16
+ clearTimeout(@reconnectTimer) if @reconnectTimer?
17
+ @websocket = new WebSocket(@serverURL)
18
+ @websocket.onopen = (event)=>
19
+ console.log "Connected to socket server at #{@serverURL}"
20
+ if @connected == false
21
+ @_runCallbacks('SocketConnected')
22
+ @connected = true
23
+ if @authentication_callback == null
24
+ @_subscribeAllUnsubscribedSubscriptions()
25
+ else
26
+ @authentication_callback.call(this)
27
+
28
+ @websocket.onmessage = (event)=>
29
+ data = JSON.parse(event.data)
30
+ if data['event'] == 'Subscribed'
31
+ @_markSubscriptionAsSubscribed(data['payload']['exchange'], data['payload']['routing_key'])
32
+ else if data['event'] == 'Unsubscribed'
33
+ @_removeSubscription(data['payload']['exchange_name'], data['payload']['routing_key'])
34
+ else if data['event'] == 'Error' && data['payload']['error'] == 'SubscriptionDenied'
35
+ @_removeSubscription(data['payload']['exchange'], data['payload']['routing_key'])
36
+ else
37
+ if data['mq'] && subscription = @subscriptions[Myxi.Subscription.keyFor(data['mq']['e'], data['mq']['rk'])]
38
+ subscription._receiveMessage(data['event'], data['payload'], data['tag'])
39
+ @_runCallbacks(data['event'], data['payload'], data['tag'], data['mq'])
40
+
41
+ @_runCallbacks('SocketMessageReceived', data)
42
+
43
+ @websocket.onclose = (event)=>
44
+ if @connected
45
+ @_runCallbacks('SocketDisconnected')
46
+ @connected = false
47
+ @authenticated = false
48
+ @_markAllSubscriptionsAsUnsubscribed()
49
+ console.log "Server disconnected."
50
+ if @reconnect
51
+ @reconnectTimer = setTimeout =>
52
+ @connect()
53
+ , 5000
54
+
55
+ true
56
+
57
+ disconnect: ->
58
+ @reconnect = false
59
+ clearTimeout(@reconnectTimer) if @reconnectTimer
60
+ @websocket.close() if @websocket
61
+
62
+ authentication: (callback)->
63
+ @authentication_callback = callback
64
+
65
+ isAuthenticated: ->
66
+ @authenticated = true
67
+ @_runCallbacks('SocketAuthenticated')
68
+ @_subscribeAllUnsubscribedSubscriptions()
69
+
70
+ sendAction: (action, payload, tag)->
71
+ if @connected
72
+ packet = {'action': action, 'tag': tag, 'payload': payload}
73
+ @websocket.send(JSON.stringify(packet))
74
+ true
75
+ else
76
+ false
77
+
78
+ on: (event, callback)->
79
+ @callbacks[event] ||= []
80
+ @callbacks[event].push(callback)
81
+
82
+ _runCallbacks: (event, payload, tag, mq)->
83
+ if callbacks = @callbacks[event]
84
+ for callback in callbacks
85
+ callback.call(this, payload, tag, mq)
86
+
87
+ subscribe: (exchange, routingKey)->
88
+ if existingSubscription = @subscriptions[Myxi.Subscription.keyFor(exchange, routingKey)]
89
+ existingSubscription
90
+ else
91
+ subscription = new Myxi.Subscription(this, exchange, routingKey)
92
+ @subscriptions[subscription.key()] = subscription
93
+ subscription
94
+
95
+ _markSubscriptionAsSubscribed: (exchange, routingKey)->
96
+ if subscription = @subscriptions[Myxi.Subscription.keyFor(exchange, routingKey)]
97
+ subscription._isSubscribed()
98
+
99
+ _removeSubscription: (exchange, routingKey)->
100
+ key = Myxi.Subscription.keyFor(exchange, routingKey)
101
+ if subscription = @subscriptions[key]
102
+ subscription._isUnsubscribed()
103
+ delete @subscriptions[key]
104
+
105
+ _markAllSubscriptionsAsUnsubscribed: ()->
106
+ for id, subscription of @subscriptions
107
+ subscription._isUnsubscribed()
108
+
109
+ _subscribeAllUnsubscribedSubscriptions: ()->
110
+ for id, subscription of @subscriptions
111
+ if subscription.subscribed == false && subscription.reconnect == true
112
+ subscription.subscribe()
@@ -0,0 +1,61 @@
1
+ window.Myxi ||= {}
2
+ class Myxi.Subscription
3
+
4
+ @keyFor: (exchange, routingKey)->
5
+ key = exchange
6
+ if routingKey?
7
+ key = "#{key}::#{routingKey}"
8
+ key
9
+
10
+ constructor: (connection, exchange, routingKey)->
11
+ @connection = connection
12
+ @exchange = exchange
13
+ @routingKey = routingKey
14
+ @subscribed = false
15
+ @reconnect = true
16
+ @callbacks = {}
17
+ @messageHandlers = {}
18
+ @subscribe() if @connection.connected
19
+
20
+ subscribe: ()->
21
+ @connection.sendAction('Subscribe', {'exchange': @exchange, 'routing_key': @routingKey})
22
+
23
+ key: ()->
24
+ Myxi.Subscription.keyFor(@exchange, @routingKey)
25
+
26
+ unsubscribe: ()->
27
+ if @connection.sendAction('Unsubscribe', {'exchange': @exchange, 'routing_key': @routingKey})
28
+ @subscribed = false
29
+ @reconnect = false
30
+ true
31
+ else
32
+ false
33
+
34
+ addMessageHandler: (name, handler, param)->
35
+ @messageHandlers[name] = {handler: handler, param: param}
36
+
37
+ removeMessageHandler: (name)->
38
+ if @messageHandlers[name]
39
+ delete @messageHandlers[name]
40
+ true
41
+ else
42
+ false
43
+
44
+ on: (event, callback)->
45
+ @callbacks[event] ||= []
46
+ @callbacks[event].push(callback)
47
+
48
+ _isSubscribed: ()->
49
+ console.log "Subscribed to #{@key()}"
50
+ @subscribed = true
51
+
52
+ _isUnsubscribed: ()->
53
+ console.log "Unsubscribed from #{@key()}"
54
+ @subscribed = false
55
+
56
+ _receiveMessage: (event, payload, tag)->
57
+ if callbacks = @callbacks[event]
58
+ for callback in callbacks
59
+ callback.call(this, payload, tag)
60
+ for handler, opts of @messageHandlers
61
+ opts.handler.call(opts.param, this, event, payload, tag)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: myxi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
@@ -66,6 +66,9 @@ files:
66
66
  - lib/myxi/server.rb
67
67
  - lib/myxi/session.rb
68
68
  - lib/myxi/version.rb
69
+ - vendor/assets/javascripts/myxi.coffee
70
+ - vendor/assets/javascripts/myxi/connection.coffee
71
+ - vendor/assets/javascripts/myxi/subscription.coffee
69
72
  homepage: https://github.com/adamcooke/myxi
70
73
  licenses:
71
74
  - MIT