This commit is contained in:
Erik 2016-06-07 13:12:34 -04:00
parent 02f30a81dd
commit b919d3e411
2 changed files with 26 additions and 21 deletions

View File

@ -163,6 +163,16 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe
} }
} }
private func closeOutSocket() {
sid = ""
closed = true
invalidated = true
connected = false
ws?.disconnect()
stopPolling()
}
/// Starts the connection to the server /// Starts the connection to the server
public func connect() { public func connect() {
if connected { if connected {
@ -266,37 +276,32 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe
} }
public func disconnect(reason: String) { public func disconnect(reason: String) {
func postSendClose(data: NSData?, _ res: NSURLResponse?, _ err: NSError?) { guard connected else { return closeOutSocket() }
sid = ""
closed = true
invalidated = true
connected = false
ws?.disconnect()
stopPolling()
}
guard connected else { return postSendClose(nil, nil, nil) }
DefaultSocketLogger.Logger.log("Engine is being closed.", type: logType) DefaultSocketLogger.Logger.log("Engine is being closed.", type: logType)
if closed { if closed {
postSendClose(nil, nil, nil) closeOutSocket()
client?.engineDidClose(reason) client?.engineDidClose(reason)
return return
} }
if websocket { if websocket {
sendWebSocketMessage("", withType: .Close, withData: []) sendWebSocketMessage("", withType: .Close, withData: [])
postSendClose(nil, nil, nil) closeOutSocket()
} else { } else {
// We need to take special care when we're polling that we send it ASAP disconnectPolling()
// Also make sure we're on the emitQueue since we're touching postWait }
dispatch_sync(emitQueue) { }
self.postWait.append(String(SocketEnginePacketType.Close.rawValue))
let req = self.createRequestForPostWithPostWait() // We need to take special care when we're polling that we send it ASAP
self.doRequest(req, withCallback: postSendClose) // Also make sure we're on the emitQueue since we're touching postWait
} private func disconnectPolling() {
dispatch_sync(emitQueue) {
self.postWait.append(String(SocketEnginePacketType.Close.rawValue))
let req = self.createRequestForPostWithPostWait()
self.doRequest(req) {_, _, _ in }
self.closeOutSocket()
} }
} }

View File

@ -123,7 +123,7 @@ extension SocketEnginePollable {
return return
} }
DefaultSocketLogger.Logger.log("Got polling response", type: "SocketEnginePolling") DefaultSocketLogger.Logger.log("Got polling response", type: "SocketEnginePolling")
if let str = String(data: data!, encoding: NSUTF8StringEncoding) { if let str = String(data: data!, encoding: NSUTF8StringEncoding) {