Skip to content

Commit

Permalink
fix race condition in polling
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearace committed Mar 14, 2015
1 parent 2cc68ae commit bb6bd2b
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions SwiftIO/SocketEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public class SocketEngine: NSObject, WebSocketDelegate {
let postData = postStr.dataUsingEncoding(NSUTF8StringEncoding,
allowLossyConversion: false)!


// println("posting: \(postStr)")
req.setValue(String(postData.length), forHTTPHeaderField: "Content-Length")
req.HTTPBody = postData

Expand Down Expand Up @@ -406,7 +406,7 @@ public class SocketEngine: NSObject, WebSocketDelegate {
}

private func parseEngineMessage(var message:String) {
// println(message!)
// println("Engine got message: \(message)")

var strMessage = RegexMutable(message)

Expand All @@ -429,7 +429,10 @@ public class SocketEngine: NSObject, WebSocketDelegate {
if let data = NSData(base64EncodedString: message,
options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters) {
// println("sending \(data)")
self.client.parseBinaryData(data)
dispatch_async(self.client.handleQueue) {[weak self] in
self?.client.parseBinaryData(data)
return
}
}

return
Expand All @@ -448,9 +451,11 @@ public class SocketEngine: NSObject, WebSocketDelegate {

// Remove message type
message.removeAtIndex(message.startIndex)
// println("sending \(messageString)")

self.client.parseSocketMessage(message)
dispatch_async(self.client.handleQueue) {[weak self] in
self?.client.parseSocketMessage(message)
return
}
}

private func probeWebSocket() {
Expand Down Expand Up @@ -512,7 +517,9 @@ public class SocketEngine: NSObject, WebSocketDelegate {
}
}

self.flushWaitingForPost()
if !self.waitingForPost {
self.flushWaitingForPost()
}
}

private func sendWebSocketMessage(str:String, withType type:PacketType, datas:[NSData]? = nil) {
Expand Down

0 comments on commit bb6bd2b

Please sign in to comment.