Skip to content

Commit 90bbc95

Browse files
committed
work on #70
1 parent 7a1a0d9 commit 90bbc95

File tree

1 file changed

+30
-35
lines changed

1 file changed

+30
-35
lines changed

SocketIOClientSwift/SocketIOClient.swift

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -195,21 +195,19 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
195195

196196
private func createOnAck(event:String, items:[AnyObject]) -> OnAckCallback {
197197
return {[weak self, ack = ++self.currentAck] timeout, callback in
198-
if self == nil {
199-
return
200-
}
201-
202-
self?.ackHandlers.addAck(ack, callback: callback)
203-
204-
dispatch_async(self!.emitQueue) {
205-
self?._emit(event, items, ack: ack)
206-
}
207-
208-
if timeout != 0 {
209-
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeout * NSEC_PER_SEC))
198+
if let this = self {
199+
this.ackHandlers.addAck(ack, callback: callback)
210200

211-
dispatch_after(time, dispatch_get_main_queue()) {
212-
self?.ackHandlers.timeoutAck(ack)
201+
dispatch_async(this.emitQueue) {[weak this] in
202+
this?._emit(event, items, ack: ack)
203+
}
204+
205+
if timeout != 0 {
206+
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeout * NSEC_PER_SEC))
207+
208+
dispatch_after(time, dispatch_get_main_queue()) {[weak this] in
209+
this?.ackHandlers.timeoutAck(ack)
210+
}
213211
}
214212
}
215213
}
@@ -337,22 +335,21 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
337335
// If the server wants to know that the client received data
338336
func emitAck(ack:Int, withData args:[AnyObject]) {
339337
dispatch_async(self.emitQueue) {[weak self] in
340-
if self == nil || !self!.connected {
341-
return
342-
}
343-
344-
let packet = SocketPacket(type: nil, data: args, nsp: self!.nsp, id: ack)
345-
let str:String
346-
347-
SocketParser.parseForEmit(packet)
348-
str = packet.createAck()
349-
350-
SocketLogger.log("Emitting Ack: \(str)", client: self!)
351-
352-
if packet.type == SocketPacket.PacketType.BINARY_ACK {
353-
self?.engine?.send(str, withData: packet.binary)
354-
} else {
355-
self?.engine?.send(str, withData: nil)
338+
if let this = self where this.connected {
339+
let packet = SocketPacket(type: nil, data: args, nsp: this.nsp, id: ack)
340+
let str:String
341+
342+
SocketParser.parseForEmit(packet)
343+
str = packet.createAck()
344+
345+
SocketLogger.log("Emitting Ack: \(str)", client: this)
346+
347+
if packet.type == SocketPacket.PacketType.BINARY_ACK {
348+
this.engine?.send(str, withData: packet.binary)
349+
} else {
350+
this.engine?.send(str, withData: nil)
351+
}
352+
356353
}
357354
}
358355
}
@@ -484,12 +481,10 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
484481
self._reconnecting = true
485482

486483
dispatch_async(dispatch_get_main_queue()) {[weak self] in
487-
if self == nil {
488-
return
484+
if let this = self {
485+
this.reconnectTimer = NSTimer.scheduledTimerWithTimeInterval(Double(this.reconnectWait),
486+
target: this, selector: "tryReconnect", userInfo: nil, repeats: true)
489487
}
490-
491-
self?.reconnectTimer = NSTimer.scheduledTimerWithTimeInterval(Double(self!.reconnectWait),
492-
target: self!, selector: "tryReconnect", userInfo: nil, repeats: true)
493488
}
494489
}
495490

0 commit comments

Comments
 (0)