File tree Expand file tree Collapse file tree 1 file changed +14
-0
lines changed
templates/swift/Sources/Services Expand file tree Collapse file tree 1 file changed +14
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,17 @@ open class Realtime : Service {
2222 private var reconnectAttempts = 0
2323 private var subscriptionsCounter = 0
2424 private var reconnect = true
25+
26+ private var onErrorCallbacks: [((Swift.Error?, HTTPResponseStatus?) -> Void)] = []
27+ private var onCloseCallbacks: [(() -> Void)] = []
28+
29+ public func onError(_ callback: @escaping (Swift.Error?, HTTPResponseStatus?) -> Void) {
30+ self.onErrorCallbacks.append(callback)
31+ }
32+
33+ public func onClose(_ callback: @escaping () -> Void) {
34+ self.onCloseCallbacks.append(callback)
35+ }
2536
2637 private func startHeartbeat() {
2738 stopHeartbeat()
@@ -212,6 +223,7 @@ extension Realtime: WebSocketClientDelegate {
212223 stopHeartbeat()
213224
214225 if (!reconnect) {
226+ onCloseCallbacks.forEach { $0() }
215227 reconnect = true
216228 return
217229 }
@@ -230,6 +242,8 @@ extension Realtime: WebSocketClientDelegate {
230242 public func onError(error: Swift.Error?, status: HTTPResponseStatus?) {
231243 stopHeartbeat()
232244 print(error?.localizedDescription ?? "Unknown error")
245+
246+ onErrorCallbacks.forEach { $0(error, status) }
233247 }
234248
235249 func handleResponseError(from json: [String: Any]) throws {
You can’t perform that action at this time.
0 commit comments