Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use WebSocket events instead of callbacks
  • Loading branch information
gizmotronic committed Jul 10, 2013
1 parent 927bb00 commit f012f86
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/bot.coffee
Expand Up @@ -91,9 +91,13 @@ class Bot
@whichServer roomId, (host, port) ->
url = "ws://#{host}:#{port}/socket.io/websocket"
@ws = new WebSocket(url)
@ws.onmessage = @onMessage.bind(@)
@ws.onerror = @disconnect.bind(@)
@ws.onclose = @onClose.bind(@)
@ws.on 'message', (data) =>
@onMessage data
.on 'wserror', (e) =>
@disconnect e
.on 'close', =>
@onClose()


whichServer: (roomid, callback) ->
options =
Expand Down Expand Up @@ -265,8 +269,7 @@ class Bot
@treatCommand(json)


onMessage: (msg) ->
data = msg.data
onMessage: (data) ->
@emit 'alive'
return @treatHeartbeat(data) if @isHeartbeat(data)
@log "> #{data}"
Expand Down Expand Up @@ -457,8 +460,9 @@ class Bot

roomRegister: (roomId, callback) ->
if @ws
@ws.onclose = ->
@ws.onerror = ->
@ws.removeAllListeners 'message'
@ws.removeAllListeners 'wserror'
@ws.removeAllListeners 'close'
@ws.close()
@callback = ->
rq = api: 'room.register', roomid: roomId
Expand Down

0 comments on commit f012f86

Please sign in to comment.