     @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 =

    @treatCommand(json)


  onMessage: (msg) ->
    data = msg.data
    @emit 'alive'
    return @treatHeartbeat(data) if @isHeartbeat(data)
    @log "> #{data}"


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