Skip to content

Commit ffd92d7

Browse files
committed
Merge branch 'timeouts'
2 parents c103734 + 2022f28 commit ffd92d7

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ socket.on("ackEvent") {data, ack in
103103
println("Got int")
104104
}
105105

106-
socket.emitWithAck("ackTest", "test").onAck {data in
106+
// You can specify a custom timeout interval. 0 means no timeout.
107+
socket.emitWithAck("ackTest", "test").onAck(0) {data in
107108
println(data?[0])
108109
}
109110

SwiftIO/SocketAckHandler.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,35 @@ public typealias AckCallback = (NSArray?) -> Void
2929
@objc public class SocketAckHandler {
3030
let ackNum:Int!
3131
let event:String!
32+
var acked = false
3233
var callback:AckCallback?
3334

3435
init(event:String, ackNum:Int = 0) {
3536
self.ackNum = ackNum
3637
self.event = event
3738
}
3839

39-
func onAck(callback:AckCallback) {
40+
public func onAck(timeout:UInt64, withCallback callback:AckCallback) {
4041
self.callback = callback
42+
43+
44+
if timeout != 0 {
45+
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeout * NSEC_PER_SEC))
46+
dispatch_after(time, dispatch_get_main_queue()) {[weak self] in
47+
if self == nil {
48+
return
49+
}
50+
51+
if !self!.acked {
52+
self?.executeAck(["No ACK"])
53+
}
54+
}
55+
}
4156
}
4257

4358
func executeAck(data:NSArray?) {
44-
dispatch_async(dispatch_get_main_queue()) {[cb = self.callback] in
59+
dispatch_async(dispatch_get_main_queue()) {[weak self, cb = self.callback] in
60+
self?.acked = true
4561
cb?(data)
4662
return
4763
}

0 commit comments

Comments
 (0)