Just use one boolean to determine polling/ws

This commit is contained in:
Erik Little 2017-11-18 10:19:30 -05:00
parent ef2228cf29
commit 3df5d6d0b4
No known key found for this signature in database
GPG Key ID: 4930B7C5FBC1A69D
3 changed files with 27 additions and 24 deletions

View File

@ -112,6 +112,7 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
public private(set) var urlWebSocket = URL(string: "http://localhost/")!
/// If `true`, then the engine is currently in WebSockets mode.
@available(*, deprecated, message: "No longer needed, if we're not polling, then we must be doing websockets")
public private(set) var websocket = false
/// The WebSocket for this engine.
@ -233,7 +234,6 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
if forceWebsockets {
polling = false
websocket = true
createWebSocketAndConnect()
return
}
@ -312,19 +312,15 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
}
private func _disconnect(reason: String) {
guard connected else { return closeOutEngine(reason: reason) }
guard connected && !closed else { return closeOutEngine(reason: reason) }
DefaultSocketLogger.Logger.log("Engine is being closed.", type: SocketEngine.logType)
if closed {
return closeOutEngine(reason: reason)
}
if websocket {
if polling {
disconnectPolling(reason: reason)
} else {
sendWebSocketMessage("", withType: .close, withData: [])
closeOutEngine(reason: reason)
} else {
disconnectPolling(reason: reason)
}
}
@ -347,8 +343,9 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
"we'll probably disconnect soon. You should report this.", type: SocketEngine.logType)
}
DefaultSocketLogger.Logger.log("Switching to WebSockets", type: SocketEngine.logType)
sendWebSocketMessage("", withType: .upgrade, withData: [])
websocket = true
polling = false
fastUpgrade = false
probing = false
@ -443,6 +440,9 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
// We should upgrade
if message == "3probe" {
DefaultSocketLogger.Logger.log("Received probe response, should upgrade to WebSockets",
type: SocketEngine.logType)
upgradeTransport()
}
@ -509,7 +509,6 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
sid = ""
waitingForPoll = false
waitingForPost = false
websocket = false
}
private func sendPing() {
@ -592,17 +591,20 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
public func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data]) {
engineQueue.async {
guard self.connected else { return }
guard !self.probing else {
self.probeWait.append((msg, type, data))
if self.websocket {
DefaultSocketLogger.Logger.log("Writing ws: \(msg) has data: \(data.count != 0)",
type: SocketEngine.logType)
self.sendWebSocketMessage(msg, withType: type, withData: data)
} else if !self.probing {
return
}
if self.polling {
DefaultSocketLogger.Logger.log("Writing poll: \(msg) has data: \(data.count != 0)",
type: SocketEngine.logType)
self.sendPollMessage(msg, withType: type, withData: data)
} else {
self.probeWait.append((msg, type, data))
DefaultSocketLogger.Logger.log("Writing ws: \(msg) has data: \(data.count != 0)",
type: SocketEngine.logType)
self.sendWebSocketMessage(msg, withType: type, withData: data)
}
}
}
@ -631,14 +633,14 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
return
}
guard websocket else {
guard !polling else {
flushProbeWait()
return
}
connected = false
websocket = false
polling = true
if let reason = error?.localizedDescription {
didError(reason: reason)

View File

@ -101,7 +101,7 @@ extension SocketEnginePollable {
///
/// You shouldn't need to call this directly, the engine should automatically maintain a long-poll request.
public func doPoll() {
guard !websocket && !waitingForPoll && connected && !closed else { return }
guard polling && !waitingForPoll && connected && !closed else { return }
var req = URLRequest(url: urlPollingWithSid)
addHeaders(to: &req)
@ -151,7 +151,7 @@ extension SocketEnginePollable {
private func flushWaitingForPost() {
guard postWait.count != 0 && connected else { return }
guard !websocket else {
guard polling else {
flushWaitingForPostToWebSocket()
return

View File

@ -82,6 +82,7 @@ import Starscream
var urlWebSocket: URL { get }
/// If `true`, then the engine is currently in WebSockets mode.
@available(*, deprecated, message: "No longer needed, if we're not polling, then we must be doing websockets")
var websocket: Bool { get }
/// The WebSocket for this engine.
@ -169,10 +170,10 @@ extension SocketEngineSpec {
}
func createBinaryDataForSend(using data: Data) -> Either<Data, String> {
if websocket {
return .left(Data(bytes: [0x4]) + data)
} else {
if polling {
return .right("b4" + data.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)))
} else {
return .left(Data(bytes: [0x4]) + data)
}
}