 WebSocket   = require 'ws'
 Log            = require 'log'
 {EventEmitter} = require 'events'
+pingInterval = 60000
 
 User = require './user.coffee'
 

            @emit 'error', error

        @ws.on 'open', =>
            @_connAttempts = 0
            @_lastPong = Date.now()
            @_pongTimeout = setInterval =>
                if not @connected then return

                @logger.debug 'ping'
                @_send {"type": "ping"}
                if @_lastPong? and Date.now() - @_lastPong > 10000
                    @logger.error "Last pong is too old: %d", (Date.now() - @_lastPong) / 1000
                    @authenticated = false
                    @connected = false
                    @reconnect()
            , 5000

        @ws.on 'message', (data, flags) =>
            @onMessage JSON.parse(data)

                @authenticated = true
                # Continue happy flow here
                @token = headers.token
                @socketUrl = 'wss://' + @host + (if @options.wssPort? then ':'+ @options.wssPort else ':443') + '/api/v1/websocket'
                @logger.info 'Websocket URL: ' + @socketUrl
                @self = new User @, data
                @emit 'loggedIn', @self
