Skip to content
This repository was archived by the owner on Jun 13, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/ParseLiveQuery/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ open class Client: NSObject {
var socket: WebSocket?
public var shouldPrintWebSocketLog = true
public var userDisconnected = false
var isConnecting = false

// This allows us to easily plug in another request ID generation scheme, or more easily change the request id type
// if needed (technically this could be a string).
Expand Down Expand Up @@ -222,12 +223,14 @@ extension Client {
you use the client, and should usually only be called when an error occurs.
*/
public func reconnect() {
guard socket == nil || !isConnecting else { return }
socket?.disconnect()
socket = {
let socket = WebSocket(url: host)
socket.delegate = self
socket.callbackQueue = queue
socket.connect()
isConnecting = true
userDisconnected = false
return socket
}()
Expand All @@ -240,6 +243,7 @@ extension Client {
Use this if you wish to dispose of the live query client.
*/
public func disconnect() {
isConnecting = false
guard let socket = socket
else {
return
Expand Down
3 changes: 3 additions & 0 deletions Sources/ParseLiveQuery/Internal/ClientPrivate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ extension Client: WebSocketDelegate {
}

public func websocketDidConnect(socket: WebSocket) {
isConnecting = false
let sessionToken = PFUser.current()?.sessionToken ?? ""
_ = self.sendOperationAsync(.connect(applicationId: applicationId, sessionToken: sessionToken, clientKey: clientKey))
}

public func websocketDidDisconnect(socket: WebSocket, error: NSError?) {
isConnecting = false
if shouldPrintWebSocketLog { print("error: \(String(describing: error))") }

// TODO: Better retry logic, unless `disconnect()` was explicitly called
Expand All @@ -143,6 +145,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))") }

// TODO: Better retry logic, unless `disconnect()` was explicitly called
Expand Down