From 4eca4803a0096d59e8e16b1c00316535fb305af3 Mon Sep 17 00:00:00 2001 From: Thom McGrath Date: Thu, 27 Jun 2024 02:21:39 -0400 Subject: [PATCH] Rejoining Pusher channels after reconnect --- Project/Modules/Beacon/PusherSocket.xojo_code | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Project/Modules/Beacon/PusherSocket.xojo_code b/Project/Modules/Beacon/PusherSocket.xojo_code index 645e8d927..31f186a73 100644 --- a/Project/Modules/Beacon/PusherSocket.xojo_code +++ b/Project/Modules/Beacon/PusherSocket.xojo_code @@ -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 @@ -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 @@ -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 @@ -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