Minor code style fixes
parent
13fde387ec
commit
35e1e41d3a
|
@ -1,3 +1,4 @@
|
|||
*.swp
|
||||
node_modules/
|
||||
npm-debug.log
|
||||
config.json
|
||||
|
|
|
@ -4,47 +4,49 @@ SubscriptionManager = require('./subscription').SubscriptionManager
|
|||
|
||||
clients = {}
|
||||
|
||||
|
||||
class Client
|
||||
constructor: (@ws) ->
|
||||
@id = uuid.v4()
|
||||
@.id = uuid.v4()
|
||||
|
||||
@handleEvents()
|
||||
@.handleEvents()
|
||||
|
||||
handleEvents: () ->
|
||||
@ws.on 'message', @handleMessage.bind(@)
|
||||
@ws.on 'message', @.handleMessage.bind(@)
|
||||
|
||||
handleMessage: (message) ->
|
||||
msg = JSON.parse(message)
|
||||
|
||||
if msg.cmd == 'auth'
|
||||
@auth(msg.data)
|
||||
@.auth(msg.data)
|
||||
else if msg.cmd == 'subscribe'
|
||||
@addSubscription(msg.routing_key)
|
||||
@.addSubscription(msg.routing_key)
|
||||
else if msg.cmd == 'unsubscribe'
|
||||
@removeSubscription(msg.routing_key)
|
||||
@.removeSubscription(msg.routing_key)
|
||||
|
||||
auth: (auth) ->
|
||||
if auth.token and auth.sessionId and signing.verify(auth.token)
|
||||
@auth = auth
|
||||
@.auth = auth
|
||||
|
||||
addSubscription: (routing_key) ->
|
||||
if @auth
|
||||
if !@subscriptionManager
|
||||
@subscriptionManager = new SubscriptionManager(@id, @auth, @ws)
|
||||
@subscriptionManager.add(routing_key)
|
||||
if @.auth
|
||||
if !@.subscriptionManager
|
||||
@.subscriptionManager = new SubscriptionManager(@.id, @.auth, @ws)
|
||||
@.subscriptionManager.add(routing_key)
|
||||
|
||||
close: () ->
|
||||
if @subscriptionManager
|
||||
@subscriptionManager.destroy()
|
||||
if @.subscriptionManager
|
||||
@.subscriptionManager.destroy()
|
||||
|
||||
removeSubscription: (routing_key) ->
|
||||
if @subscriptionManager
|
||||
@subscriptionManager.remove(routing_key)
|
||||
if @.subscriptionManager
|
||||
@.subscriptionManager.remove(routing_key)
|
||||
|
||||
|
||||
exports.createClient = (ws) ->
|
||||
client = new Client(ws)
|
||||
clients[client.id] = client
|
||||
client.ws.on 'close', (() ->
|
||||
@.close()
|
||||
delete clients[@id]
|
||||
delete clients[@.id]
|
||||
).bind(client)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
queue = require('./rabbit')
|
||||
|
||||
|
||||
class Subscription
|
||||
constructor: (@client_id, @auth, @ws, @routing_key) ->
|
||||
|
||||
|
@ -16,31 +17,32 @@ class Subscription
|
|||
@ws.send clientMsgStr
|
||||
|
||||
start: () ->
|
||||
queue.subscribe(@client_id, @routing_key, @handleMessage.bind(@))
|
||||
queue.subscribe(@client_id, @routing_key, @.handleMessage.bind(@))
|
||||
|
||||
stop: () ->
|
||||
queue.unsubscribe(@client_id, @routing_key)
|
||||
|
||||
|
||||
class SubscriptionManager
|
||||
constructor: (@client_id, @auth, @ws) ->
|
||||
@subscriptions = {}
|
||||
@.subscriptions = {}
|
||||
|
||||
add: (routing_key) ->
|
||||
if !@subscriptions[routing_key]
|
||||
@subscriptions[routing_key] = {}
|
||||
if !@.subscriptions[routing_key]
|
||||
@.subscriptions[routing_key] = {}
|
||||
else
|
||||
@subscriptions[routing_key].stop()
|
||||
@.subscriptions[routing_key].stop()
|
||||
|
||||
@subscriptions[routing_key] = new Subscription(@client_id, @auth, @ws, routing_key)
|
||||
@subscriptions[routing_key].start()
|
||||
@.subscriptions[routing_key] = new Subscription(@client_id, @auth, @ws, routing_key)
|
||||
@.subscriptions[routing_key].start()
|
||||
|
||||
remove: (routing_key) ->
|
||||
@subscriptions[routing_key].stop()
|
||||
@.subscriptions[routing_key].stop()
|
||||
|
||||
delete @subscriptions[routing_key]
|
||||
delete @.subscriptions[routing_key]
|
||||
|
||||
destroy: () ->
|
||||
@subscriptions = {}
|
||||
@.subscriptions = {}
|
||||
queue.destroy(@client_id)
|
||||
|
||||
exports.SubscriptionManager = SubscriptionManager
|
||||
|
|
Loading…
Reference in New Issue