 # Unfortunately this needs to a global variable. The only other option is to
 # hack into the internals of the web_socket.js plugin we are using.
 window.WEB_SOCKET_SWF_LOCATION = '/assets/firehose/WebSocketMain.swf' if !window.WEB_SOCKET_SWF_LOCATION
+
+INITIAL_PING_TIMEOUT      =  2000
+KEEPALIVE_PING_TIMEOUT = 20000
 
 class Firehose.WebSocket extends Firehose.Transport
   name: -> 'WebSocket'


  constructor: (args) ->
    super args

    # Configrations specifically for web sockets
    @config.webSocket ||= {}
    # Protocol schema we should use for talking to WS server.

    @socket.onmessage = @_waitForPong

  stop: =>
    @_cleanUp()

  _open: =>
    @_sendPing()
    # TODO: consider making this timeout configurable somehow...
    @pingTimeout = setTimeout @_error, INITIAL_PING_TIMEOUT

  _waitForPong: (event) =>
    if isPong(try JSON.parse event.data catch e then {})
      @_succeeded = true
      @config.connected @
      clearTimeout @pingTimeout
      @socket.onmessage = @_message
      @_restartKeepAlive()
    else @_message event

  _message: (event) =>
    @_restartKeepAlive()
    msg = @config.parse event.data
    @config.message msg if not isPong msg

  _close: (event) =>
    if event?.wasClean then @_cleanUp()
    else @_error event

  _error: (event) =>
    @_cleanUp()
    super

  _cleanUp: =>
    if @socket?
      @socket.onopen    = null
      @socket.onclose   = null
