Treat transport open as joining the default namespace
This commit is contained in:
parent
84218d55c3
commit
cd2454373a
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
import XCTest
|
import XCTest
|
||||||
@testable import SocketIO
|
@testable import SocketIO
|
||||||
|
import Starscream
|
||||||
|
|
||||||
class SocketSideEffectTest: XCTestCase {
|
class SocketSideEffectTest: XCTestCase {
|
||||||
func testInitialCurrentAck() {
|
func testInitialCurrentAck() {
|
||||||
@ -86,6 +87,8 @@ class SocketSideEffectTest: XCTestCase {
|
|||||||
func testHandleOnceClientEvent() {
|
func testHandleOnceClientEvent() {
|
||||||
let expect = expectation(description: "handled event")
|
let expect = expectation(description: "handled event")
|
||||||
|
|
||||||
|
socket.setTestStatus(.connecting)
|
||||||
|
|
||||||
socket.once(clientEvent: .connect) {data, ack in
|
socket.once(clientEvent: .connect) {data, ack in
|
||||||
XCTAssertEqual(self.socket.testHandlers.count, 0)
|
XCTAssertEqual(self.socket.testHandlers.count, 0)
|
||||||
expect.fulfill()
|
expect.fulfill()
|
||||||
@ -249,6 +252,7 @@ class SocketSideEffectTest: XCTestCase {
|
|||||||
let expect = expectation(description: "The client should call the timeout function")
|
let expect = expectation(description: "The client should call the timeout function")
|
||||||
|
|
||||||
socket.setTestStatus(.notConnected)
|
socket.setTestStatus(.notConnected)
|
||||||
|
socket.engine = TestEngine(client: socket, url: socket.socketURL, options: nil)
|
||||||
|
|
||||||
socket.connect(timeoutAfter: 0.5, withHandler: {
|
socket.connect(timeoutAfter: 0.5, withHandler: {
|
||||||
expect.fulfill()
|
expect.fulfill()
|
||||||
@ -261,6 +265,7 @@ class SocketSideEffectTest: XCTestCase {
|
|||||||
let expect = expectation(description: "The client should not call the timeout function")
|
let expect = expectation(description: "The client should not call the timeout function")
|
||||||
|
|
||||||
socket.setTestStatus(.notConnected)
|
socket.setTestStatus(.notConnected)
|
||||||
|
socket.engine = TestEngine(client: socket, url: socket.socketURL, options: nil)
|
||||||
|
|
||||||
socket.on(clientEvent: .connect) {data, ack in
|
socket.on(clientEvent: .connect) {data, ack in
|
||||||
expect.fulfill()
|
expect.fulfill()
|
||||||
@ -283,6 +288,8 @@ class SocketSideEffectTest: XCTestCase {
|
|||||||
let nspString = "/swift"
|
let nspString = "/swift"
|
||||||
|
|
||||||
socket.setTestStatus(.notConnected)
|
socket.setTestStatus(.notConnected)
|
||||||
|
socket.nsp = nspString
|
||||||
|
socket.engine = TestEngine(client: socket, url: socket.socketURL, options: nil)
|
||||||
|
|
||||||
socket.on(clientEvent: .connect) {data, ack in
|
socket.on(clientEvent: .connect) {data, ack in
|
||||||
guard let nsp = data[0] as? String else {
|
guard let nsp = data[0] as? String else {
|
||||||
@ -390,3 +397,35 @@ struct ThrowingData : SocketData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class TestEngine : SocketEngineSpec {
|
||||||
|
var client: SocketEngineClient? = nil
|
||||||
|
private(set) var closed = false
|
||||||
|
private(set) var connected = false
|
||||||
|
var connectParams: [String: Any]? = nil
|
||||||
|
private(set) var cookies: [HTTPCookie]? = nil
|
||||||
|
private(set) var engineQueue = DispatchQueue.main
|
||||||
|
private(set) var extraHeaders: [String: String]? = nil
|
||||||
|
private(set) var fastUpgrade = false
|
||||||
|
private(set) var forcePolling = false
|
||||||
|
private(set) var forceWebsockets = false
|
||||||
|
private(set) var polling = false
|
||||||
|
private(set) var probing = false
|
||||||
|
private(set) var sid = ""
|
||||||
|
private(set) var socketPath = ""
|
||||||
|
private(set) var urlPolling = URL(string: "http://localhost/")!
|
||||||
|
private(set) var urlWebSocket = URL(string: "http://localhost/")!
|
||||||
|
private(set) var websocket = false
|
||||||
|
private(set) var ws: WebSocket? = nil
|
||||||
|
|
||||||
|
required init(client: SocketEngineClient, url: URL, options: NSDictionary?) { }
|
||||||
|
|
||||||
|
func connect() { }
|
||||||
|
func didError(reason: String) { }
|
||||||
|
func disconnect(reason: String) { }
|
||||||
|
func doFastUpgrade() { }
|
||||||
|
func flushWaitingForPostToWebSocket() { }
|
||||||
|
func parseEngineData(_ data: Data) { }
|
||||||
|
func parseEngineMessage(_ message: String) { }
|
||||||
|
func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data]) { }
|
||||||
|
}
|
||||||
|
|||||||
@ -110,7 +110,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
|
|||||||
|
|
||||||
/// The engine for this client.
|
/// The engine for this client.
|
||||||
@objc
|
@objc
|
||||||
public private(set) var engine: SocketEngineSpec?
|
public internal(set) var engine: SocketEngineSpec?
|
||||||
|
|
||||||
/// The array of handlers for this socket.
|
/// The array of handlers for this socket.
|
||||||
public private(set) var handlers = [SocketEventHandler]()
|
public private(set) var handlers = [SocketEventHandler]()
|
||||||
@ -244,6 +244,8 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
|
|||||||
///
|
///
|
||||||
/// - parameter toNamespace: The namespace that was connected to.
|
/// - parameter toNamespace: The namespace that was connected to.
|
||||||
open func didConnect(toNamespace namespace: String) {
|
open func didConnect(toNamespace namespace: String) {
|
||||||
|
guard status != .connected else { return }
|
||||||
|
|
||||||
DefaultSocketLogger.Logger.log("Socket connected", type: SocketIOClient.logType)
|
DefaultSocketLogger.Logger.log("Socket connected", type: SocketIOClient.logType)
|
||||||
|
|
||||||
status = .connected
|
status = .connected
|
||||||
@ -434,7 +436,21 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
|
|||||||
///
|
///
|
||||||
/// - parameter reason: The reason the engine opened.
|
/// - parameter reason: The reason the engine opened.
|
||||||
open func engineDidOpen(reason: String) {
|
open func engineDidOpen(reason: String) {
|
||||||
DefaultSocketLogger.Logger.log(reason, type: SocketIOClient.logType)
|
handleQueue.async {
|
||||||
|
self._engineDidOpen(reason: reason)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func _engineDidOpen(reason: String) {
|
||||||
|
DefaultSocketLogger.Logger.log("Engine opened \(reason)", type: SocketIOClient.logType)
|
||||||
|
|
||||||
|
guard nsp != "/" else {
|
||||||
|
didConnect(toNamespace: "/")
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
joinNamespace(nsp)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Called when socket.io has acked one of our emits. Causes the corresponding ack callback to be called.
|
/// Called when socket.io has acked one of our emits. Causes the corresponding ack callback to be called.
|
||||||
@ -480,11 +496,11 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
|
|||||||
/// Call when you wish to leave a namespace and return to the default namespace.
|
/// Call when you wish to leave a namespace and return to the default namespace.
|
||||||
@objc
|
@objc
|
||||||
open func leaveNamespace() {
|
open func leaveNamespace() {
|
||||||
if nsp != "/" {
|
guard nsp != "/" else { return }
|
||||||
|
|
||||||
engine?.send("1\(nsp)", withData: [])
|
engine?.send("1\(nsp)", withData: [])
|
||||||
nsp = "/"
|
nsp = "/"
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Joins `namespace`.
|
/// Joins `namespace`.
|
||||||
///
|
///
|
||||||
@ -493,13 +509,13 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
|
|||||||
/// - parameter namespace: The namespace to join.
|
/// - parameter namespace: The namespace to join.
|
||||||
@objc
|
@objc
|
||||||
open func joinNamespace(_ namespace: String) {
|
open func joinNamespace(_ namespace: String) {
|
||||||
nsp = namespace
|
guard namespace != "/" else { return }
|
||||||
|
|
||||||
if nsp != "/" {
|
DefaultSocketLogger.Logger.log("Joining namespace \(namespace)", type: SocketIOClient.logType)
|
||||||
DefaultSocketLogger.Logger.log("Joining namespace", type: SocketIOClient.logType)
|
|
||||||
|
nsp = namespace
|
||||||
engine?.send("0\(nsp)", withData: [])
|
engine?.send("0\(nsp)", withData: [])
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Removes handler(s) for a client event.
|
/// Removes handler(s) for a client event.
|
||||||
///
|
///
|
||||||
|
|||||||
@ -71,14 +71,10 @@ public extension SocketParsable where Self: SocketIOClientSpec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func handleConnect(_ packetNamespace: String) {
|
private func handleConnect(_ packetNamespace: String) {
|
||||||
// If we connected with a namespace, check if we've joined the default namespace first, then switch to the
|
guard packetNamespace == nsp else { return }
|
||||||
// other namespace
|
|
||||||
if packetNamespace == "/" && nsp != "/" {
|
|
||||||
joinNamespace(nsp)
|
|
||||||
} else {
|
|
||||||
didConnect(toNamespace: packetNamespace)
|
didConnect(toNamespace: packetNamespace)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private func handlePacket(_ pack: SocketPacket) {
|
private func handlePacket(_ pack: SocketPacket) {
|
||||||
switch pack.type {
|
switch pack.type {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user