Skip to content

Commit e2bf84f

Browse files
committed
Add SocketIOClientConfiguration
1 parent 437c983 commit e2bf84f

File tree

9 files changed

+184
-31
lines changed

9 files changed

+184
-31
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Socket.IO-client for iOS/OS X.
55

66
##Example
77
```swift
8-
let socket = SocketIOClient(socketURL: NSURL(string: "http://localhost:8080")!, options: [.Log(true), .ForcePolling(true)])
8+
let socket = SocketIOClient(socketURL: NSURL(string: "http://localhost:8080")!, config: [.Log(true), .ForcePolling(true)])
99

1010
socket.on("connect") {data, ack in
1111
print("socket connected")
@@ -27,7 +27,7 @@ socket.connect()
2727
##Objective-C Example
2828
```objective-c
2929
NSURL* url = [[NSURL alloc] initWithString:@"http://localhost:8080"];
30-
SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:url options:@{@"log": @YES, @"forcePolling": @YES}];
30+
SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:url config:@{@"log": @YES, @"forcePolling": @YES}];
3131

3232
[socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
3333
NSLog(@"socket connected");
@@ -139,7 +139,7 @@ Run `seed install`.
139139
##API
140140
Constructors
141141
-----------
142-
`init(var socketURL: NSURL, options: Set<SocketIOClientOption> = [])` - Creates a new SocketIOClient. options is a Set of SocketIOClientOption. If your socket.io server is secure, you need to specify `https` in your socketURL.
142+
`init(var socketURL: NSURL, config: SocketIOClientConfiguration = [])` - Creates a new SocketIOClient. options is a Set of SocketIOClientOption. If your socket.io server is secure, you need to specify `https` in your socketURL.
143143

144144
`convenience init(socketURL: NSURL, options: NSDictionary?)` - Same as above, but meant for Objective-C. See Options on how convert between SocketIOClientOptions and dictionary keys.
145145

Socket.IO-Client-Swift.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@
115115
7472C65D1BCAB53E003CA70D /* SocketNamespacePacketTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7472C65B1BCAB53E003CA70D /* SocketNamespacePacketTest.swift */; };
116116
7472C65F1BCAC46E003CA70D /* SocketSideEffectTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7472C65E1BCAC46E003CA70D /* SocketSideEffectTest.swift */; };
117117
7472C6601BCAC46E003CA70D /* SocketSideEffectTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7472C65E1BCAC46E003CA70D /* SocketSideEffectTest.swift */; };
118+
747BC5991D5F943500CA5FA4 /* SocketIOClientConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 747BC5981D5F943500CA5FA4 /* SocketIOClientConfiguration.swift */; };
119+
747BC59A1D5F943500CA5FA4 /* SocketIOClientConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 747BC5981D5F943500CA5FA4 /* SocketIOClientConfiguration.swift */; };
120+
747BC59B1D5F943500CA5FA4 /* SocketIOClientConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 747BC5981D5F943500CA5FA4 /* SocketIOClientConfiguration.swift */; };
121+
747BC59F1D5F9BA200CA5FA4 /* SocketIOClientConfigurationTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 747BC59E1D5F9BA200CA5FA4 /* SocketIOClientConfigurationTest.swift */; };
118122
749642B51D3FCE5500DD32D1 /* SSLSecurity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 749642B31D3FCE5500DD32D1 /* SSLSecurity.swift */; };
119123
749642B61D3FCE5500DD32D1 /* SSLSecurity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 749642B31D3FCE5500DD32D1 /* SSLSecurity.swift */; };
120124
749642B71D3FCE5500DD32D1 /* SSLSecurity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 749642B31D3FCE5500DD32D1 /* SSLSecurity.swift */; };
@@ -192,6 +196,8 @@
192196
74321DCA1C2D939A00CF6F43 /* SocketParserTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocketParserTest.swift; sourceTree = "<group>"; };
193197
7472C65B1BCAB53E003CA70D /* SocketNamespacePacketTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketNamespacePacketTest.swift; sourceTree = "<group>"; };
194198
7472C65E1BCAC46E003CA70D /* SocketSideEffectTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketSideEffectTest.swift; sourceTree = "<group>"; };
199+
747BC5981D5F943500CA5FA4 /* SocketIOClientConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketIOClientConfiguration.swift; path = Source/SocketIOClientConfiguration.swift; sourceTree = "<group>"; };
200+
747BC59E1D5F9BA200CA5FA4 /* SocketIOClientConfigurationTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketIOClientConfigurationTest.swift; sourceTree = "<group>"; };
195201
749642B31D3FCE5500DD32D1 /* SSLSecurity.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SSLSecurity.swift; path = Source/SSLSecurity.swift; sourceTree = "<group>"; };
196202
749642B41D3FCE5500DD32D1 /* WebSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WebSocket.swift; path = Source/WebSocket.swift; sourceTree = "<group>"; };
197203
74ABF7761C3991C10078C657 /* SocketIOClientSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketIOClientSpec.swift; path = Source/SocketIOClientSpec.swift; sourceTree = "<group>"; };
@@ -314,6 +320,7 @@
314320
74F124EF1BC574CF002966F4 /* SocketBasicPacketTest.swift */,
315321
741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */,
316322
7472C65B1BCAB53E003CA70D /* SocketNamespacePacketTest.swift */,
323+
747BC59E1D5F9BA200CA5FA4 /* SocketIOClientConfigurationTest.swift */,
317324
742D150B1CA5794B00BD987D /* SocketObjectiveCTest.m */,
318325
74321DCA1C2D939A00CF6F43 /* SocketParserTest.swift */,
319326
7472C65E1BCAC46E003CA70D /* SocketSideEffectTest.swift */,
@@ -346,6 +353,7 @@
346353
74171E571C10CD240062D398 /* SocketEventHandler.swift */,
347354
CEBA56991CDA0B8200BA0389 /* SocketExtensions.swift */,
348355
74171E591C10CD240062D398 /* SocketIOClient.swift */,
356+
747BC5981D5F943500CA5FA4 /* SocketIOClientConfiguration.swift */,
349357
74171E5A1C10CD240062D398 /* SocketIOClientOption.swift */,
350358
74ABF7761C3991C10078C657 /* SocketIOClientSpec.swift */,
351359
74171E5B1C10CD240062D398 /* SocketIOClientStatus.swift */,
@@ -622,6 +630,7 @@
622630
74171EB71C10CD240062D398 /* SocketParsable.swift in Sources */,
623631
74171E811C10CD240062D398 /* SocketEnginePacketType.swift in Sources */,
624632
74171E6F1C10CD240062D398 /* SocketAnyEvent.swift in Sources */,
633+
747BC5991D5F943500CA5FA4 /* SocketIOClientConfiguration.swift in Sources */,
625634
74171E9F1C10CD240062D398 /* SocketIOClientOption.swift in Sources */,
626635
74BC45AB1D0C6675008CC431 /* SocketClientManager.swift in Sources */,
627636
);
@@ -679,6 +688,7 @@
679688
74171EB91C10CD240062D398 /* SocketParsable.swift in Sources */,
680689
74171E831C10CD240062D398 /* SocketEnginePacketType.swift in Sources */,
681690
74171E711C10CD240062D398 /* SocketAnyEvent.swift in Sources */,
691+
747BC59A1D5F943500CA5FA4 /* SocketIOClientConfiguration.swift in Sources */,
682692
74171EA11C10CD240062D398 /* SocketIOClientOption.swift in Sources */,
683693
74BC45AC1D0C6675008CC431 /* SocketClientManager.swift in Sources */,
684694
);
@@ -691,6 +701,7 @@
691701
742D150C1CA5794B00BD987D /* SocketObjectiveCTest.m in Sources */,
692702
74321DCB1C2D939A00CF6F43 /* SocketAckManagerTest.swift in Sources */,
693703
74321DCC1C2D939A00CF6F43 /* SocketParserTest.swift in Sources */,
704+
747BC59F1D5F9BA200CA5FA4 /* SocketIOClientConfigurationTest.swift in Sources */,
694705
7472C6601BCAC46E003CA70D /* SocketSideEffectTest.swift in Sources */,
695706
74171EA81C10CD240062D398 /* SocketIOClientStatus.swift in Sources */,
696707
741F39EF1BD025D80026C9CC /* SocketEngineTest.swift in Sources */,
@@ -724,6 +735,7 @@
724735
74171EBB1C10CD240062D398 /* SocketParsable.swift in Sources */,
725736
74171E851C10CD240062D398 /* SocketEnginePacketType.swift in Sources */,
726737
74171E731C10CD240062D398 /* SocketAnyEvent.swift in Sources */,
738+
747BC59B1D5F943500CA5FA4 /* SocketIOClientConfiguration.swift in Sources */,
727739
74171EA31C10CD240062D398 /* SocketIOClientOption.swift in Sources */,
728740
74BC45AD1D0C6675008CC431 /* SocketClientManager.swift in Sources */,
729741
);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// TestSocketIOClientConfiguration.swift
3+
// Socket.IO-Client-Swift
4+
//
5+
// Created by Erik Little on 8/13/16.
6+
//
7+
//
8+
9+
import XCTest
10+
import SocketIOClientSwift
11+
12+
class TestSocketIOClientConfiguration: XCTestCase {
13+
var config = [] as SocketIOClientConfiguration
14+
15+
override func setUp() {
16+
super.setUp()
17+
18+
config = [.Log(false), .ForceNew(true)] as SocketIOClientConfiguration
19+
}
20+
21+
func testReplaceSameOption() {
22+
config.insert(.Log(true))
23+
24+
XCTAssertEqual(config.count, 2)
25+
26+
switch config[0] {
27+
case let .Log(log):
28+
XCTAssertTrue(log)
29+
default:
30+
XCTFail()
31+
}
32+
}
33+
34+
func testIgnoreIfExisting() {
35+
config.insert(.ForceNew(false), replacing: false)
36+
37+
XCTAssertEqual(config.count, 2)
38+
39+
switch config[1] {
40+
case let .ForceNew(new):
41+
XCTAssertTrue(new)
42+
default:
43+
XCTFail()
44+
}
45+
}
46+
}

