Skip to content

Commit a94b7ff

Browse files
committed
Merge pull request #16 from nuclearace/polling
I've tested as much as I can. Open an issue if you find something break.
2 parents 761623a + 6399df4 commit a94b7ff

File tree

6 files changed

+833
-273
lines changed

6 files changed

+833
-273
lines changed

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Socket.IO-Client-Swift
22
======================
33

4-
Socket.IO-client for Swift. Supports ws/wss connections and binary. For socket.io 1.0+ and Swift 1.1.
4+
Socket.IO-client for Swift. Supports ws/wss/polling connections and binary. For socket.io 1.0+ and Swift 1.1.
55

66
For Swift 1.2 use the 1.2 branch.
77

@@ -15,12 +15,13 @@ API
1515
===
1616
Constructor
1717
-----------
18-
`init(socketURL: String, opts:[String: AnyObject]? = nil)` - Constructs a new client for the given URL. opts can be omitted (will use default values.)
18+
`init(socketURL: String, opts:[String: AnyObject]? = nil)` - Constructs a new client for the given URL. opts can be omitted (will use default values. See example)
1919
Methods
2020
-------
2121
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.
22+
2. `socket.onAny(callback:((event:String, items:AnyObject?)) -> Void)` - Adds a handler for all events. It will be called on any received event.
2223
3. `socket.emit(event:String, args:AnyObject...)` - Sends a message. Can send multiple args.
23-
4. `socket.emitWithAck(event:String, args:AnyObject...) -> SocketAckHandler` - Sends a message that requests an acknoweldgement from the server. Returns a SocketAckHandler which you can use to add an onAck handler. See example.
24+
4. `socket.emitWithAck(event:String, args: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.
2425
5. `socket.connect()` - Establishes a connection to the server. A "connect" event is fired upon successful connection.
2526
6. `socket.connectWithParams(params:[String: AnyObject])` - Establishes a connection to the server passing the specified params. A "connect" event is fired upon successful connection.
2627
7. `socket.close()` - Closes the socket. Once a socket is closed it should not be reopened.
@@ -41,9 +42,13 @@ let socket = SocketIOClient(socketURL: "https://localhost:8080", opts: [
4142
"reconnects": true, // default true
4243
"reconnectAttempts": 5, // default -1 (infinite tries)
4344
"reconnectWait": 5, // default 10
44-
"nsp": "swift" // connects to the specified namespace. Default is /
45+
"nsp": "swift", // connects to the specified namespace. Default is /
46+
"forcePolling": true // if true, the socket will only use XHR polling, default is false (polling/WebSockets)
4547
])
4648

49+
// Called on every event
50+
socket.onAny {println("got event: \($0.event) with items \($0.items)")}
51+
4752
// Socket Events
4853
socket.on("connect") {data, ack in
4954
println("socket connected")
@@ -67,7 +72,7 @@ socket.on("ackEvent") {data, ack in
6772
}
6873

6974
socket.emitWithAck("ackTest", "test").onAck {data in
70-
println(data)
75+
println(data?[0])
7176
}
7277

7378
ack?("Got your event", "dude")

SwiftIO/SocketAckHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import Foundation
2626

27-
typealias AckCallback = (AnyObject?) -> Void
27+
typealias AckCallback = (NSArray?) -> Void
2828

2929
class SocketAckHandler {
3030
let ackNum:Int!

0 commit comments

Comments
 (0)