Skip to content

Commit 3df5d6d

Browse files
committed
Just use one boolean to determine polling/ws
1 parent ef2228c commit 3df5d6d

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

Source/SocketIO/Engine/SocketEngine.swift

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
112112
public private(set) var urlWebSocket = URL(string: "http://localhost/")!
113113

114114
/// If `true`, then the engine is currently in WebSockets mode.
115+
@available(*, deprecated, message: "No longer needed, if we're not polling, then we must be doing websockets")
115116
public private(set) var websocket = false
116117

117118
/// The WebSocket for this engine.
@@ -233,7 +234,6 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
233234

234235
if forceWebsockets {
235236
polling = false
236-
websocket = true
237237
createWebSocketAndConnect()
238238
return
239239
}
@@ -312,19 +312,15 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
312312
}
313313

314314
private func _disconnect(reason: String) {
315-
guard connected else { return closeOutEngine(reason: reason) }
315+
guard connected && !closed else { return closeOutEngine(reason: reason) }
316316

317317
DefaultSocketLogger.Logger.log("Engine is being closed.", type: SocketEngine.logType)
318318

319-
if closed {
320-
return closeOutEngine(reason: reason)
321-
}
322-
323-
if websocket {
319+
if polling {
320+
disconnectPolling(reason: reason)
321+
} else {
324322
sendWebSocketMessage("", withType: .close, withData: [])
325323
closeOutEngine(reason: reason)
326-
} else {
327-
disconnectPolling(reason: reason)
328324
}
329325
}
330326

@@ -347,8 +343,9 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
347343
"we'll probably disconnect soon. You should report this.", type: SocketEngine.logType)
348344
}
349345

346+
DefaultSocketLogger.Logger.log("Switching to WebSockets", type: SocketEngine.logType)
347+
350348
sendWebSocketMessage("", withType: .upgrade, withData: [])
351-
websocket = true
352349
polling = false
353350
fastUpgrade = false
354351
probing = false
@@ -443,6 +440,9 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
443440

444441
// We should upgrade
445442
if message == "3probe" {
443+
DefaultSocketLogger.Logger.log("Received probe response, should upgrade to WebSockets",
444+
type: SocketEngine.logType)
445+
446446
upgradeTransport()
447447
}
448448

@@ -509,7 +509,6 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
509509
sid = ""
510510
waitingForPoll = false
511511
waitingForPost = false
512-
websocket = false
513512
}
514513

515514
private func sendPing() {
@@ -592,17 +591,20 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
592591
public func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data]) {
593592
engineQueue.async {
594593
guard self.connected else { return }
594+
guard !self.probing else {
595+
self.probeWait.append((msg, type, data))
595596

596-
if self.websocket {
597-
DefaultSocketLogger.Logger.log("Writing ws: \(msg) has data: \(data.count != 0)",
598-
type: SocketEngine.logType)
599-
self.sendWebSocketMessage(msg, withType: type, withData: data)
600-
} else if !self.probing {
597+
return
598+
}
599+
600+
if self.polling {
601601
DefaultSocketLogger.Logger.log("Writing poll: \(msg) has data: \(data.count != 0)",
602602
type: SocketEngine.logType)
603603
self.sendPollMessage(msg, withType: type, withData: data)
604604
} else {
605-
self.probeWait.append((msg, type, data))
605+
DefaultSocketLogger.Logger.log("Writing ws: \(msg) has data: \(data.count != 0)",
606+
type: SocketEngine.logType)
607+
self.sendWebSocketMessage(msg, withType: type, withData: data)
606608
}
607609
}
608610
}
@@ -631,14 +633,14 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
631633
return
632634
}
633635

634-
guard websocket else {
636+
guard !polling else {
635637
flushProbeWait()
636638

637639
return
638640
}
639641

640642
connected = false
641-
websocket = false
643+
polling = true
642644

643645
if let reason = error?.localizedDescription {
644646
didError(reason: reason)

Source/SocketIO/Engine/SocketEnginePollable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ extension SocketEnginePollable {
101101
///
102102
/// You shouldn't need to call this directly, the engine should automatically maintain a long-poll request.
103103
public func doPoll() {
104-
guard !websocket && !waitingForPoll && connected && !closed else { return }
104+
guard polling && !waitingForPoll && connected && !closed else { return }
105105

106106
var req = URLRequest(url: urlPollingWithSid)
107107
addHeaders(to: &req)
@@ -151,7 +151,7 @@ extension SocketEnginePollable {
151151

152152
private func flushWaitingForPost() {
153153
guard postWait.count != 0 && connected else { return }
154-
guard !websocket else {
154+
guard polling else {
155155
flushWaitingForPostToWebSocket()
156156

157157
return

Source/SocketIO/Engine/SocketEngineSpec.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import Starscream
8282
var urlWebSocket: URL { get }
8383

8484
/// If `true`, then the engine is currently in WebSockets mode.
85+
@available(*, deprecated, message: "No longer needed, if we're not polling, then we must be doing websockets")
8586
var websocket: Bool { get }
8687

8788
/// The WebSocket for this engine.
@@ -169,10 +170,10 @@ extension SocketEngineSpec {
169170
}
170171

171172
func createBinaryDataForSend(using data: Data) -> Either<Data, String> {
172-
if websocket {
173-
return .left(Data(bytes: [0x4]) + data)
174-
} else {
173+
if polling {
175174
return .right("b4" + data.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)))
175+
} else {
176+
return .left(Data(bytes: [0x4]) + data)
176177
}
177178
}
178179

0 commit comments

Comments
 (0)