Skip to content

Commit

Permalink
dispatch reconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Nov 28, 2024
1 parent 73e255a commit ac34b47
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
8 changes: 4 additions & 4 deletions Sources/Realtime/V2/RealtimeChannelV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ public actor RealtimeChannelV2 {
}
} catch {
if error is TimeoutError {
logger?.debug("Subscribe timed out.")
logger?.debug("channel timeout \(topic)")
await subscribe()
} else {
logger?.error("Subscribe failed: \(error)")
logger?.error("channel error \(topic) \(error.localizedDescription)")
}
}
}
Expand Down Expand Up @@ -362,9 +362,9 @@ public actor RealtimeChannelV2 {
callbackManager.triggerBroadcast(event: event, json: payload)

case .close:
await socket._remove(self)
logger?.debug("Unsubscribed from channel \(message.topic)")
status = .unsubscribed
logger?.debug("channel close \(message.topic) \(joinRef ?? "<no joinRef>")")
await socket._remove(self)

case .error:
logger?.debug(
Expand Down
25 changes: 15 additions & 10 deletions Sources/Realtime/V2/RealtimeClientV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ public actor RealtimeClientV2 {
}

func connect(reconnect: Bool) async {
if let connectionTask { await connectionTask.value; return }
if let connectionTask {
await connectionTask.value
return
}

connectionTask = Task {
guard ws == nil else {
Expand Down Expand Up @@ -164,25 +167,24 @@ public actor RealtimeClientV2 {
startHeartbeating()

if reconnect {
await rejoinChannels()
Task {
await rejoinChannels()
}
}

flushSendBuffer()
} catch {
options.logger?.verbose("error \(error.localizedDescription)")
disconnect()
await connect(reconnect: true)
Task {
disconnect()
await connect(reconnect: true)
}
}
}

await connectionTask?.value
}

private func reconnect() async {
disconnect()
await connect(reconnect: true)
}

/// Creates a new channel and bind it to this client.
/// - Parameters:
/// - topic: Channel's topic.
Expand Down Expand Up @@ -295,7 +297,10 @@ public actor RealtimeClientV2 {
if pendingHeartbeatRef != nil {
pendingHeartbeatRef = nil
options.logger?.verbose("heartbeat timeout")
await reconnect()
Task {
disconnect()
await connect(reconnect: true)
}
}

let ref = makeRef()
Expand Down

0 comments on commit ac34b47

Please sign in to comment.