SocketIO-MacTests/SocketObjectiveCTest.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ @implementation SocketObjectiveCTest
2121
- (void)setUp {
2222
[super setUp];
2323
NSURL* url = [[NSURL alloc] initWithString:@"http://localhost"];
24-
self.socket = [[SocketIOClient alloc] initWithSocketURL:url options:nil];
24+
self.socket = [[SocketIOClient alloc] initWithSocketURL:url config:nil];
2525
}
2626

2727
- (void)testOnSyntax {

Source/SocketEngine.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public final class SocketEngine : NSObject, NSURLSessionDelegate, SocketEnginePo
8080
private var selfSigned = false
8181
private var voipEnabled = false
8282

83-
public init(client: SocketEngineClient, url: NSURL, options: Set<SocketIOClientOption>) {
83+
public init(client: SocketEngineClient, url: NSURL, options: SocketIOClientConfiguration) {
8484
self.client = client
8585
self.url = url
8686

@@ -127,7 +127,7 @@ public final class SocketEngine : NSObject, NSURLSessionDelegate, SocketEnginePo
127127
}
128128

129129
public convenience init(client: SocketEngineClient, url: NSURL, options: NSDictionary?) {
130-
self.init(client: client, url: url, options: options?.toSocketOptionsSet() ?? [])
130+
self.init(client: client, url: url, options: options?.toSocketConfiguration() ?? [])
131131
}
132132

133133
deinit {

Source/SocketExtensions.swift

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,19 @@ extension NSDictionary {
8989
}
9090
}
9191

92-
func toSocketOptionsSet() -> Set<SocketIOClientOption> {
93-
var options = Set<SocketIOClientOption>()
92+
func toSocketConfiguration() -> SocketIOClientConfiguration {
93+
var options = [] as SocketIOClientConfiguration
9494

9595
for (rawKey, value) in self {
9696
if let key = rawKey as? String, opt = NSDictionary.keyValueToSocketIOClientOption(key, value: value) {
97-
options.insertIgnore(opt)
97+
options.insert(opt)
9898
}
9999
}
100100

101101
return options
102102
}
103103
}
104104

105-
extension Set where Element : ClientOption {
106-
mutating func insertIgnore(element: Element) {
107-
if !contains(element) {
108-
insert(element)
109-
}
110-
}
111-
}
112-
113105
extension String {
114106
func toArray() throws -> [AnyObject] {
115107
guard let stringData = dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) else { return [] }

Source/SocketIOClient.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable
4242

4343
public var forceNew = false
4444
public var nsp = "/"
45-
public var options: Set<SocketIOClientOption>
45+
public var config: SocketIOClientConfiguration
4646
public var reconnects = true
4747
public var reconnectWait = 10
4848
public var sid: String? {
@@ -67,15 +67,15 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable
6767
var waitingPackets = [SocketPacket]()
6868

6969
/// Type safe way to create a new SocketIOClient. opts can be omitted
70-
public init(socketURL: NSURL, options: Set<SocketIOClientOption> = []) {
71-
self.options = options
70+
public init(socketURL: NSURL, config: SocketIOClientConfiguration = []) {
71+
self.config = config
7272
self.socketURL = socketURL
7373

7474
if socketURL.absoluteString.hasPrefix("https://") {
75-
self.options.insertIgnore(.Secure(true))
75+
self.config.insert(.Secure(true))
7676
}
7777

78-
for option in options {
78+
for option in config {
7979
switch option {
8080
case let .Reconnects(reconnects):
8181
self.reconnects = reconnects
@@ -98,15 +98,15 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable
9898
}
9999
}
100100

101-
self.options.insertIgnore(.Path("/socket.io/"))
101+
self.config.insert(.Path("/socket.io/"), replacing: false)
102102

103103
super.init()
104104
}
105105

106106
/// Not so type safe way to create a SocketIOClient, meant for Objective-C compatiblity.
107107
/// If using Swift it's recommended to use `init(socketURL: NSURL, options: Set<SocketIOClientOption>)`
108-
public convenience init(socketURL: NSURL, options: NSDictionary?) {
109-
self.init(socketURL: socketURL, options: options?.toSocketOptionsSet() ?? [])
108+
public convenience init(socketURL: NSURL, config: NSDictionary?) {
109+
self.init(socketURL: socketURL, config: config?.toSocketConfiguration() ?? [])
110110
}
111111

112112
deinit {
@@ -117,7 +117,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable
117117
private func addEngine() -> SocketEngineSpec {
118118
DefaultSocketLogger.Logger.log("Adding engine", type: logType)
119119

120-
engine = SocketEngine(client: self, url: socketURL, options: options)
120+
engine = SocketEngine(client: self, url: socketURL, options: config)
121121

122122
return engine!
123123
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// SocketIOClientConfiguration.swift
3+
// Socket.IO-Client-Swift
4+
//
5+
// Created by Erik Little on 8/13/16.
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in
15+
// all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
// THE SOFTWARE.
24+
25+
public struct SocketIOClientConfiguration : ArrayLiteralConvertible, CollectionType, MutableCollectionType {
26+
public typealias Element = SocketIOClientOption
27+
public typealias Index = Array<SocketIOClientOption>.Index
28+
public typealias Generator = Array<SocketIOClientOption>.Generator
29+
public typealias SubSequence = Array<SocketIOClientOption>.SubSequence
30+
31+
private var backingArray = [SocketIOClientOption]()
32+
33+
public var startIndex: Index {
34+
return backingArray.startIndex
35+
}
36+
37+
public var endIndex: Index {
38+
return backingArray.endIndex
39+
}
40+
41+
public var isEmpty: Bool {
42+
return backingArray.isEmpty
43+
}
44+
45+
public var count: Index.Distance {
46+
return backingArray.count
47+
}
48+
49+
public var first: Generator.Element? {
50+
return backingArray.first
51+
}
52+
53+
public subscript(position: Index) -> Generator.Element {
54+
get {
55+
return backingArray[position]
56+
}
57+
58+
set {
59+
backingArray[position] = newValue
60+
}
61+
}
62+
63+
public subscript(bounds: Range<Index>) -> SubSequence {
64+
get {
65+
return backingArray[bounds]
66+
}
67+
68+
set {
69+
backingArray[bounds] = newValue
70+
}
71+
}
72+
73+
public init(arrayLiteral elements: Element...) {
74+
backingArray = elements
75+
}
76+
77+
public func generate() -> Generator {
78+
return backingArray.generate()
79+
}
80+
81+
public mutating func insert(element: Element, replacing replace: Bool = true) {
82+
for i in 0..<backingArray.count where backingArray[i] == element {
83+
guard replace else { return }
84+
85+
backingArray[i] = element
86+
87+
return
88+
}
89+
90+
backingArray.append(element)
91+
}
92+
93+
@warn_unused_result
94+
public func prefixUpTo(end: Index) -> SubSequence {
95+
return backingArray.prefixUpTo(end)
96+
}
97+
98+
@warn_unused_result
99+
public func prefixThrough(position: Index) -> SubSequence {
100+
return prefixThrough(position)
101+
}
102+
103+
@warn_unused_result
104+
public func suffixFrom(start: Index) -> SubSequence {
105+
return backingArray.suffixFrom(start)
106+
}
107+
}

Source/SocketIOClientOption.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import Foundation
2626

27-
protocol ClientOption : CustomStringConvertible, Hashable {
27+
protocol ClientOption : CustomStringConvertible, Equatable {
2828
func getSocketIOOptionValue() -> AnyObject
2929
}
3030

@@ -99,10 +99,6 @@ public enum SocketIOClientOption : ClientOption {
9999
return description
100100
}
101101

102-
public var hashValue: Int {
103-
return description.hashValue
104-
}
105-
106102
func getSocketIOOptionValue() -> AnyObject {
107103
let value: AnyObject
108104

0 commit comments

Comments
 (0)