Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solves reconnect bug in trigger old events and channel resubscription. #353

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions lib/assets/javascripts/websocket_rails/websocket_rails.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class @WebSocketRails
@callbacks = {}
@channels = {}
@queue = {}
@reconnect_event = status: false, old_connection_id: null

@connect()

Expand All @@ -45,25 +46,19 @@ class @WebSocketRails

@state = 'disconnected'

# Reconnects the whole connection,
# Reconnects the whole connection,
# keeping the messages queue and its' connected channels.
#
#
# After successfull connection, this will:
# - reconnect to all channels, that were active while disconnecting
# - resend all events from which we haven't received any response yet
reconnect: =>
old_connection_id = @_conn?.connection_id

unless @reconnect_event.status
@reconnect_event.status = true
@reconnect_event.old_connection_id = @_conn?.connection_id
@disconnect()
@connect()

# Resend all unfinished events from the previous connection.
for id, event of @queue
if event.connection_id == old_connection_id && !event.is_result()
@trigger_event event

@reconnect_channels()

new_message: (data) =>
for socket_message in data
event = new WebSocketRails.Event( socket_message )
Expand All @@ -78,6 +73,8 @@ class @WebSocketRails
@dispatch event

if @state == 'connecting' and event.name == 'client_connected'
if @reconnect_event.status
@process_reconnect()
@connection_established event.data

connection_established: (data) =>
Expand Down Expand Up @@ -143,6 +140,15 @@ class @WebSocketRails
connection_stale: =>
@state != 'connected'

process_reconnect: ->
old_connection_id = @reconnect_event.old_connection_id
@reconnect_event.status = false
@reconnect_event.old_connection_id = null
for id, event of @queue
if event.connection_id == old_connection_id && !event.is_result()
@trigger_event event
@reconnect_channels()

# Destroy and resubscribe to all existing @channels.
reconnect_channels: ->
for name, channel of @channels
Expand Down