commit
83cee796d8
40
README.md
40
README.md
@ -5,7 +5,7 @@ Socket.IO-client for iOS/OS X.
|
|||||||
|
|
||||||
##Example
|
##Example
|
||||||
```swift
|
```swift
|
||||||
let socket = SocketIOClient(socketURL: "localhost:8080")
|
let socket = SocketIOClient(socketURL: "localhost:8080", options: [.Log(true), .ForcePolling(true)])
|
||||||
|
|
||||||
socket.on("connect") {data, ack in
|
socket.on("connect") {data, ack in
|
||||||
print("socket connected")
|
print("socket connected")
|
||||||
@ -26,7 +26,7 @@ socket.connect()
|
|||||||
|
|
||||||
##Objective-C Example
|
##Objective-C Example
|
||||||
```objective-c
|
```objective-c
|
||||||
SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" opts:nil];
|
SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" options:@{@"log": @YES, @"forcePolling": @YES}];
|
||||||
|
|
||||||
[socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
|
[socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
|
||||||
NSLog(@"socket connected");
|
NSLog(@"socket connected");
|
||||||
@ -120,25 +120,29 @@ Run `seed install`.
|
|||||||
##API
|
##API
|
||||||
Constructors
|
Constructors
|
||||||
-----------
|
-----------
|
||||||
`init(socketURL: String, opts: NSDictionary? = nil)` - Constructs a new client for the given URL. opts can be omitted (will use default values) note: If your socket.io server is secure, you need to specify `https` in your socketURL.
|
`init(var socketURL: String, options: Set<SocketIOClientOption>? = nil)` - Creates a new SocketIOClient. opts is a Set of SocketIOClientOption. If your socket.io server is secure, you need to specify `https` in your socketURL.
|
||||||
|
`convenience init(socketURL: String, options: NSDictionary?)` - Same as above, but meant for Objective-C. See Options on how convert between SocketIOClientOptions and dictionary keys.
|
||||||
|
|
||||||
Options
|
Options
|
||||||
-------
|
-------
|
||||||
- `connectParams: [String: AnyObject]?` - Dictionary whose contents will be passed with the connection.
|
All options are a case of SocketIOClientOption. To get the Objective-C Option, convert the name to lowerCamelCase.
|
||||||
- `reconnects: Bool` Default is `true`
|
|
||||||
- `reconnectAttempts: Int` Default is `-1` (infinite tries)
|
|
||||||
- `reconnectWait: Int` Default is `10`
|
|
||||||
- `forcePolling: Bool` Default is `false`. `true` forces the client to use xhr-polling.
|
|
||||||
- `forceWebsockets: Bool` Default is `false`. `true` forces the client to use WebSockets.
|
|
||||||
- `nsp: String` Default is `"/"`. Connects to a namespace.
|
|
||||||
- `cookies: [NSHTTPCookie]?` An array of NSHTTPCookies. Passed during the handshake. Default is nil.
|
|
||||||
- `log: Bool` If `true` socket will log debug messages. Default is false.
|
|
||||||
- `logger: SocketLogger` If you wish to implement your own logger that conforms to SocketLogger you can pass it in here. Will use the default logging defined under the protocol otherwise.
|
|
||||||
- `sessionDelegate: NSURLSessionDelegate` Sets an NSURLSessionDelegate for the underlying engine. Useful if you need to handle self-signed certs. Default is nil.
|
|
||||||
- `path: String` - If the server uses a custom path. ex: `"/swift"`. Default is `""`
|
|
||||||
- `extraHeaders: [String: String]?` - Adds custom headers to the initial request. Default is nil.
|
|
||||||
- `handleQueue: dispatch_queue_t` - The dispatch queue that handlers are run on. Default is the main queue.
|
|
||||||
|
|
||||||
|
```swift
|
||||||
|
case ConnectParams([String: AnyObject]) // Dictionary whose contents will be passed with the connection.
|
||||||
|
case Reconnects(Bool) // Whether to reconnect on server lose. Default is `true`
|
||||||
|
case ReconnectAttempts(Int) // How many times to reconnect. Default is `-1` (infinite tries)
|
||||||
|
case ReconnectWait(Int) // Amount of time to wait between reconnects. Default is `10`
|
||||||
|
case ForcePolling(Bool) // `true` forces the client to use xhr-polling. Default is `false`
|
||||||
|
case ForceWebsockets(Bool) // `true` forces the client to use WebSockets. Default is `false`
|
||||||
|
case Nsp(String) // The namespace to connect to. Must begin with /. Default is `/`
|
||||||
|
case Cookies([NSHTTPCookie]) // An array of NSHTTPCookies. Passed during the handshake. Default is nil.
|
||||||
|
case Log(Bool) // If `true` socket will log debug messages. Default is false.
|
||||||
|
case Logger(SocketLogger) // Custom logger that conforms to SocketLogger. Will use the default logging otherwise.
|
||||||
|
case SessionDelegate(NSURLSessionDelegate) // Sets an NSURLSessionDelegate for the underlying engine. Useful if you need to handle self-signed certs. Default is nil.
|
||||||
|
case Path(String) // If the server uses a custom path. ex: `"/swift"`. Default is `""`
|
||||||
|
case ExtraHeaders([String: String]) // Adds custom headers to the initial request. Default is nil.
|
||||||
|
case HandleQueue(dispatch_queue_t) // The dispatch queue that handlers are run on. Default is the main queue.
|
||||||
|
```
|
||||||
Methods
|
Methods
|
||||||
-------
|
-------
|
||||||
1. `on(event: String, callback: NormalCallback)` - Adds a handler for an event. Items are passed by an array. `ack` can be used to send an ack when one is requested. See example.
|
1. `on(event: String, callback: NormalCallback)` - Adds a handler for an event. Items are passed by an array. `ack` can be used to send an ack when one is requested. See example.
|
||||||
@ -154,6 +158,8 @@ Methods
|
|||||||
11. `reconnect()` - Causes the client to reconnect to the server.
|
11. `reconnect()` - Causes the client to reconnect to the server.
|
||||||
12. `joinNamespace()` - Causes the client to join nsp. Shouldn't need to be called unless you change nsp manually.
|
12. `joinNamespace()` - Causes the client to join nsp. Shouldn't need to be called unless you change nsp manually.
|
||||||
13. `leaveNamespace()` - Causes the client to leave the nsp and go back to /
|
13. `leaveNamespace()` - Causes the client to leave the nsp and go back to /
|
||||||
|
14. `off(event: String)` - Removes all event handlers for event.
|
||||||
|
15. `removeAllHandlers()` - Removes all handlers.
|
||||||
|
|
||||||
Client Events
|
Client Events
|
||||||
------
|
------
|
||||||
|
|||||||
@ -37,6 +37,10 @@
|
|||||||
5764DFA01B51F254004FF46E /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5764DF871B51F254004FF46E /* SwiftRegex.swift */; };
|
5764DFA01B51F254004FF46E /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5764DF871B51F254004FF46E /* SwiftRegex.swift */; };
|
||||||
5764DFA11B51F254004FF46E /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5764DF881B51F254004FF46E /* WebSocket.swift */; };
|
5764DFA11B51F254004FF46E /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5764DF881B51F254004FF46E /* WebSocket.swift */; };
|
||||||
5764DFA21B51F254004FF46E /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5764DF881B51F254004FF46E /* WebSocket.swift */; };
|
5764DFA21B51F254004FF46E /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5764DF881B51F254004FF46E /* WebSocket.swift */; };
|
||||||
|
740E10441BD2C4680064FC4A /* SocketIOClientOption.swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOption.swift */; settings = {ASSET_TAGS = (); }; };
|
||||||
|
740E10451BD2C4680064FC4A /* SocketIOClientOption.swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOption.swift */; settings = {ASSET_TAGS = (); }; };
|
||||||
|
740E10461BD2C4680064FC4A /* SocketIOClientOption.swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOption.swift */; settings = {ASSET_TAGS = (); }; };
|
||||||
|
740E10471BD2C4680064FC4A /* SocketIOClientOption.swift in Sources */ = {isa = PBXBuildFile; fileRef = 740E10431BD2C4680064FC4A /* SocketIOClientOption.swift */; settings = {ASSET_TAGS = (); }; };
|
||||||
741F39EE1BD025D80026C9CC /* SocketEngineTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */; settings = {ASSET_TAGS = (); }; };
|
741F39EE1BD025D80026C9CC /* SocketEngineTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */; settings = {ASSET_TAGS = (); }; };
|
||||||
741F39EF1BD025D80026C9CC /* SocketEngineTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */; settings = {ASSET_TAGS = (); }; };
|
741F39EF1BD025D80026C9CC /* SocketEngineTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */; settings = {ASSET_TAGS = (); }; };
|
||||||
745895381BB59A0A0050ACC8 /* SocketAckManagerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94A20D601B99E22F00BF9E44 /* SocketAckManagerTest.swift */; };
|
745895381BB59A0A0050ACC8 /* SocketAckManagerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94A20D601B99E22F00BF9E44 /* SocketAckManagerTest.swift */; };
|
||||||
@ -138,6 +142,7 @@
|
|||||||
5764DF861B51F254004FF46E /* SocketTypes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketTypes.swift; path = SocketIOClientSwift/SocketTypes.swift; sourceTree = "<group>"; };
|
5764DF861B51F254004FF46E /* SocketTypes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketTypes.swift; path = SocketIOClientSwift/SocketTypes.swift; sourceTree = "<group>"; };
|
||||||
5764DF871B51F254004FF46E /* SwiftRegex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SwiftRegex.swift; path = SocketIOClientSwift/SwiftRegex.swift; sourceTree = "<group>"; };
|
5764DF871B51F254004FF46E /* SwiftRegex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SwiftRegex.swift; path = SocketIOClientSwift/SwiftRegex.swift; sourceTree = "<group>"; };
|
||||||
5764DF881B51F254004FF46E /* WebSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WebSocket.swift; path = SocketIOClientSwift/WebSocket.swift; sourceTree = "<group>"; };
|
5764DF881B51F254004FF46E /* WebSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WebSocket.swift; path = SocketIOClientSwift/WebSocket.swift; sourceTree = "<group>"; };
|
||||||
|
740E10431BD2C4680064FC4A /* SocketIOClientOption.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketIOClientOption.swift; path = SocketIOClientSwift/SocketIOClientOption.swift; sourceTree = "<group>"; };
|
||||||
741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketEngineTest.swift; sourceTree = "<group>"; };
|
741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketEngineTest.swift; sourceTree = "<group>"; };
|
||||||
7472C65B1BCAB53E003CA70D /* SocketNamespacePacketTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketNamespacePacketTest.swift; sourceTree = "<group>"; };
|
7472C65B1BCAB53E003CA70D /* SocketNamespacePacketTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketNamespacePacketTest.swift; sourceTree = "<group>"; };
|
||||||
7472C65E1BCAC46E003CA70D /* SocketSideEffectTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketSideEffectTest.swift; sourceTree = "<group>"; };
|
7472C65E1BCAC46E003CA70D /* SocketSideEffectTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketSideEffectTest.swift; sourceTree = "<group>"; };
|
||||||
@ -293,6 +298,7 @@
|
|||||||
5764DF801B51F254004FF46E /* SocketEventHandler.swift */,
|
5764DF801B51F254004FF46E /* SocketEventHandler.swift */,
|
||||||
5764DF811B51F254004FF46E /* SocketFixUTF8.swift */,
|
5764DF811B51F254004FF46E /* SocketFixUTF8.swift */,
|
||||||
5764DF821B51F254004FF46E /* SocketIOClient.swift */,
|
5764DF821B51F254004FF46E /* SocketIOClient.swift */,
|
||||||
|
740E10431BD2C4680064FC4A /* SocketIOClientOption.swift */,
|
||||||
74781D591B7E83930042CACA /* SocketIOClientStatus.swift */,
|
74781D591B7E83930042CACA /* SocketIOClientStatus.swift */,
|
||||||
5764DF831B51F254004FF46E /* SocketLogger.swift */,
|
5764DF831B51F254004FF46E /* SocketLogger.swift */,
|
||||||
5764DF841B51F254004FF46E /* SocketPacket.swift */,
|
5764DF841B51F254004FF46E /* SocketPacket.swift */,
|
||||||
@ -492,6 +498,7 @@
|
|||||||
74781D5A1B7E83930042CACA /* SocketIOClientStatus.swift in Sources */,
|
74781D5A1B7E83930042CACA /* SocketIOClientStatus.swift in Sources */,
|
||||||
5764DFA11B51F254004FF46E /* WebSocket.swift in Sources */,
|
5764DFA11B51F254004FF46E /* WebSocket.swift in Sources */,
|
||||||
5764DF991B51F254004FF46E /* SocketPacket.swift in Sources */,
|
5764DF991B51F254004FF46E /* SocketPacket.swift in Sources */,
|
||||||
|
740E10441BD2C4680064FC4A /* SocketIOClientOption.swift in Sources */,
|
||||||
74F124E31BC5697B002966F4 /* SocketEngineSpec.swift in Sources */,
|
74F124E31BC5697B002966F4 /* SocketEngineSpec.swift in Sources */,
|
||||||
5764DF891B51F254004FF46E /* SocketAckManager.swift in Sources */,
|
5764DF891B51F254004FF46E /* SocketAckManager.swift in Sources */,
|
||||||
74F124E81BC56BFC002966F4 /* SocketEnginePacketType.swift in Sources */,
|
74F124E81BC56BFC002966F4 /* SocketEnginePacketType.swift in Sources */,
|
||||||
@ -523,6 +530,7 @@
|
|||||||
74781D5B1B7E83930042CACA /* SocketIOClientStatus.swift in Sources */,
|
74781D5B1B7E83930042CACA /* SocketIOClientStatus.swift in Sources */,
|
||||||
945B653B1B5FCEEA0081E995 /* SocketIOClient.swift in Sources */,
|
945B653B1B5FCEEA0081E995 /* SocketIOClient.swift in Sources */,
|
||||||
74F124F01BC574CF002966F4 /* SocketBasicPacketTest.swift in Sources */,
|
74F124F01BC574CF002966F4 /* SocketBasicPacketTest.swift in Sources */,
|
||||||
|
740E10451BD2C4680064FC4A /* SocketIOClientOption.swift in Sources */,
|
||||||
949FAE8D1B9B94E600073BE9 /* SocketParserTest.swift in Sources */,
|
949FAE8D1B9B94E600073BE9 /* SocketParserTest.swift in Sources */,
|
||||||
7472C65C1BCAB53E003CA70D /* SocketNamespacePacketTest.swift in Sources */,
|
7472C65C1BCAB53E003CA70D /* SocketNamespacePacketTest.swift in Sources */,
|
||||||
945B65411B5FCEEA0081E995 /* WebSocket.swift in Sources */,
|
945B65411B5FCEEA0081E995 /* WebSocket.swift in Sources */,
|
||||||
@ -548,6 +556,7 @@
|
|||||||
74781D5C1B7E83930042CACA /* SocketIOClientStatus.swift in Sources */,
|
74781D5C1B7E83930042CACA /* SocketIOClientStatus.swift in Sources */,
|
||||||
5764DFA21B51F254004FF46E /* WebSocket.swift in Sources */,
|
5764DFA21B51F254004FF46E /* WebSocket.swift in Sources */,
|
||||||
5764DF9A1B51F254004FF46E /* SocketPacket.swift in Sources */,
|
5764DF9A1B51F254004FF46E /* SocketPacket.swift in Sources */,
|
||||||
|
740E10461BD2C4680064FC4A /* SocketIOClientOption.swift in Sources */,
|
||||||
74F124E51BC5697B002966F4 /* SocketEngineSpec.swift in Sources */,
|
74F124E51BC5697B002966F4 /* SocketEngineSpec.swift in Sources */,
|
||||||
5764DF8A1B51F254004FF46E /* SocketAckManager.swift in Sources */,
|
5764DF8A1B51F254004FF46E /* SocketAckManager.swift in Sources */,
|
||||||
74F124EA1BC56BFC002966F4 /* SocketEnginePacketType.swift in Sources */,
|
74F124EA1BC56BFC002966F4 /* SocketEnginePacketType.swift in Sources */,
|
||||||
@ -579,6 +588,7 @@
|
|||||||
745895381BB59A0A0050ACC8 /* SocketAckManagerTest.swift in Sources */,
|
745895381BB59A0A0050ACC8 /* SocketAckManagerTest.swift in Sources */,
|
||||||
7458953D1BB59A0A0050ACC8 /* SocketParserTest.swift in Sources */,
|
7458953D1BB59A0A0050ACC8 /* SocketParserTest.swift in Sources */,
|
||||||
74F124F11BC574CF002966F4 /* SocketBasicPacketTest.swift in Sources */,
|
74F124F11BC574CF002966F4 /* SocketBasicPacketTest.swift in Sources */,
|
||||||
|
740E10471BD2C4680064FC4A /* SocketIOClientOption.swift in Sources */,
|
||||||
74F124EB1BC56BFC002966F4 /* SocketEnginePacketType.swift in Sources */,
|
74F124EB1BC56BFC002966F4 /* SocketEnginePacketType.swift in Sources */,
|
||||||
7472C65D1BCAB53E003CA70D /* SocketNamespacePacketTest.swift in Sources */,
|
7472C65D1BCAB53E003CA70D /* SocketNamespacePacketTest.swift in Sources */,
|
||||||
749A7F901BA9D42D00782993 /* SocketAckEmitter.swift in Sources */,
|
749A7F901BA9D42D00782993 /* SocketAckEmitter.swift in Sources */,
|
||||||
|
|||||||
@ -32,7 +32,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
|
|||||||
public private(set) var status = SocketIOClientStatus.NotConnected
|
public private(set) var status = SocketIOClientStatus.NotConnected
|
||||||
|
|
||||||
public var nsp = "/"
|
public var nsp = "/"
|
||||||
public var opts: [String: AnyObject]?
|
public var options: Set<SocketIOClientOption>?
|
||||||
public var reconnects = true
|
public var reconnects = true
|
||||||
public var reconnectWait = 10
|
public var reconnectWait = 10
|
||||||
public var sid: String? {
|
public var sid: String? {
|
||||||
@ -40,9 +40,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private let emitQueue = dispatch_queue_create("com.socketio.emitQueue", DISPATCH_QUEUE_SERIAL)
|
private let emitQueue = dispatch_queue_create("com.socketio.emitQueue", DISPATCH_QUEUE_SERIAL)
|
||||||
private let handleQueue: dispatch_queue_t!
|
|
||||||
private let logType = "SocketIOClient"
|
private let logType = "SocketIOClient"
|
||||||
private let reconnectAttempts: Int!
|
|
||||||
|
|
||||||
private var anyHandler: ((SocketAnyEvent) -> Void)?
|
private var anyHandler: ((SocketAnyEvent) -> Void)?
|
||||||
private var currentReconnectAttempt = 0
|
private var currentReconnectAttempt = 0
|
||||||
@ -52,12 +50,14 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
|
|||||||
private var ackHandlers = SocketAckManager()
|
private var ackHandlers = SocketAckManager()
|
||||||
|
|
||||||
private(set) var currentAck = -1
|
private(set) var currentAck = -1
|
||||||
|
private(set) var handleQueue = dispatch_get_main_queue()
|
||||||
|
private(set) var reconnectAttempts = -1
|
||||||
var waitingData = [SocketPacket]()
|
var waitingData = [SocketPacket]()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Create a new SocketIOClient. opts can be omitted
|
Type safe way to create a new SocketIOClient. opts can be omitted
|
||||||
*/
|
*/
|
||||||
public init(var socketURL: String, opts: [String: AnyObject]? = nil) {
|
public init(var socketURL: String, options: Set<SocketIOClientOption>? = nil) {
|
||||||
if socketURL["https://"].matches().count != 0 {
|
if socketURL["https://"].matches().count != 0 {
|
||||||
self.secure = true
|
self.secure = true
|
||||||
}
|
}
|
||||||
@ -66,47 +66,43 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
|
|||||||
socketURL = socketURL["https://"] ~= ""
|
socketURL = socketURL["https://"] ~= ""
|
||||||
|
|
||||||
self.socketURL = socketURL
|
self.socketURL = socketURL
|
||||||
self.opts = opts
|
self.options = options
|
||||||
|
|
||||||
if let connectParams = opts?["connectParams"] as? [String: AnyObject] {
|
for option in options ?? [] {
|
||||||
self.connectParams = connectParams
|
switch option {
|
||||||
}
|
case .ConnectParams(let params):
|
||||||
|
connectParams = params
|
||||||
if let logger = opts?["logger"] as? SocketLogger {
|
case .Reconnects(let reconnects):
|
||||||
Logger = logger
|
|
||||||
}
|
|
||||||
|
|
||||||
if let log = opts?["log"] as? Bool {
|
|
||||||
Logger.log = log
|
|
||||||
}
|
|
||||||
|
|
||||||
if let nsp = opts?["nsp"] as? String {
|
|
||||||
self.nsp = nsp
|
|
||||||
}
|
|
||||||
|
|
||||||
if let reconnects = opts?["reconnects"] as? Bool {
|
|
||||||
self.reconnects = reconnects
|
self.reconnects = reconnects
|
||||||
|
case .ReconnectAttempts(let attempts):
|
||||||
|
reconnectAttempts = attempts
|
||||||
|
case .ReconnectWait(let wait):
|
||||||
|
reconnectWait = abs(wait)
|
||||||
|
case .Nsp(let nsp):
|
||||||
|
self.nsp = nsp
|
||||||
|
case .Log(let log):
|
||||||
|
Logger.log = log
|
||||||
|
case .Logger(let logger):
|
||||||
|
Logger = logger
|
||||||
|
case .HandleQueue(let queue):
|
||||||
|
handleQueue = queue
|
||||||
|
default:
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if let reconnectAttempts = opts?["reconnectAttempts"] as? Int {
|
|
||||||
self.reconnectAttempts = reconnectAttempts
|
|
||||||
} else {
|
|
||||||
self.reconnectAttempts = -1
|
|
||||||
}
|
|
||||||
|
|
||||||
if let reconnectWait = opts?["reconnectWait"] as? Int {
|
|
||||||
self.reconnectWait = abs(reconnectWait)
|
|
||||||
}
|
|
||||||
|
|
||||||
if let handleQueue = opts?["handleQueue"] as? dispatch_queue_t {
|
|
||||||
self.handleQueue = handleQueue
|
|
||||||
} else {
|
|
||||||
self.handleQueue = dispatch_get_main_queue()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
super.init()
|
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, opts: SocketOptionsDictionary? = nil)`
|
||||||
|
*/
|
||||||
|
public convenience init(socketURL: String, options: NSDictionary?) {
|
||||||
|
self.init(socketURL: socketURL,
|
||||||
|
options: SocketIOClientOption.NSDictionaryToSocketOptionsSet(options ?? [:]))
|
||||||
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
Logger.log("Client is being deinit", type: logType)
|
Logger.log("Client is being deinit", type: logType)
|
||||||
}
|
}
|
||||||
@ -114,7 +110,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient {
|
|||||||
private func addEngine() -> SocketEngine {
|
private func addEngine() -> SocketEngine {
|
||||||
Logger.log("Adding engine", type: logType)
|
Logger.log("Adding engine", type: logType)
|
||||||
|
|
||||||
let newEngine = SocketEngine(client: self, opts: opts)
|
let newEngine = SocketEngine(client: self, opts:
|
||||||
|
SocketIOClientOption.SocketOptionsSetToNSDictionary(options ?? []))
|
||||||
|
|
||||||
engine = newEngine
|
engine = newEngine
|
||||||
return newEngine
|
return newEngine
|
||||||
|
|||||||
119
SocketIOClientSwift/SocketIOClientOption.swift
Normal file
119
SocketIOClientSwift/SocketIOClientOption.swift
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
//
|
||||||
|
// SocketIOClientOption .swift
|
||||||
|
// Socket.IO-Client-Swift
|
||||||
|
//
|
||||||
|
// Created by Erik Little on 10/17/15.
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in
|
||||||
|
// all copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
// THE SOFTWARE.
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
public enum SocketIOClientOption: CustomStringConvertible, Hashable {
|
||||||
|
case ConnectParams([String: AnyObject])
|
||||||
|
case Reconnects(Bool)
|
||||||
|
case ReconnectAttempts(Int)
|
||||||
|
case ReconnectWait(Int)
|
||||||
|
case ForcePolling(Bool)
|
||||||
|
case ForceWebsockets(Bool)
|
||||||
|
case Nsp(String)
|
||||||
|
case Cookies([NSHTTPCookie])
|
||||||
|
case Log(Bool)
|
||||||
|
case Logger(SocketLogger)
|
||||||
|
case SessionDelegate(NSURLSessionDelegate)
|
||||||
|
case Path(String)
|
||||||
|
case ExtraHeaders([String: String])
|
||||||
|
case HandleQueue(dispatch_queue_t)
|
||||||
|
|
||||||
|
public var description: String {
|
||||||
|
if let label = Mirror(reflecting: self).children.first?.label {
|
||||||
|
return String(label[label.startIndex]).lowercaseString + String(label.characters.dropFirst())
|
||||||
|
} else {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public var hashValue: Int {
|
||||||
|
return description.hashValue
|
||||||
|
}
|
||||||
|
|
||||||
|
static func keyValueToSocketIOClientOption(key: String, value: AnyObject) -> SocketIOClientOption? {
|
||||||
|
switch key {
|
||||||
|
case "connectParams" where value is [String: AnyObject]:
|
||||||
|
return .ConnectParams(value as! [String: AnyObject])
|
||||||
|
case "reconnects" where value is Bool:
|
||||||
|
return .Reconnects(value as! Bool)
|
||||||
|
case "reconnectAttempts" where value is Int:
|
||||||
|
return .ReconnectAttempts(value as! Int)
|
||||||
|
case "reconnectWait" where value is Int:
|
||||||
|
return .ReconnectWait(value as! Int)
|
||||||
|
case "forcePolling" where value is Bool:
|
||||||
|
return .ForcePolling(value as! Bool)
|
||||||
|
case "forceWebsockets" where value is Bool:
|
||||||
|
return .ForceWebsockets(value as! Bool)
|
||||||
|
case "nsp" where value is String:
|
||||||
|
return .Nsp(value as! String)
|
||||||
|
case "cookies" where value is [NSHTTPCookie]:
|
||||||
|
return .Cookies(value as! [NSHTTPCookie])
|
||||||
|
case "log" where value is Bool:
|
||||||
|
return .Log(value as! Bool)
|
||||||
|
case "logger" where value is SocketLogger:
|
||||||
|
return .Logger(value as! SocketLogger)
|
||||||
|
case "sessionDelegate" where value is NSURLSessionDelegate:
|
||||||
|
return .SessionDelegate(value as! NSURLSessionDelegate)
|
||||||
|
case "path" where value is String:
|
||||||
|
return .Path(value as! String)
|
||||||
|
case "extraHeaders" where value is [String: String]:
|
||||||
|
return .ExtraHeaders(value as! [String: String])
|
||||||
|
case "handleQueue" where value is dispatch_queue_t:
|
||||||
|
return .HandleQueue(value as! dispatch_queue_t)
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getSocketIOOptionValue() -> AnyObject? {
|
||||||
|
return Mirror(reflecting: self).children.first?.value as? AnyObject
|
||||||
|
}
|
||||||
|
|
||||||
|
static func NSDictionaryToSocketOptionsSet(dict: NSDictionary) -> Set<SocketIOClientOption> {
|
||||||
|
var options = Set<SocketIOClientOption>()
|
||||||
|
|
||||||
|
for (rawKey, value) in dict {
|
||||||
|
if let key = rawKey as? String, opt = keyValueToSocketIOClientOption(key, value: value) {
|
||||||
|
options.insert(opt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
static func SocketOptionsSetToNSDictionary(set: Set<SocketIOClientOption>) -> NSDictionary {
|
||||||
|
let options = NSMutableDictionary()
|
||||||
|
|
||||||
|
for option in set {
|
||||||
|
options[option.description] = option.getSocketIOOptionValue()
|
||||||
|
}
|
||||||
|
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public func ==(lhs: SocketIOClientOption, rhs: SocketIOClientOption) -> Bool {
|
||||||
|
return lhs.description == rhs.description
|
||||||
|
}
|
||||||
@ -26,7 +26,7 @@ import Foundation
|
|||||||
|
|
||||||
var Logger: SocketLogger = DefaultSocketLogger()
|
var Logger: SocketLogger = DefaultSocketLogger()
|
||||||
|
|
||||||
public protocol SocketLogger {
|
public protocol SocketLogger: class {
|
||||||
/// Whether to log or not
|
/// Whether to log or not
|
||||||
var log: Bool {get set}
|
var log: Bool {get set}
|
||||||
|
|
||||||
@ -56,6 +56,6 @@ public extension SocketLogger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DefaultSocketLogger: SocketLogger {
|
class DefaultSocketLogger: SocketLogger {
|
||||||
var log = false
|
var log = false
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user