1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add client-side console logging to help debug reconnect issues

This commit is contained in:
Javan Makhmali 2016-02-17 16:07:21 -05:00 committed by Pratik Naik
parent c57e7239a8
commit 832f88bdf3
2 changed files with 15 additions and 1 deletions

View file

@ -17,8 +17,10 @@ class ActionCable.Connection
open: =>
if @webSocket and not @isState("closed")
console.log("[cable] Attemped to open WebSocket, but existing socket is #{@getState()}", Date.now())
throw new Error("Existing connection must be closed before opening")
else
console.log("[cable] Opening WebSocket", Date.now())
@webSocket = new WebSocket(@consumer.url)
@installEventHandlers()
true
@ -27,12 +29,14 @@ class ActionCable.Connection
@webSocket?.close()
reopen: ->
console.log("[cable] Reopening WebSocket, current state is #{@getState()}", Date.now())
if @isState("closed")
@open()
else
try
@close()
finally
console.log("[cable] Failed to reopen WebSocket, retrying in #{@constructor.reopenDelay}ms", Date.now())
setTimeout(@open, @constructor.reopenDelay)
isOpen: ->
@ -66,13 +70,16 @@ class ActionCable.Connection
@consumer.subscriptions.notify(identifier, "received", message)
open: ->
console.log("[cable] WebSocket onopen event", Date.now())
@disconnected = false
@consumer.subscriptions.reload()
close: ->
console.log("[cable] WebSocket onclose event", Date.now())
@disconnect()
error: ->
console.log("[cable] WebSocket onerror event", Date.now())
@disconnect()
disconnect: ->

View file

@ -33,10 +33,12 @@ class ActionCable.ConnectionMonitor
@startedAt = now()
@poll()
document.addEventListener("visibilitychange", @visibilityDidChange)
console.log("[cable] ConnectionMonitor started, pollInterval is #{@getInterval()}ms", Date.now())
stop: ->
@stoppedAt = now()
document.removeEventListener("visibilitychange", @visibilityDidChange)
console.log("[cable] ConnectionMonitor stopped", Date.now())
poll: ->
setTimeout =>
@ -52,8 +54,12 @@ class ActionCable.ConnectionMonitor
reconnectIfStale: ->
if @connectionIsStale()
console.log("[cable] ConnectionMonitor detected stale connection, reconnectAttempts = #{@reconnectAttempts}", Date.now())
@reconnectAttempts++
unless @disconnectedRecently()
if @disconnectedRecently()
console.log("[cable] ConnectionMonitor skipping repopen because recently disconnected at #{@disconnectedAt}", Date.now())
else
console.log("[cable] ConnectionMonitor reopening", Date.now())
@consumer.connection.reopen()
connectionIsStale: ->
@ -66,6 +72,7 @@ class ActionCable.ConnectionMonitor
if document.visibilityState is "visible"
setTimeout =>
if @connectionIsStale() or not @consumer.connection.isOpen()
console.log("[cable] ConnectionMonitor reopening stale connection after visibilitychange", Date.now())
@consumer.connection.reopen()
, 200