 {client: nodeWs, w3cwebsocket: browserWs} = require 'websocket'
 
 class BaseWS extends EventEmitter
-  constructor: (url, headers, timeout) ->
+  constructor: (@url, @headers, timeout) ->
     super()
-    @headers = headers
     @config =
       closeTimeout: timeout
       keepaliveInterval: 15000


    @socket.onopen =>
      @connected = true
      @emit 'open'

    @socket.onmessage (message) =>
      @emit 'message', message

    @socket.onclose =>
      @emit 'close'

    @socket.onerror (error) =>
      if @connected
        @emit 'error', error
      else
        @emit 'connectFailed', error

  # Close the WebSocket if it exists
  close: ->
    if @socket?
      @socket.close()
    @socket = null

  ping: ->
    @socket.ping() if @socket?

  send: (data) ->
    new P((resolve, reject) ->
      if @socket?
        @socket.send data, (error) ->
          if error?

            resolve()
      else
        reject new Error("Not connected.")


# Browser WebSocket
