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

Commit b039624

Browse files
JoeSzymanskiflovilmart
authored andcommitted
Don't attempt to reconnect if a connection is already in progress (#140)
1 parent c462f09 commit b039624

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

Sources/ParseLiveQuery/Client.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ open class Client: NSObject {
2525
var socket: WebSocket?
2626
public var shouldPrintWebSocketLog = true
2727
public var userDisconnected = false
28+
var isConnecting = false
2829

2930
// This allows us to easily plug in another request ID generation scheme, or more easily change the request id type
3031
// if needed (technically this could be a string).
@@ -222,12 +223,14 @@ extension Client {
222223
you use the client, and should usually only be called when an error occurs.
223224
*/
224225
public func reconnect() {
226+
guard socket == nil || !isConnecting else { return }
225227
socket?.disconnect()
226228
socket = {
227229
let socket = WebSocket(url: host)
228230
socket.delegate = self
229231
socket.callbackQueue = queue
230232
socket.connect()
233+
isConnecting = true
231234
userDisconnected = false
232235
return socket
233236
}()
@@ -240,6 +243,7 @@ extension Client {
240243
Use this if you wish to dispose of the live query client.
241244
*/
242245
public func disconnect() {
246+
isConnecting = false
243247
guard let socket = socket
244248
else {
245249
return

Sources/ParseLiveQuery/Internal/ClientPrivate.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,13 @@ extension Client: WebSocketDelegate {
129129
}
130130

131131
public func websocketDidConnect(socket: WebSocket) {
132+
isConnecting = false
132133
let sessionToken = PFUser.current()?.sessionToken ?? ""
133134
_ = self.sendOperationAsync(.connect(applicationId: applicationId, sessionToken: sessionToken, clientKey: clientKey))
134135
}
135136

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

139141
// TODO: Better retry logic, unless `disconnect()` was explicitly called
@@ -143,6 +145,7 @@ extension Client: WebSocketDelegate {
143145
}
144146

145147
public func webSocket(_ webSocket: WebSocket, didCloseWithCode code: Int, reason: String?, wasClean: Bool) {
148+
isConnecting = false
146149
if shouldPrintWebSocketLog { print("code: \(code) reason: \(String(describing: reason))") }
147150

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

0 commit comments

Comments
 (0)