Skip to content

Commit 82dadaa

Browse files
committed
Merge master
2 parents b04329f + 2a322ff commit 82dadaa

15 files changed

+695
-612
lines changed

README.md

Lines changed: 70 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,52 @@
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)(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+
}];
537

6-
For Swift 1.2 use the 1.2 branch.
38+
[socket connect];
739

8-
Installation
9-
============
10-
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](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
1148
49+
##Installation
1250
Manually (iOS 7+)
1351
-----------------
1452
1. Copy the SwiftIO folder into your Xcode project!
@@ -37,127 +75,45 @@ Import in your swift file:
3775
import Socket_IO_Client_Swift
3876
```
3977

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
5679
Constructors
5780
-----------
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)
5982

6083
`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+
6195
Methods
6296
-------
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.
72106

73107
Events
74108
------
75109
1. `connect` - Emitted when on a successful connection.
76110
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.
78112
4. `reconnect` - Emitted when the connection is starting to reconnect.
79113
5. `reconnectAttempt` - Emitted when attempting to reconnect.
80114

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
159116
A more detailed example can be found [here](https://github.com/nuclearace/socket.io-client-swift-example)
160117

161-
License
162-
=======
118+
##License
163119
MIT

Socket.IO-Client-Swift.podspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "Socket.IO-Client-Swift"
3-
s.version = "1.4.0"
3+
s.version = "2.0.1"
44
s.summary = "Socket.IO-client for Swift"
55
s.description = <<-DESC
66
Socket.IO-client for Swift.
@@ -12,8 +12,8 @@ Pod::Spec.new do |s|
1212
s.author = { "Erik" => "nuclear.ace@gmail.com" }
1313
s.ios.deployment_target = '8.0'
1414
s.osx.deployment_target = '10.10'
15-
s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v1.4.0' }
16-
s.source_files = "SocketIO/**/*.swift"
15+
s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v2.0.1' }
16+
s.source_files = "SwiftIO/**/*.swift"
1717
s.requires_arc = true
1818
# s.dependency 'Starscream', '~> 0.9' # currently this repo includes Starscream swift files
1919
end

SocketIO/SocketAckHandler.swift

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)