Skip to content

Commit 2910355

Browse files
committed
Merge branch 'development'
* development: update readme Renamed modules to make then consistent. rename engine init tweaks Add SocketIOClientConfiguration Rename variable refactor some reconnect code bump websocket version move extensions refactor out checking for base64
2 parents 6147cda + 5e1dc53 commit 2910355

19 files changed

+461
-318
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ 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+
import SocketIO
9+
10+
let socket = SocketIOClient(socketURL: NSURL(string: "http://localhost:8080")!, config: [.Log(true), .ForcePolling(true)])
911

1012
socket.on("connect") {data, ack in
1113
print("socket connected")
@@ -26,8 +28,9 @@ socket.connect()
2628

2729
##Objective-C Example
2830
```objective-c
31+
@import SocketIO;
2932
NSURL* url = [[NSURL alloc] initWithString:@"http://localhost:8080"];
30-
SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:url options:@{@"log": @YES, @"forcePolling": @YES}];
33+
SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:url config:@{@"log": @YES, @"forcePolling": @YES}];
3134

3235
[socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
3336
NSLog(@"socket connected");
@@ -115,13 +118,13 @@ Import the module:
115118

116119
Swift:
117120
```swift
118-
import SocketIOClientSwift
121+
import SocketIO
119122
```
120123

121124
Objective-C:
122125

123126
```Objective-C
124-
@import SocketIOClientSwift;
127+
@import SocketIO;
125128
```
126129

127130
CocoaSeeds
@@ -139,7 +142,7 @@ Run `seed install`.
139142
##API
140143
Constructors
141144
-----------
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.
145+
`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.
143146

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

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

Lines changed: 80 additions & 90 deletions
Large diffs are not rendered by default.

SocketIO-MacTests/SocketAckManagerTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
import XCTest
10-
@testable import SocketIOClientSwift
10+
@testable import SocketIO
1111

1212
class SocketAckManagerTest: XCTestCase {
1313
var ackManager = SocketAckManager()

SocketIO-MacTests/SocketBasicPacketTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
import XCTest
10-
@testable import SocketIOClientSwift
10+
@testable import SocketIO
1111

1212
class SocketBasicPacketTest: XCTestCase {
1313
let data = "test".dataUsingEncoding(NSUTF8StringEncoding)!

SocketIO-MacTests/SocketEngineTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
import XCTest
10-
@testable import SocketIOClientSwift
10+
@testable import SocketIO
1111

1212
class SocketEngineTest: XCTestCase {
1313
var client: SocketIOClient!
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 SocketIO
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)]
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/SocketNamespacePacketTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
import XCTest
10-
@testable import SocketIOClientSwift
10+
@testable import SocketIO
1111

1212
class SocketNamespacePacketTest: XCTestCase {
1313
let data = "test".dataUsingEncoding(NSUTF8StringEncoding)!

SocketIO-MacTests/SocketObjectiveCTest.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//
99

1010
#import <XCTest/XCTest.h>
11-
@import SocketIOClientSwift;
11+
@import SocketIO;
1212

1313
@interface SocketObjectiveCTest : XCTestCase
1414

@@ -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 {

SocketIO-MacTests/SocketParserTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
import XCTest
10-
@testable import SocketIOClientSwift
10+
@testable import SocketIO
1111

1212
class SocketParserTest: XCTestCase {
1313
let testSocket = SocketIOClient(socketURL: NSURL())

SocketIO-MacTests/SocketSideEffectTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
import XCTest
10-
@testable import SocketIOClientSwift
10+
@testable import SocketIO
1111

1212
class SocketSideEffectTest: XCTestCase {
1313
let data = "test".dataUsingEncoding(NSUTF8StringEncoding)!
File renamed without changes.

Source/SSLSecurity.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ public class SSLSecurity : NSObject {
8888
- returns: a representation security object to be used with
8989
*/
9090
public init(certs: [SSLCert], usePublicKeys: Bool) {
91-
super.init()
92-
9391
self.usePublicKeys = usePublicKeys
9492

93+
super.init()
94+
9595
if self.usePublicKeys {
9696
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)) {
9797
let pubKeys = certs.reduce([SecKeyRef]()) { (pubKeys: [SecKeyRef], cert: SSLCert) -> [SecKeyRef] in

Source/SocketEngine.swift

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ 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, config: SocketIOClientConfiguration) {
8484
self.client = client
8585
self.url = url
8686

87-
for option in options {
87+
for option in config {
8888
switch option {
8989
case let .ConnectParams(params):
9090
connectParams = params
@@ -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, config: options?.toSocketConfiguration() ?? [])
131131
}
132132

133133
deinit {
@@ -153,18 +153,12 @@ public final class SocketEngine : NSObject, NSURLSessionDelegate, SocketEnginePo
153153
}
154154
}
155155

156-
private func checkIfMessageIsBase64Binary(message: String) -> Bool {
157-
if message.hasPrefix("b4") {
158-
// binary in base64 string
159-
let noPrefix = message[message.startIndex.advancedBy(2)..<message.endIndex]
160-
161-
if let data = NSData(base64EncodedString: noPrefix, options: .IgnoreUnknownCharacters) {
162-
client?.parseEngineBinaryData(data)
163-
}
164-
165-
return true
166-
} else {
167-
return false
156+
private func handleBase64(message: String) {
157+
// binary in base64 string
158+
let noPrefix = message[message.startIndex.advancedBy(2)..<message.endIndex]
159+
160+
if let data = NSData(base64EncodedString: noPrefix, options: .IgnoreUnknownCharacters) {
161+
client?.parseEngineBinaryData(data)
168162
}
169163
}
170164

@@ -265,7 +259,7 @@ public final class SocketEngine : NSObject, NSURLSessionDelegate, SocketEnginePo
265259
}
266260
}
267261

268-
ws?.queue = handleQueue
262+
ws?.callbackQueue = handleQueue
269263
ws?.voipEnabled = voipEnabled
270264
ws?.delegate = self
271265
ws?.selfSignedSSL = selfSigned
@@ -419,11 +413,13 @@ public final class SocketEngine : NSObject, NSURLSessionDelegate, SocketEnginePo
419413

420414
let reader = SocketStringReader(message: message)
421415
let fixedString: String
416+
417+
if message.hasPrefix("b4") {
418+
return handleBase64(message)
419+
}
422420

423421
guard let type = SocketEnginePacketType(rawValue: Int(reader.currentCharacter) ?? -1) else {
424-
if !checkIfMessageIsBase64Binary(message) {
425-
checkAndHandleEngineError(message)
426-
}
422+
checkAndHandleEngineError(message)
427423

428424
return
429425
}

Source/SocketExtensions.swift

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,72 @@ extension Array where Element: AnyObject {
3636
}
3737

3838
extension NSCharacterSet {
39-
class var allowedURLCharacterSet: NSCharacterSet {
39+
static var allowedURLCharacterSet: NSCharacterSet {
4040
return NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" {}").invertedSet
4141
}
4242
}
4343

44+
extension NSDictionary {
45+
private static func keyValueToSocketIOClientOption(key: String, value: AnyObject) -> SocketIOClientOption? {
46+
switch (key, value) {
47+
case let ("connectParams", params as [String: AnyObject]):
48+
return .ConnectParams(params)
49+
case let ("cookies", cookies as [NSHTTPCookie]):
50+
return .Cookies(cookies)
51+
case let ("doubleEncodeUTF8", encode as Bool):
52+
return .DoubleEncodeUTF8(encode)
53+
case let ("extraHeaders", headers as [String: String]):
54+
return .ExtraHeaders(headers)
55+
case let ("forceNew", force as Bool):
56+
return .ForceNew(force)
57+
case let ("forcePolling", force as Bool):
58+
return .ForcePolling(force)
59+
case let ("forceWebsockets", force as Bool):
60+
return .ForceWebsockets(force)
61+
case let ("handleQueue", queue as dispatch_queue_t):
62+
return .HandleQueue(queue)
63+
case let ("log", log as Bool):
64+
return .Log(log)
65+
case let ("logger", logger as SocketLogger):
66+
return .Logger(logger)
67+
case let ("nsp", nsp as String):
68+
return .Nsp(nsp)
69+
case let ("path", path as String):
70+
return .Path(path)
71+
case let ("reconnects", reconnects as Bool):
72+
return .Reconnects(reconnects)
73+
case let ("reconnectAttempts", attempts as Int):
74+
return .ReconnectAttempts(attempts)
75+
case let ("reconnectWait", wait as Int):
76+
return .ReconnectWait(wait)
77+
case let ("secure", secure as Bool):
78+
return .Secure(secure)
79+
case let ("security", security as SSLSecurity):
80+
return .Security(security)
81+
case let ("selfSigned", selfSigned as Bool):
82+
return .SelfSigned(selfSigned)
83+
case let ("sessionDelegate", delegate as NSURLSessionDelegate):
84+
return .SessionDelegate(delegate)
85+
case let ("voipEnabled", enable as Bool):
86+
return .VoipEnabled(enable)
87+
default:
88+
return nil
89+
}
90+
}
91+
92+
func toSocketConfiguration() -> SocketIOClientConfiguration {
93+
var options = [] as SocketIOClientConfiguration
94+
95+
for (rawKey, value) in self {
96+
if let key = rawKey as? String, opt = NSDictionary.keyValueToSocketIOClientOption(key, value: value) {
97+
options.insert(opt)
98+
}
99+
}
100+
101+
return options
102+
}
103+
}
104+
44105
extension String {
45106
func toArray() throws -> [AnyObject] {
46107
guard let stringData = dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) else { return [] }

0 commit comments

Comments
 (0)