From f7b064d80f5f1b8866313e14c7b2badae1805c8b Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 1 Apr 2015 14:49:01 -0400 Subject: [PATCH 1/7] refactor SocketEngine#sendPing --- SwiftIO/SocketEngine.swift | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/SwiftIO/SocketEngine.swift b/SwiftIO/SocketEngine.swift index d28674a..2e66584 100644 --- a/SwiftIO/SocketEngine.swift +++ b/SwiftIO/SocketEngine.swift @@ -514,11 +514,7 @@ public class SocketEngine: NSObject, WebSocketDelegate { } func sendPing() { - if self.websocket { - self.sendWebSocketMessage("", withType: PacketType.PING) - } else { - self.sendPollMessage("", withType: PacketType.PING) - } + self.write("", withType: PacketType.PING, withData: nil) } private func sendPollMessage(var msg:String, withType type:PacketType, From 060060f5c20677eb63a67d9b78dca232f6c5ce5f Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 1 Apr 2015 15:02:25 -0400 Subject: [PATCH 2/7] work on readme --- README.md | 125 ++++++++++++++++++++---------------------------------- 1 file changed, 46 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index 0a520c2..51fed88 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,53 @@ Socket.IO-Client-Swift ====================== -Socket.IO-client for Swift. Supports ws/wss/polling connections and binary. For socket.io 1.0+ and Swift 1.1. +Socket.IO-client for Swift. -For Swift 1.2 use the 1.2 branch. +Example +======= +```swift +let socket = SocketIOClient(socketURL: "localhost:8080") + +socket.on("connect") {data, ack in + println("socket connected") +} + +socket.on("currentAmount") {data, ack in + if let cur = data?[0] as? Double { + socket.emitWithAck("canUpdate", cur).onAck(0) {data in + socket.emit("update", ["amount": cur + 2.50]) + } + + ack?("Got your currentAmount", "dude") + } +} + +// Connect +socket.connect() +``` + +Objective-C Example +=================== +```objective-c +SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" options:nil]; + +[socket on: @"connect" callback: ^(NSArray* data, void (^ack)(NSArray*)) { + NSLog(@"connected"); + [socket emitObjc:@"echo" withItems:@[@"echo test"]]; + [[socket emitWithAckObjc:@"ackack" withItems:@[@"test"]] onAck:0 withCallback:^(NSArray* data) { + NSLog(@"Got data"); + }]; +}]; + +``` + +Features +======== +- Supports socket.io 1.0+ +- Supports binary +- Supports Polling and WebSockets +- Supports TLS/SSL +- Can be used from Objective-C Installation ============ @@ -63,83 +107,6 @@ Events 4. `reconnect` - Emitted when the connection is starting to reconnect. 5. `reconnectAttempt` - Emitted when attempting to reconnect. -Example -======= -```swift -// opts can be omitted, will use default values -let socket = SocketIOClient(socketURL: "https://localhost:8080", opts: [ - "reconnects": true, // Default is true - "reconnectAttempts": 5, // Default is -1 (infinite tries) - "reconnectWait": 5, // Default is 10 - "nsp": "swift", // connects to the specified namespace. Default is / - "forcePolling": true, // if true the client will only use XHR polling, Default is false (polling/WebSockets) - "forceWebsockets": false, // if true the client will only use WebSockets. Trumps forcePolling. Default is false. (polling/WebSockets) - "cookies": nil // An array of NSHTTPCookies. Passed during handshake. Default is nil -]) - -// Called on every event -socket.onAny {println("got event: \($0.event) with items \($0.items)")} - -// Socket Events -socket.on("connect") {data, ack in - println("socket connected") - - // Sending messages - socket.emit("testEcho") - - socket.emit("testObject", [ - "data": true - ]) - - // Sending multiple items per message - socket.emit("multTest", [1], 1.4, 1, "true", - true, ["test": "foo"], "bar") -} - -// Requesting acks, and responding to acks -socket.on("ackEvent") {data, ack in - if let str = data?[0] as? String { - println("Got ackEvent") - } - - // data is an array - if let int = data?[1] as? Int { - println("Got int") - } - - // You can specify a custom timeout interval. 0 means no timeout. - socket.emitWithAck("ackTest", "test").onAck(0) {data in - println(data?[0]) - } - - ack?("Got your event", "dude") -} - -socket.on("jsonTest") {data, ack in - if let json = data?[0] as? NSDictionary { - println(json["test"]!) // foo bar - } -} - -// Connecting -socket.connect() -``` - -Objective-C Example -=================== -```objective-c -SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" options:nil]; - -[socket on: @"connect" callback: ^(NSArray* data, void (^ack)(NSArray*)) { - NSLog(@"connected"); - [socket emitObjc:@"echo" withItems:@[@"echo test"]]; - [[socket emitWithAckObjc:@"ackack" withItems:@[@"test"]] onAck:0 withCallback:^(NSArray* data) { - NSLog(@"Got data"); - }]; -}]; - -``` - Detailed Example ================ A more detailed example can be found [here](https://github.com/nuclearace/socket.io-client-swift-example) From e90ecb81be58b003a206b30485181cfc9332f345 Mon Sep 17 00:00:00 2001 From: Erik Little Date: Wed, 1 Apr 2015 15:08:26 -0400 Subject: [PATCH 3/7] Update README.md --- README.md | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 51fed88..3d733a9 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,7 @@ -Socket.IO-Client-Swift -====================== - +#Socket.IO-Client-Swift Socket.IO-client for Swift. -Example -======= +##Example ```swift let socket = SocketIOClient(socketURL: "localhost:8080") @@ -26,8 +23,7 @@ socket.on("currentAmount") {data, ack in socket.connect() ``` -Objective-C Example -=================== +##Objective-C Example ```objective-c SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" options:nil]; @@ -41,17 +37,14 @@ SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8 ``` -Features -======== +##Features - Supports socket.io 1.0+ - Supports binary - Supports Polling and WebSockets - Supports TLS/SSL - Can be used from Objective-C -Installation -============ - +##Installation Manually (iOS 7+) ----------------- 1. Copy the SwiftIO folder into your Xcode project! @@ -80,8 +73,7 @@ Import in your swift file: import Socket_IO_Client_Swift ``` -API -=== +##API Constructors ----------- `init(socketURL: String, opts:NSDictionary? = nil)` - Constructs a new client for the given URL. opts can be omitted (will use default values. See example) @@ -107,10 +99,8 @@ Events 4. `reconnect` - Emitted when the connection is starting to reconnect. 5. `reconnectAttempt` - Emitted when attempting to reconnect. -Detailed Example -================ +##Detailed Example A more detailed example can be found [here](https://github.com/nuclearace/socket.io-client-swift-example) -License -======= +##License MIT From dd28b1747ca467262f0d61b847d056d46afc99e9 Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 1 Apr 2015 15:42:23 -0400 Subject: [PATCH 4/7] add connection options --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3d733a9..15852f6 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,17 @@ import Socket_IO_Client_Swift ##API Constructors ----------- -`init(socketURL: String, opts:NSDictionary? = nil)` - Constructs a new client for the given URL. opts can be omitted (will use default values. See example) +`init(socketURL: String, opts:NSDictionary? = nil)` - Constructs a new client for the given URL. opts can be omitted (will use default values) + +Options +------- +- `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 `"/"` +- `cookies: [NSHTTPCookie]?` An array of NSHTTPCookies. Passed during the handshake. Default is nil. `convenience init(socketURL: String, options:NSDictionary?)` - Same as above, but meant for Objective-C. See Objective-C Example. Methods From 58d2693910c61b6a6601ff7c915b62ac0388456c Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 1 Apr 2015 15:43:46 -0400 Subject: [PATCH 5/7] Fix readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 15852f6..b5597d2 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ import Socket_IO_Client_Swift Constructors ----------- `init(socketURL: String, opts:NSDictionary? = nil)` - Constructs a new client for the given URL. opts can be omitted (will use default values) +`convenience init(socketURL: String, options:NSDictionary?)` - Same as above, but meant for Objective-C. See Objective-C Example. Options ------- @@ -88,7 +89,6 @@ Options - `nsp: String` Default is `"/"` - `cookies: [NSHTTPCookie]?` An array of NSHTTPCookies. Passed during the handshake. Default is nil. -`convenience init(socketURL: String, options:NSDictionary?)` - Same as above, but meant for Objective-C. See Objective-C Example. Methods ------- 1. `socket.on(name:String, callback:((data:NSArray?, ack:AckEmitter?) -> Void))` - 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. From 7641ffef4104aa6964ee062e365eb7716c2ade41 Mon Sep 17 00:00:00 2001 From: Erik Little Date: Wed, 1 Apr 2015 15:44:19 -0400 Subject: [PATCH 6/7] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b5597d2..2b83816 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ import Socket_IO_Client_Swift Constructors ----------- `init(socketURL: String, opts:NSDictionary? = nil)` - Constructs a new client for the given URL. opts can be omitted (will use default values) + `convenience init(socketURL: String, options:NSDictionary?)` - Same as above, but meant for Objective-C. See Objective-C Example. Options From 2952120fc1b940ac19afcdfb1f82ffd243756841 Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 1 Apr 2015 15:49:06 -0400 Subject: [PATCH 7/7] tweak readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b83816..b5ef8be 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ #Socket.IO-Client-Swift -Socket.IO-client for Swift. +Socket.IO-client for iOS/OS X. ##Example ```swift