Skip to content

Commit

Permalink
fix: add isConnect flag to WebSocketClient
Browse files Browse the repository at this point in the history
  • Loading branch information
dodo849 committed Jun 5, 2024
1 parent 6483e98 commit 2a1dd96
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Sources/StompClient/WebSocketClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import OSLog
class WebSocketClient: NSObject, URLSessionWebSocketDelegate {
private var webSocketTask: URLSessionWebSocketTask?
private var urlSession: URLSession?
private(set) var isConnected: Bool = false
private let logger = Logger(
subsystem: Bundle.main.bundleIdentifier!,
category: "WebSocket"
Expand Down Expand Up @@ -55,7 +56,9 @@ class WebSocketClient: NSObject, URLSessionWebSocketDelegate {
print("WebSocket receive message:\n\(message)")
completion(.success(message))
}
self?.receiveMessage(completion)
if self?.isConnected == true {
self?.receiveMessage(completion)
}
}
}

Expand All @@ -68,10 +71,12 @@ class WebSocketClient: NSObject, URLSessionWebSocketDelegate {
// URLSessionWebSocketDelegate methods
func urlSession(_ session: URLSession, webSocketTask: URLSessionWebSocketTask, didOpenWithProtocol protocol: String?) {
self.logger.info("WebSocket connected successfully")
isConnected = true
}

func urlSession(_ session: URLSession, webSocketTask: URLSessionWebSocketTask, didCloseWith closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?) {
self.logger.info("WebSocket disconnected")
isConnected = false
}
}

Expand Down

0 comments on commit 2a1dd96

Please sign in to comment.