This commit is contained in:
Erik 2016-01-25 14:55:58 -05:00
parent cc7d96e3d5
commit c9ac69f408
6 changed files with 59 additions and 33 deletions

View File

@ -15,8 +15,8 @@ class SocketEngineTest: XCTestCase {
override func setUp() {
super.setUp()
client = SocketIOClient(socketURL: "")
engine = SocketEngine(client: client, url: "", options: nil)
client = SocketIOClient(socketURL: NSURL())
engine = SocketEngine(client: client, url: NSURL(), options: nil)
client.setTestable()
}

View File

@ -10,7 +10,7 @@ import XCTest
@testable import SocketIOClientSwift
class SocketParserTest: XCTestCase {
let testSocket = SocketIOClient(socketURL: "")
let testSocket = SocketIOClient(socketURL: NSURL())
//Format key: message; namespace-data-binary-id
static let packetTypes: Dictionary<String, (String, [AnyObject], [NSData], Int)> = [

View File

@ -16,7 +16,7 @@ class SocketSideEffectTest: XCTestCase {
override func setUp() {
super.setUp()
socket = SocketIOClient(socketURL: "")
socket = SocketIOClient(socketURL: NSURL())
socket.setTestable()
}

View File

@ -61,7 +61,7 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
private let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" {}").invertedSet
private let logType = "SocketEngine"
private let url: String
private let url: NSURL
private var connectParams: [String: AnyObject]?
private var pingInterval: Double?
@ -76,11 +76,11 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
private var secure = false
private var selfSigned = false
private var voipEnabled = false
public init(client: SocketEngineClient, url: String, options: Set<SocketIOClientOption>) {
public init(client: SocketEngineClient, url: NSURL, options: Set<SocketIOClientOption>) {
self.client = client
self.url = url
for option in options {
switch option {
case let .SessionDelegate(delegate):
@ -107,9 +107,20 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
}
}
public convenience init(client: SocketEngineClient, url: String, options: NSDictionary?) {
self.init(client: client, url: url,
options: options?.toSocketOptionsSet() ?? [])
public convenience init(client: SocketEngineClient, url: NSURL, options: NSDictionary?) {
self.init(client: client, url: url, options: options?.toSocketOptionsSet() ?? [])
}
@available(*, deprecated=5.3)
public convenience init(client: SocketEngineClient, urlString: String, options: Set<SocketIOClientOption>) {
guard let url = NSURL(string: urlString) else { fatalError("Incorrect url") }
self.init(client: client, url: url, options: options)
}
@available(*, deprecated=5.3)
public convenience init(client: SocketEngineClient, urlString: String, options: NSDictionary?) {
guard let url = NSURL(string: urlString) else { fatalError("Incorrect url") }
self.init(client: client, url: url, options: options?.toSocketOptionsSet() ?? [])
}
deinit {
@ -193,7 +204,16 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
return ("", "")
}
let socketURL = "\(url)\(socketPath)/?transport="
let absURL = url.absoluteString["https?://"] <~ ""
let baseURL: String
if absURL.hasSuffix("/") {
baseURL = String(absURL.characters.dropLast())
} else {
baseURL = absURL
}
let socketURL = "\(baseURL)\(socketPath)/?transport="
var urlPolling: String
var urlWebSocket: String

View File

@ -46,7 +46,7 @@ import Foundation
var urlWebSocket: String { get }
var websocket: Bool { get }
init(client: SocketEngineClient, url: String, options: NSDictionary?)
init(client: SocketEngineClient, url: NSURL, options: NSDictionary?)
func close(reason: String)
func didError(error: String)

View File

@ -25,7 +25,7 @@
import Foundation
public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable {
public let socketURL: String
public let socketURL: NSURL
public private(set) var engine: SocketEngineSpec?
public private(set) var status = SocketIOClientStatus.NotConnected
@ -55,25 +55,18 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
private(set) var reconnectAttempts = -1
var waitingData = [SocketPacket]()
/**
Type safe way to create a new SocketIOClient. opts can be omitted
*/
public init(socketURL: String, options: Set<SocketIOClientOption> = []) {
public init(socketURL: NSURL, options: Set<SocketIOClientOption> = []) {
self.options = options
if socketURL.hasPrefix("https://") {
self.socketURL = socketURL
if socketURL.absoluteString.hasPrefix("https://") {
self.options.insertIgnore(.Secure(true))
}
var cleanedURL = socketURL["https?://"] <~ ""
if cleanedURL.hasSuffix("/") {
cleanedURL = String(cleanedURL.characters.dropLast())
}
self.socketURL = cleanedURL
for option in options {
switch option {
case let .ConnectParams(params):
@ -98,19 +91,32 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
continue
}
}
self.options.insertIgnore(.Path("/socket.io"))
super.init()
}
/**
Not so type safe way to create a SocketIOClient, meant for Objective-C compatiblity.
If using Swift it's recommended to use `init(var socketURL: String, options: Set<SocketIOClientOption>)`
If using Swift it's recommended to use `init(var socketURL: NSURL, options: Set<SocketIOClientOption>)`
*/
public convenience init(socketURL: String, options: NSDictionary?) {
self.init(socketURL: socketURL,
options: options?.toSocketOptionsSet() ?? [])
public convenience init(socketURL: NSURL, options: NSDictionary?) {
self.init(socketURL: socketURL, options: options?.toSocketOptionsSet() ?? [])
}
/// Please use the NSURL based init
@available(*, deprecated=5.3)
public convenience init(socketURLString: String, options: Set<SocketIOClientOption> = []) {
guard let url = NSURL(string: socketURLString) else { fatalError("Incorrect url") }
self.init(socketURL: url, options: options)
}
/// Please use the NSURL based init
@available(*, deprecated=5.3)
public convenience init(socketURLString: String, options: NSDictionary?) {
guard let url = NSURL(string: socketURLString) else { fatalError("Incorrect url") }
self.init(socketURL: url, options: options?.toSocketOptionsSet() ?? [])
}
deinit {