Treat transport open as joining the default namespace

This commit is contained in:
Erik Little 2017-10-03 11:02:22 -04:00
parent 84218d55c3
commit cd2454373a
No known key found for this signature in database
GPG Key ID: 62F837E56F4E9320
3 changed files with 69 additions and 18 deletions

View File

@ -8,6 +8,7 @@
import XCTest
@testable import SocketIO
import Starscream
class SocketSideEffectTest: XCTestCase {
func testInitialCurrentAck() {
@ -86,6 +87,8 @@ class SocketSideEffectTest: XCTestCase {
func testHandleOnceClientEvent() {
let expect = expectation(description: "handled event")
socket.setTestStatus(.connecting)
socket.once(clientEvent: .connect) {data, ack in
XCTAssertEqual(self.socket.testHandlers.count, 0)
expect.fulfill()
@ -249,6 +252,7 @@ class SocketSideEffectTest: XCTestCase {
let expect = expectation(description: "The client should call the timeout function")
socket.setTestStatus(.notConnected)
socket.engine = TestEngine(client: socket, url: socket.socketURL, options: nil)
socket.connect(timeoutAfter: 0.5, withHandler: {
expect.fulfill()
@ -261,6 +265,7 @@ class SocketSideEffectTest: XCTestCase {
let expect = expectation(description: "The client should not call the timeout function")
socket.setTestStatus(.notConnected)
socket.engine = TestEngine(client: socket, url: socket.socketURL, options: nil)
socket.on(clientEvent: .connect) {data, ack in
expect.fulfill()
@ -283,6 +288,8 @@ class SocketSideEffectTest: XCTestCase {
let nspString = "/swift"
socket.setTestStatus(.notConnected)
socket.nsp = nspString
socket.engine = TestEngine(client: socket, url: socket.socketURL, options: nil)
socket.on(clientEvent: .connect) {data, ack in
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]) { }
}

View File

@ -110,7 +110,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// The engine for this client.
@objc
public private(set) var engine: SocketEngineSpec?
public internal(set) var engine: SocketEngineSpec?
/// The array of handlers for this socket.
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.
open func didConnect(toNamespace namespace: String) {
guard status != .connected else { return }
DefaultSocketLogger.Logger.log("Socket connected", type: SocketIOClient.logType)
status = .connected
@ -434,7 +436,21 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
///
/// - parameter reason: The reason the engine opened.
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.
@ -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.
@objc
open func leaveNamespace() {
if nsp != "/" {
guard nsp != "/" else { return }
engine?.send("1\(nsp)", withData: [])
nsp = "/"
}
}
/// Joins `namespace`.
///
@ -493,13 +509,13 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
/// - parameter namespace: The namespace to join.
@objc
open func joinNamespace(_ namespace: String) {
nsp = namespace
guard namespace != "/" else { return }
if nsp != "/" {
DefaultSocketLogger.Logger.log("Joining namespace", type: SocketIOClient.logType)
DefaultSocketLogger.Logger.log("Joining namespace \(namespace)", type: SocketIOClient.logType)
nsp = namespace
engine?.send("0\(nsp)", withData: [])
}
}
/// Removes handler(s) for a client event.
///

View File

@ -71,14 +71,10 @@ public extension SocketParsable where Self: SocketIOClientSpec {
}
private func handleConnect(_ packetNamespace: String) {
// If we connected with a namespace, check if we've joined the default namespace first, then switch to the
// other namespace
if packetNamespace == "/" && nsp != "/" {
joinNamespace(nsp)
} else {
guard packetNamespace == nsp else { return }
didConnect(toNamespace: packetNamespace)
}
}
private func handlePacket(_ pack: SocketPacket) {
switch pack.type {