first pass at socketio/socket.io-client-swift#288
This commit is contained in:
parent
cc7d96e3d5
commit
c9ac69f408
@ -15,8 +15,8 @@ class SocketEngineTest: XCTestCase {
|
|||||||
|
|
||||||
override func setUp() {
|
override func setUp() {
|
||||||
super.setUp()
|
super.setUp()
|
||||||
client = SocketIOClient(socketURL: "")
|
client = SocketIOClient(socketURL: NSURL())
|
||||||
engine = SocketEngine(client: client, url: "", options: nil)
|
engine = SocketEngine(client: client, url: NSURL(), options: nil)
|
||||||
|
|
||||||
client.setTestable()
|
client.setTestable()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import XCTest
|
|||||||
@testable import SocketIOClientSwift
|
@testable import SocketIOClientSwift
|
||||||
|
|
||||||
class SocketParserTest: XCTestCase {
|
class SocketParserTest: XCTestCase {
|
||||||
let testSocket = SocketIOClient(socketURL: "")
|
let testSocket = SocketIOClient(socketURL: NSURL())
|
||||||
|
|
||||||
//Format key: message; namespace-data-binary-id
|
//Format key: message; namespace-data-binary-id
|
||||||
static let packetTypes: Dictionary<String, (String, [AnyObject], [NSData], Int)> = [
|
static let packetTypes: Dictionary<String, (String, [AnyObject], [NSData], Int)> = [
|
||||||
|
|||||||
@ -16,7 +16,7 @@ class SocketSideEffectTest: XCTestCase {
|
|||||||
|
|
||||||
override func setUp() {
|
override func setUp() {
|
||||||
super.setUp()
|
super.setUp()
|
||||||
socket = SocketIOClient(socketURL: "")
|
socket = SocketIOClient(socketURL: NSURL())
|
||||||
socket.setTestable()
|
socket.setTestable()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -61,7 +61,7 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
|
|||||||
|
|
||||||
private let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" {}").invertedSet
|
private let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" {}").invertedSet
|
||||||
private let logType = "SocketEngine"
|
private let logType = "SocketEngine"
|
||||||
private let url: String
|
private let url: NSURL
|
||||||
|
|
||||||
private var connectParams: [String: AnyObject]?
|
private var connectParams: [String: AnyObject]?
|
||||||
private var pingInterval: Double?
|
private var pingInterval: Double?
|
||||||
@ -76,11 +76,11 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
|
|||||||
private var secure = false
|
private var secure = false
|
||||||
private var selfSigned = false
|
private var selfSigned = false
|
||||||
private var voipEnabled = 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.client = client
|
||||||
self.url = url
|
self.url = url
|
||||||
|
|
||||||
for option in options {
|
for option in options {
|
||||||
switch option {
|
switch option {
|
||||||
case let .SessionDelegate(delegate):
|
case let .SessionDelegate(delegate):
|
||||||
@ -107,9 +107,20 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public convenience init(client: SocketEngineClient, url: String, options: NSDictionary?) {
|
public convenience init(client: SocketEngineClient, url: NSURL, options: NSDictionary?) {
|
||||||
self.init(client: client, url: url,
|
self.init(client: client, url: url, options: options?.toSocketOptionsSet() ?? [])
|
||||||
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 {
|
deinit {
|
||||||
@ -193,7 +204,16 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
|
|||||||
return ("", "")
|
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 urlPolling: String
|
||||||
var urlWebSocket: String
|
var urlWebSocket: String
|
||||||
|
|
||||||
|
|||||||
@ -46,7 +46,7 @@ import Foundation
|
|||||||
var urlWebSocket: String { get }
|
var urlWebSocket: String { get }
|
||||||
var websocket: Bool { get }
|
var websocket: Bool { get }
|
||||||
|
|
||||||
init(client: SocketEngineClient, url: String, options: NSDictionary?)
|
init(client: SocketEngineClient, url: NSURL, options: NSDictionary?)
|
||||||
|
|
||||||
func close(reason: String)
|
func close(reason: String)
|
||||||
func didError(error: String)
|
func didError(error: String)
|
||||||
|
|||||||
@ -25,7 +25,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable {
|
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 engine: SocketEngineSpec?
|
||||||
public private(set) var status = SocketIOClientStatus.NotConnected
|
public private(set) var status = SocketIOClientStatus.NotConnected
|
||||||
@ -55,25 +55,18 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
private(set) var reconnectAttempts = -1
|
private(set) var reconnectAttempts = -1
|
||||||
|
|
||||||
var waitingData = [SocketPacket]()
|
var waitingData = [SocketPacket]()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Type safe way to create a new SocketIOClient. opts can be omitted
|
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
|
self.options = options
|
||||||
|
self.socketURL = socketURL
|
||||||
if socketURL.hasPrefix("https://") {
|
|
||||||
|
if socketURL.absoluteString.hasPrefix("https://") {
|
||||||
self.options.insertIgnore(.Secure(true))
|
self.options.insertIgnore(.Secure(true))
|
||||||
}
|
}
|
||||||
|
|
||||||
var cleanedURL = socketURL["https?://"] <~ ""
|
|
||||||
|
|
||||||
if cleanedURL.hasSuffix("/") {
|
|
||||||
cleanedURL = String(cleanedURL.characters.dropLast())
|
|
||||||
}
|
|
||||||
|
|
||||||
self.socketURL = cleanedURL
|
|
||||||
|
|
||||||
for option in options {
|
for option in options {
|
||||||
switch option {
|
switch option {
|
||||||
case let .ConnectParams(params):
|
case let .ConnectParams(params):
|
||||||
@ -98,19 +91,32 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.options.insertIgnore(.Path("/socket.io"))
|
self.options.insertIgnore(.Path("/socket.io"))
|
||||||
|
|
||||||
super.init()
|
super.init()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Not so type safe way to create a SocketIOClient, meant for Objective-C compatiblity.
|
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?) {
|
public convenience init(socketURL: NSURL, options: NSDictionary?) {
|
||||||
self.init(socketURL: socketURL,
|
self.init(socketURL: socketURL, options: options?.toSocketOptionsSet() ?? [])
|
||||||
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 {
|
deinit {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user