diff --git a/README.md b/README.md index 08264bda..89dfe02e 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,11 @@ By default, it will print the logs from WebSocket / WebSocketDelegate. You can t Client.shared.shouldPrintWebSocketLog = false ``` +You can also enable socket trace messages for all sent and received strings. By default, these trace messages are disabled. +```swift +Client.shared.shouldPrintWebSocketTrace = true +``` + Handling errors is and other events is similar, take a look at the `Subscription` class for more information. ## Advanced Usage diff --git a/Sources/ParseLiveQuery/Client.swift b/Sources/ParseLiveQuery/Client.swift index 92c9e13b..a8d9cf96 100644 --- a/Sources/ParseLiveQuery/Client.swift +++ b/Sources/ParseLiveQuery/Client.swift @@ -24,6 +24,7 @@ open class Client: NSObject { var socket: WebSocket? public var shouldPrintWebSocketLog = true + public var shouldPrintWebSocketTrace = false public var userDisconnected = false var isConnecting = false @@ -159,7 +160,7 @@ extension Client { if !userDisconnected { reconnect() } else { - debugPrint("Warning: The client was explicitly disconnected! You must explicitly call .reconnect() in order to process your subscriptions.") + NSLog("ParseLiveQuery: Warning: The client was explicitly disconnected! You must explicitly call .reconnect() in order to process your subscriptions.") } } diff --git a/Sources/ParseLiveQuery/Internal/ClientPrivate.swift b/Sources/ParseLiveQuery/Internal/ClientPrivate.swift index 9233f64a..1fb84002 100644 --- a/Sources/ParseLiveQuery/Internal/ClientPrivate.swift +++ b/Sources/ParseLiveQuery/Internal/ClientPrivate.swift @@ -117,13 +117,13 @@ func == (first: Client.RequestId, second: Client.RequestId) -> Bool { extension Client: WebSocketDelegate { public func websocketDidReceiveData(socket: WebSocket, data: Data) { - if shouldPrintWebSocketLog { print("Received binary data but we don't handle it...") } + if shouldPrintWebSocketLog { NSLog("ParseLiveQuery: Received binary data but we don't handle it...") } } public func websocketDidReceiveMessage(socket: WebSocket, text: String) { handleOperationAsync(text).continueWith { [weak self] task in if let error = task.error, self?.shouldPrintWebSocketLog == true { - print("Error: \(error)") + NSLog("ParseLiveQuery: Error processing message: \(error)") } } } @@ -136,7 +136,7 @@ extension Client: WebSocketDelegate { public func websocketDidDisconnect(socket: WebSocket, error: NSError?) { isConnecting = false - if shouldPrintWebSocketLog { print("error: \(String(describing: error))") } + if shouldPrintWebSocketLog { NSLog("ParseLiveQuery: WebSocket did disconnect with error: \(String(describing: error))") } // TODO: Better retry logic, unless `disconnect()` was explicitly called if !userDisconnected { @@ -146,7 +146,7 @@ extension Client: WebSocketDelegate { public func webSocket(_ webSocket: WebSocket, didCloseWithCode code: Int, reason: String?, wasClean: Bool) { isConnecting = false - if shouldPrintWebSocketLog { print("code: \(code) reason: \(String(describing: reason))") } + if shouldPrintWebSocketLog { NSLog("ParseLiveQuery: WebSocket did close with code: \(code) reason: \(String(describing: reason))") } // TODO: Better retry logic, unless `disconnect()` was explicitly called if !userDisconnected { @@ -202,12 +202,14 @@ extension Client { let jsonEncoded = operation.JSONObjectRepresentation let jsonData = try JSONSerialization.data(withJSONObject: jsonEncoded, options: JSONSerialization.WritingOptions(rawValue: 0)) let jsonString = String(data: jsonData, encoding: String.Encoding.utf8) + if self.shouldPrintWebSocketTrace { NSLog("ParseLiveQuery: Sending message: \(jsonString!)") } self.socket?.write(string: jsonString!) } } func handleOperationAsync(_ string: String) -> Task { return Task(.queue(queue)) { + if self.shouldPrintWebSocketTrace { NSLog("ParseLiveQuery: Received message: \(string)") } guard let jsonData = string.data(using: String.Encoding.utf8), let jsonDecoded = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions(rawValue: 0)) @@ -217,8 +219,6 @@ extension Client { throw LiveQueryErrors.InvalidResponseError(response: string) } - - switch response { case .connected: let sessionToken = PFUser.current()?.sessionToken