notify client when engine disconnects

This commit is contained in:
Erik 2016-06-24 02:21:25 -04:00
parent 6fd3294fba
commit 4383ddfe65

View File

@ -166,7 +166,7 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe
} }
} }
private func closeOutEngine() { private func closeOutEngine(reason: String) {
sid = "" sid = ""
closed = true closed = true
invalidated = true invalidated = true
@ -174,6 +174,7 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe
ws?.disconnect() ws?.disconnect()
stopPolling() stopPolling()
client?.engineDidClose(reason)
} }
/// Starts the connection to the server /// Starts the connection to the server
@ -280,32 +281,30 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe
} }
public func disconnect(reason: String) { public func disconnect(reason: String) {
guard connected else { return closeOutEngine() } guard connected else { return closeOutEngine(reason) }
DefaultSocketLogger.Logger.log("Engine is being closed.", type: logType) DefaultSocketLogger.Logger.log("Engine is being closed.", type: logType)
if closed { if closed {
closeOutEngine() return closeOutEngine(reason)
client?.engineDidClose(reason)
return
} }
if websocket { if websocket {
sendWebSocketMessage("", withType: .Close, withData: []) sendWebSocketMessage("", withType: .Close, withData: [])
closeOutEngine() closeOutEngine(reason)
} else { } else {
disconnectPolling() disconnectPolling(reason)
} }
} }
// We need to take special care when we're polling that we send it ASAP // We need to take special care when we're polling that we send it ASAP
// Also make sure we're on the emitQueue since we're touching postWait // Also make sure we're on the emitQueue since we're touching postWait
private func disconnectPolling() { private func disconnectPolling(reason: String) {
dispatch_sync(emitQueue) { dispatch_sync(emitQueue) {
self.postWait.append(String(SocketEnginePacketType.Close.rawValue)) self.postWait.append(String(SocketEnginePacketType.Close.rawValue))
let req = self.createRequestForPostWithPostWait() let req = self.createRequestForPostWithPostWait()
self.doRequest(req) {_, _, _ in } self.doRequest(req) {_, _, _ in }
self.closeOutEngine() self.closeOutEngine(reason)
} }
} }