Skip to content

Commit 38163d2

Browse files
committed
Merge pull request #40 from socketio/betterreadme
Betterreadme
2 parents 01a699c + 2952120 commit 38163d2

File tree

2 files changed

+59
-95
lines changed

2 files changed

+59
-95
lines changed

README.md

Lines changed: 58 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,50 @@
1-
Socket.IO-Client-Swift
2-
======================
1+
#Socket.IO-Client-Swift
2+
Socket.IO-client for iOS/OS X.
33

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).onAck(0) {data in
15+
socket.emit("update", ["amount": cur + 2.50])
16+
}
17+
18+
ack?("Got your currentAmount", "dude")
19+
}
20+
}
521

6-
For Swift 1.2 use the 1.2 branch.
22+
// Connect
23+
socket.connect()
24+
```
725

8-
Installation
9-
============
26+
##Objective-C Example
27+
```objective-c
28+
SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" options:nil];
1029

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:@[@"test"]] onAck:0 withCallback:^(NSArray* data) {
34+
NSLog(@"Got data");
35+
}];
36+
}];
37+
38+
```
39+
40+
##Features
41+
- Supports socket.io 1.0+
42+
- Supports binary
43+
- Supports Polling and WebSockets
44+
- Supports TLS/SSL
45+
- Can be used from Objective-C
46+
47+
##Installation
1148
Manually (iOS 7+)
1249
-----------------
1350
1. Copy the SwiftIO folder into your Xcode project!
@@ -36,13 +73,23 @@ Import in your swift file:
3673
import Socket_IO_Client_Swift
3774
```
3875

39-
API
40-
===
76+
##API
4177
Constructors
4278
-----------
43-
`init(socketURL: String, opts:NSDictionary? = nil)` - Constructs a new client for the given URL. opts can be omitted (will use default values. See example)
79+
`init(socketURL: String, opts:NSDictionary? = nil)` - Constructs a new client for the given URL. opts can be omitted (will use default values)
4480

4581
`convenience init(socketURL: String, options:NSDictionary?)` - Same as above, but meant for Objective-C. See Objective-C Example.
82+
83+
Options
84+
-------
85+
- `reconnects: Bool` Default is `true`
86+
- `reconnectAttempts: Int` Default is `-1` (infinite tries)
87+
- `reconnectWait: Int` Default is `10`
88+
- `forcePolling: Bool` Default is `false`. `true` forces the client to use xhr-polling.
89+
- `forceWebsockets: Bool` Default is `false`. `true` forces the client to use WebSockets.
90+
- `nsp: String` Default is `"/"`
91+
- `cookies: [NSHTTPCookie]?` An array of NSHTTPCookies. Passed during the handshake. Default is nil.
92+
4693
Methods
4794
-------
4895
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.
@@ -63,87 +110,8 @@ Events
63110
4. `reconnect` - Emitted when the connection is starting to reconnect.
64111
5. `reconnectAttempt` - Emitted when attempting to reconnect.
65112

66-
Example
67-
=======
68-
```swift
69-
// opts can be omitted, will use default values
70-
let socket = SocketIOClient(socketURL: "https://localhost:8080", opts: [
71-
"reconnects": true, // Default is true
72-
"reconnectAttempts": 5, // Default is -1 (infinite tries)
73-
"reconnectWait": 5, // Default is 10
74-
"nsp": "swift", // connects to the specified namespace. Default is /
75-
"forcePolling": true, // if true the client will only use XHR polling, Default is false (polling/WebSockets)
76-
"forceWebsockets": false, // if true the client will only use WebSockets. Trumps forcePolling. Default is false. (polling/WebSockets)
77-
"cookies": nil // An array of NSHTTPCookies. Passed during handshake. Default is nil
78-
])
79-
80-
// Called on every event
81-
socket.onAny {println("got event: \($0.event) with items \($0.items)")}
82-
83-
// Socket Events
84-
socket.on("connect") {data, ack in
85-
println("socket connected")
86-
87-
// Sending messages
88-
socket.emit("testEcho")
89-
90-
socket.emit("testObject", [
91-
"data": true
92-
])
93-
94-
// Sending multiple items per message
95-
socket.emit("multTest", [1], 1.4, 1, "true",
96-
true, ["test": "foo"], "bar")
97-
}
98-
99-
// Requesting acks, and responding to acks
100-
socket.on("ackEvent") {data, ack in
101-
if let str = data?[0] as? String {
102-
println("Got ackEvent")
103-
}
104-
105-
// data is an array
106-
if let int = data?[1] as? Int {
107-
println("Got int")
108-
}
109-
110-
// You can specify a custom timeout interval. 0 means no timeout.
111-
socket.emitWithAck("ackTest", "test").onAck(0) {data in
112-
println(data?[0])
113-
}
114-
115-
ack?("Got your event", "dude")
116-
}
117-
118-
socket.on("jsonTest") {data, ack in
119-
if let json = data?[0] as? NSDictionary {
120-
println(json["test"]!) // foo bar
121-
}
122-
}
123-
124-
// Connecting
125-
socket.connect()
126-
```
127-
128-
Objective-C Example
129-
===================
130-
```objective-c
131-
SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:@"localhost:8080" options:nil];
132-
133-
[socket on: @"connect" callback: ^(NSArray* data, void (^ack)(NSArray*)) {
134-
NSLog(@"connected");
135-
[socket emitObjc:@"echo" withItems:@[@"echo test"]];
136-
[[socket emitWithAckObjc:@"ackack" withItems:@[@"test"]] onAck:0 withCallback:^(NSArray* data) {
137-
NSLog(@"Got data");
138-
}];
139-
}];
140-
141-
```
142-
143-
Detailed Example
144-
================
113+
##Detailed Example
145114
A more detailed example can be found [here](https://github.com/nuclearace/socket.io-client-swift-example)
146115

147-
License
148-
=======
116+
##License
149117
MIT

SwiftIO/SocketEngine.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,11 +514,7 @@ public class SocketEngine: NSObject, WebSocketDelegate {
514514
}
515515

516516
func sendPing() {
517-
if self.websocket {
518-
self.sendWebSocketMessage("", withType: PacketType.PING)
519-
} else {
520-
self.sendPollMessage("", withType: PacketType.PING)
521-
}
517+
self.write("", withType: PacketType.PING, withData: nil)
522518
}
523519

524520
private func sendPollMessage(var msg:String, withType type:PacketType,

0 commit comments

Comments
 (0)