       @socket.onmessage = null
       @socket.close()
       delete @socket
+
+  _sendPing: =>
+    @socket.send JSON.stringify ping: 'PING'
+
+  _restartKeepAlive: =>
+    doPing = =>
+      console.log "Sending keepalive ping!"
+      @_sendPing()
+      setNextKeepAlive()
+    setNextKeepAlive = =>
+      @keepaliveTimeout = setTimeout doPing, KEEPALIVE_PING_TIMEOUT
+    if @keepaliveTimeout?
+      clearTimeout @keepaliveTimeout
+      @keepaliveTimeout = null
+    setNextKeepAlive()
+
+isPong = (o) ->
+  o.pong is 'PONG'


  constructor: (args) ->
    super args

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

# 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'
