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/")! public private(set) var urlWebSocket = URL(string: "http://localhost/")!
/// If `true`, then the engine is currently in WebSockets mode. /// 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 public private(set) var websocket = false
/// The WebSocket for this engine. /// The WebSocket for this engine.
@ -233,7 +234,6 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
if forceWebsockets { if forceWebsockets {
polling = false polling = false
websocket = true
createWebSocketAndConnect() createWebSocketAndConnect()
return return
} }
@ -312,19 +312,15 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
} }
private func _disconnect(reason: String) { 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) DefaultSocketLogger.Logger.log("Engine is being closed.", type: SocketEngine.logType)
if closed { if polling {
return closeOutEngine(reason: reason) disconnectPolling(reason: reason)
} } else {
if websocket {
sendWebSocketMessage("", withType: .close, withData: []) sendWebSocketMessage("", withType: .close, withData: [])
closeOutEngine(reason: reason) 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) "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: []) sendWebSocketMessage("", withType: .upgrade, withData: [])
websocket = true
polling = false polling = false
fastUpgrade = false fastUpgrade = false
probing = false probing = false
@ -443,6 +440,9 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
// We should upgrade // We should upgrade
if message == "3probe" { if message == "3probe" {
DefaultSocketLogger.Logger.log("Received probe response, should upgrade to WebSockets",
type: SocketEngine.logType)
upgradeTransport() upgradeTransport()
} }
@ -509,7 +509,6 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
sid = "" sid = ""
waitingForPoll = false waitingForPoll = false
waitingForPost = false waitingForPost = false
websocket = false
} }
private func sendPing() { 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]) { public func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data]) {
engineQueue.async { engineQueue.async {
guard self.connected else { return } guard self.connected else { return }
guard !self.probing else {
self.probeWait.append((msg, type, data))
if self.websocket { return
DefaultSocketLogger.Logger.log("Writing ws: \(msg) has data: \(data.count != 0)", }
type: SocketEngine.logType)
self.sendWebSocketMessage(msg, withType: type, withData: data) if self.polling {
} else if !self.probing {
DefaultSocketLogger.Logger.log("Writing poll: \(msg) has data: \(data.count != 0)", DefaultSocketLogger.Logger.log("Writing poll: \(msg) has data: \(data.count != 0)",
type: SocketEngine.logType) type: SocketEngine.logType)
self.sendPollMessage(msg, withType: type, withData: data) self.sendPollMessage(msg, withType: type, withData: data)
} else { } 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 return
} }
guard websocket else { guard !polling else {
flushProbeWait() flushProbeWait()
return return
} }
connected = false connected = false
websocket = false polling = true
if let reason = error?.localizedDescription { if let reason = error?.localizedDescription {
didError(reason: reason) 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. /// You shouldn't need to call this directly, the engine should automatically maintain a long-poll request.
public func doPoll() { public func doPoll() {
guard !websocket && !waitingForPoll && connected && !closed else { return } guard polling && !waitingForPoll && connected && !closed else { return }
var req = URLRequest(url: urlPollingWithSid) var req = URLRequest(url: urlPollingWithSid)
addHeaders(to: &req) addHeaders(to: &req)
@ -151,7 +151,7 @@ extension SocketEnginePollable {
private func flushWaitingForPost() { private func flushWaitingForPost() {
guard postWait.count != 0 && connected else { return } guard postWait.count != 0 && connected else { return }
guard !websocket else { guard polling else {
flushWaitingForPostToWebSocket() flushWaitingForPostToWebSocket()
return return

View File

@ -82,6 +82,7 @@ import Starscream
var urlWebSocket: URL { get } var urlWebSocket: URL { get }
/// If `true`, then the engine is currently in WebSockets mode. /// 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 } var websocket: Bool { get }
/// The WebSocket for this engine. /// The WebSocket for this engine.
@ -169,10 +170,10 @@ extension SocketEngineSpec {
} }
func createBinaryDataForSend(using data: Data) -> Either<Data, String> { func createBinaryDataForSend(using data: Data) -> Either<Data, String> {
if websocket { if polling {
return .left(Data(bytes: [0x4]) + data)
} else {
return .right("b4" + data.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0))) return .right("b4" + data.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)))
} else {
return .left(Data(bytes: [0x4]) + data)
} }
} }