Skip to content

Commit

Permalink
Rejoining Pusher channels after reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
thommcgrath committed Jun 27, 2024
1 parent 6c5bbc1 commit 4eca480
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion Project/Modules/Beacon/PusherSocket.xojo_code
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ Protected Class PusherSocket
End Sub
#tag EndMethod

#tag Method, Flags = &h0
Function IsSubscribed(Channel As String) As Boolean
Return Self.mChannels.IndexOf(Channel) > -1
End Function
#tag EndMethod

#tag Method, Flags = &h0
Sub Listen(ChannelName As String, EventName As String, Callback As Beacon.PusherSocket.EventHandler)
Var CallbackKey As String = ChannelName.Lowercase + "+" + EventName.Lowercase
Expand Down Expand Up @@ -249,6 +255,10 @@ Protected Class PusherSocket
Self.State = Beacon.PusherSocket.States.Disconnected

If ShouldReconnect Then
// Resubscribe to channels
For Each Channel As String In Self.mChannels
Self.mPendingMessages.Add(New Dictionary("event": "pusher:subscribe", "data": New Dictionary("channel": Channel)))
Next
GoTo ReconnectPoint
End If

Expand Down Expand Up @@ -357,13 +367,22 @@ Protected Class PusherSocket

#tag Method, Flags = &h0
Sub Subscribe(Channel As String)
If Self.IsSubscribed(Channel) Then
Return
End If

Self.mPendingMessages.Add(New Dictionary("event": "pusher:subscribe", "data": New Dictionary("channel": Channel)))
Self.mChannels.Add(Channel)
End Sub
#tag EndMethod

#tag Method, Flags = &h0
Sub Unsubscribe(Channel As String)
Self.mPendingMessages.Add(New Dictionary("event": "pusher:unsubscribe", "data": New Dictionary("channel": Channel)))
Var Idx As Integer = Self.mChannels.IndexOf(Channel)
If Idx > -1 Then
Self.mPendingMessages.Add(New Dictionary("event": "pusher:unsubscribe", "data": New Dictionary("channel": Channel)))
Self.mChannels.RemoveAt(Idx)
End If
End Sub
#tag EndMethod

Expand All @@ -378,6 +397,10 @@ Protected Class PusherSocket
Private mCallbacks As Dictionary
#tag EndProperty

#tag Property, Flags = &h21
Private mChannels() As String
#tag EndProperty

#tag Property, Flags = &h21
Private mCurl As CURLSMBS
#tag EndProperty
Expand Down

0 comments on commit 4eca480

Please sign in to comment.