Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
Update logging for WebSockets (#138)
Browse files Browse the repository at this point in the history
* Update logging for WebSockets

* Convert all logs from print()/debugPrint() to NSLog() to support
debugging on devices
* Add opt-in trace level logging of all sent and received messages
with a new property "shouldPrintWebSocketTrace" added to allow turning
the new logs on
* Add description for "shouldPrintWebSocketTrace" in README

* Fix build errors for logging updates
  • Loading branch information
JoeSzymanski authored and flovilmart committed Dec 9, 2017
1 parent 8715b15 commit c2e6bbd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Sources/ParseLiveQuery/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.")
}
}

Expand Down
12 changes: 6 additions & 6 deletions Sources/ParseLiveQuery/Internal/ClientPrivate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
}
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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<Void> {
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))
Expand All @@ -217,8 +219,6 @@ extension Client {
throw LiveQueryErrors.InvalidResponseError(response: string)
}



switch response {
case .connected:
let sessionToken = PFUser.current()?.sessionToken
Expand Down

0 comments on commit c2e6bbd

Please sign in to comment.