|
1 |
| -Socket.IO-Client-Swift |
2 |
| -====================== |
| 1 | +#Socket.IO-Client-Swift |
| 2 | +Socket.IO-client for iOS/OS X. |
3 | 3 |
|
4 |
| -Socket.IO-client for Swift. Supports ws/wss/polling connections and binary. For socket.io 1.0+ and Swift 1.1. |
| 4 | +##Example |
| 5 | +```swift |
| 6 | +let socket = SocketIOClient(socketURL: "localhost:8080") |
| 7 | + |
| 8 | +socket.on("connect") {data, ack in |
| 9 | + println("socket connected") |
| 10 | +} |
| 11 | + |
| 12 | +socket.on("currentAmount") {data, ack in |
| 13 | + if let cur = data?[0] as? Double { |
| 14 | + socket.emitWithAck("canUpdate", cur)(timeout: 0) {data in |
| 15 | + socket.emit("update", ["amount": cur + 2.50]) |
| 16 | + } |
| 17 | + |
| 18 | + ack?("Got your currentAmount", "dude") |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +// Connect |
| 23 | +socket.connect() |
| 24 | +``` |
| 25 | + |
| 26 | +##Objective-C Example |
| 27 | +```objective-c |
| 28 | +SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" options:nil]; |
| 29 | + |
| 30 | +[socket on: @"connect" callback: ^(NSArray* data, void (^ack)(NSArray*)) { |
| 31 | + NSLog(@"connected"); |
| 32 | + [socket emitObjc:@"echo" withItems:@[@"echo test"]]; |
| 33 | + [socket emitWithAckObjc:@"ackack" withItems:@[@1]](10, ^(NSArray* data) { |
| 34 | + NSLog(@"Got ack"); |
| 35 | + }); |
| 36 | +}]; |
5 | 37 |
|
6 |
| -For Swift 1.2 use the 1.2 branch. |
| 38 | +[socket connect]; |
7 | 39 |
|
8 |
| -Installation |
9 |
| -============ |
10 |
| -[](https://github.com/Carthage/Carthage) |
| 40 | +``` |
| 41 | +
|
| 42 | +##Features |
| 43 | +- Supports socket.io 1.0+ |
| 44 | +- Supports binary |
| 45 | +- Supports Polling and WebSockets |
| 46 | +- Supports TLS/SSL |
| 47 | +- Can be used from Objective-C |
11 | 48 |
|
| 49 | +##Installation |
12 | 50 | Manually (iOS 7+)
|
13 | 51 | -----------------
|
14 | 52 | 1. Copy the SwiftIO folder into your Xcode project!
|
@@ -37,127 +75,45 @@ Import in your swift file:
|
37 | 75 | import Socket_IO_Client_Swift
|
38 | 76 | ```
|
39 | 77 |
|
40 |
| -Carthage |
41 |
| --------------- |
42 |
| -Add this line to your Cartfile |
43 |
| -``` |
44 |
| -github "SocketIO/Socket.IO-client-swift" >= 1.4.2 |
45 |
| -``` |
46 |
| - |
47 |
| -In your project directory |
48 |
| -```bash |
49 |
| -$ carthage update |
50 |
| -``` |
51 |
| - |
52 |
| -Add the `SocketIO.framework` from Carthage/Build/iOS or Carthage/Build/OSX to your project |
53 |
| - |
54 |
| -API |
55 |
| -=== |
| 78 | +##API |
56 | 79 | Constructors
|
57 | 80 | -----------
|
58 |
| -`init(socketURL: String, opts:NSDictionary? = nil)` - Constructs a new client for the given URL. opts can be omitted (will use default values. See example) |
| 81 | +`init(socketURL: String, opts:NSDictionary? = nil)` - Constructs a new client for the given URL. opts can be omitted (will use default values) |
59 | 82 |
|
60 | 83 | `convenience init(socketURL: String, options:NSDictionary?)` - Same as above, but meant for Objective-C. See Objective-C Example.
|
| 84 | + |
| 85 | +Options |
| 86 | +------- |
| 87 | +- `reconnects: Bool` Default is `true` |
| 88 | +- `reconnectAttempts: Int` Default is `-1` (infinite tries) |
| 89 | +- `reconnectWait: Int` Default is `10` |
| 90 | +- `forcePolling: Bool` Default is `false`. `true` forces the client to use xhr-polling. |
| 91 | +- `forceWebsockets: Bool` Default is `false`. `true` forces the client to use WebSockets. |
| 92 | +- `nsp: String` Default is `"/"` |
| 93 | +- `cookies: [NSHTTPCookie]?` An array of NSHTTPCookies. Passed during the handshake. Default is nil. |
| 94 | + |
61 | 95 | Methods
|
62 | 96 | -------
|
63 |
| -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. |
64 |
| -2. `socket.onAny(callback:((event:String, items:AnyObject?)) -> Void)` - Adds a handler for all events. It will be called on any received event. |
65 |
| -3. `socket.emit(event:String, _ items:AnyObject...)` - Sends a message. Can send multiple items. |
66 |
| -4. `socket.emitObjc(event:String, withItems items:[AnyObject])` - `emit` for Objective-C |
67 |
| -5. `socket.emitWithAck(event:String, _ items:AnyObject...) -> SocketAckHandler` - Sends a message that requests an acknowledgement from the server. Returns a SocketAckHandler which you can use to add an onAck handler. See example. |
68 |
| -6. `socket.emitWithAckObjc(event:String, withItems items:[AnyObject]) -> SocketAckHandler` - `emitWithAck` for Objective-C. |
69 |
| -7. `socket.connect()` - Establishes a connection to the server. A "connect" event is fired upon successful connection. |
70 |
| -8. `socket.connectWithParams(params:[String: AnyObject])` - Establishes a connection to the server passing the specified params. A "connect" event is fired upon successful connection. |
71 |
| -9. `socket.close(#fast:Bool)` - Closes the socket. Once a socket is closed it should not be reopened. Pass true to fast if you're closing from a background task. |
| 97 | +1. `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. |
| 98 | +2. `onAny(callback:((event:String, items:AnyObject?)) -> Void)` - Adds a handler for all events. It will be called on any received event. |
| 99 | +3. `emit(event:String, _ items:AnyObject...)` - Sends a message. Can send multiple items. |
| 100 | +4. `emitObjc(event:String, withItems items:[AnyObject])` - `emit` for Objective-C |
| 101 | +5. `emitWithAck(event:String, _ items:AnyObject...) -> (timeout:UInt64, callback:(NSArray?) -> Void) -> Void` - Sends a message that requests an acknowledgement from the server. Returns a function which you can use to add a handler. See example. Note: The message is not sent until you call the returned function. |
| 102 | +6. `emitWithAckObjc(event:String, withItems items:[AnyObject]) -> (UInt64, (NSArray?) -> Void) -> Void` - `emitWithAck` for Objective-C. Note: The message is not sent until you call the returned function. |
| 103 | +7. `connect()` - Establishes a connection to the server. A "connect" event is fired upon successful connection. |
| 104 | +8. `connectWithParams(params:[String: AnyObject])` - Establishes a connection to the server passing the specified params. A "connect" event is fired upon successful connection. |
| 105 | +9. `close(#fast:Bool)` - Closes the socket. Once a socket is closed it should not be reopened. Pass true to fast if you're closing from a background task. |
72 | 106 |
|
73 | 107 | Events
|
74 | 108 | ------
|
75 | 109 | 1. `connect` - Emitted when on a successful connection.
|
76 | 110 | 2. `disconnect` - Emitted when the connection is closed.
|
77 |
| -3. `error` - Emitted if the websocket encounters an error. |
| 111 | +3. `error` - Emitted on an error. |
78 | 112 | 4. `reconnect` - Emitted when the connection is starting to reconnect.
|
79 | 113 | 5. `reconnectAttempt` - Emitted when attempting to reconnect.
|
80 | 114 |
|
81 |
| -Example |
82 |
| -======= |
83 |
| -```swift |
84 |
| -// opts can be omitted, will use default values |
85 |
| -let socket = SocketIOClient(socketURL: "https://localhost:8080", opts: [ |
86 |
| - "reconnects": true, // Default is true |
87 |
| - "reconnectAttempts": 5, // Default is -1 (infinite tries) |
88 |
| - "reconnectWait": 5, // Default is 10 |
89 |
| - "nsp": "swift", // connects to the specified namespace. Default is / |
90 |
| - "forcePolling": true, // if true, the socket will only use XHR polling, Default is false (polling/WebSockets) |
91 |
| - "cookies": nil // An array of NSHTTPCookies. Passed during handshake. Default is nil |
92 |
| -]) |
93 |
| - |
94 |
| -// Called on every event |
95 |
| -socket.onAny {println("got event: \($0.event) with items \($0.items)")} |
96 |
| - |
97 |
| -// Socket Events |
98 |
| -socket.on("connect") {data, ack in |
99 |
| - println("socket connected") |
100 |
| - |
101 |
| - // Sending messages |
102 |
| - socket.emit("testEcho") |
103 |
| - |
104 |
| - socket.emit("testObject", [ |
105 |
| - "data": true |
106 |
| - ]) |
107 |
| - |
108 |
| - // Sending multiple items per message |
109 |
| - socket.emit("multTest", [1], 1.4, 1, "true", |
110 |
| - true, ["test": "foo"], "bar") |
111 |
| -} |
112 |
| - |
113 |
| -// Requesting acks, and responding to acks |
114 |
| -socket.on("ackEvent") {data, ack in |
115 |
| - if let str = data?[0] as? String { |
116 |
| - println("Got ackEvent") |
117 |
| - } |
118 |
| - |
119 |
| - // data is an array |
120 |
| - if let int = data?[1] as? Int { |
121 |
| - println("Got int") |
122 |
| - } |
123 |
| - |
124 |
| - // You can specify a custom timeout interval. 0 means no timeout. |
125 |
| - socket.emitWithAck("ackTest", "test").onAck(0) {data in |
126 |
| - println(data?[0]) |
127 |
| - } |
128 |
| - |
129 |
| - ack?("Got your event", "dude") |
130 |
| -} |
131 |
| - |
132 |
| -socket.on("jsonTest") {data, ack in |
133 |
| - if let json = data?[0] as? NSDictionary { |
134 |
| - println(json["test"]!) // foo bar |
135 |
| - } |
136 |
| -} |
137 |
| - |
138 |
| -// Connecting |
139 |
| -socket.connect() |
140 |
| -``` |
141 |
| - |
142 |
| -Objective-C Example |
143 |
| -=================== |
144 |
| -```objective-c |
145 |
| -SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" options:nil]; |
146 |
| - |
147 |
| -[socket on: @"connect" callback: ^(NSArray* data, void (^ack)(NSArray*)) { |
148 |
| - NSLog(@"connected"); |
149 |
| - [socket emitObjc:@"echo" withItems:@[@"echo test"]]; |
150 |
| - [[socket emitWithAckObjc:@"ackack" withItems:@[@"test"]] onAck:0 withCallback:^(NSArray* data) { |
151 |
| - NSLog(@"Got data"); |
152 |
| - }]; |
153 |
| -}]; |
154 |
| - |
155 |
| -``` |
156 |
| -
|
157 |
| -Detailed Example |
158 |
| -================ |
| 115 | +##Detailed Example |
159 | 116 | A more detailed example can be found [here](https://github.com/nuclearace/socket.io-client-swift-example)
|
160 | 117 |
|
161 |
| -License |
162 |
| -======= |
| 118 | +##License |
163 | 119 | MIT
|
0 commit comments