This commit is contained in:
parent
6cb372e3b9
commit
7a1a0d91f9
@ -240,33 +240,32 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
SocketLogger.log("Doing polling request", client: self)
|
SocketLogger.log("Doing polling request", client: self)
|
||||||
|
|
||||||
self.session.dataTaskWithRequest(req) {[weak self] data, res, err in
|
self.session.dataTaskWithRequest(req) {[weak self] data, res, err in
|
||||||
if self == nil {
|
if let this = self {
|
||||||
return
|
if err != nil {
|
||||||
} else if err != nil {
|
if this.polling {
|
||||||
if self!.polling {
|
this.handlePollingFailed(err.localizedDescription)
|
||||||
self?.handlePollingFailed(err.localizedDescription)
|
|
||||||
} else {
|
} else {
|
||||||
NSLog(err.localizedDescription)
|
NSLog(err.localizedDescription)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketLogger.log("Got polling response", client: self!)
|
SocketLogger.log("Got polling response", client: this)
|
||||||
|
|
||||||
if let str = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
|
if let str = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
|
||||||
dispatch_async(self!.parseQueue) {[weak self] in
|
dispatch_async(this.parseQueue) {[weak this] in
|
||||||
self?.parsePollingMessage(str)
|
this?.parsePollingMessage(str)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self?.waitingForPoll = false
|
this.waitingForPoll = false
|
||||||
|
|
||||||
if self!.fastUpgrade {
|
if this.fastUpgrade {
|
||||||
self?.doFastUpgrade()
|
this.doFastUpgrade()
|
||||||
return
|
return
|
||||||
} else if !self!.closed && self!.polling {
|
} else if !this.closed && this.polling {
|
||||||
self?.doPoll()
|
this.doPoll()
|
||||||
|
}
|
||||||
}}.resume()
|
}}.resume()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -274,18 +273,16 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
SocketLogger.log("Flushing probe wait", client: self)
|
SocketLogger.log("Flushing probe wait", client: self)
|
||||||
|
|
||||||
dispatch_async(self.emitQueue) {[weak self] in
|
dispatch_async(self.emitQueue) {[weak self] in
|
||||||
if self == nil {
|
if let this = self {
|
||||||
return
|
for waiter in this.probeWait {
|
||||||
|
this.write(waiter.msg, withType: waiter.type, withData: waiter.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
for waiter in self!.probeWait {
|
this.probeWait.removeAll(keepCapacity: false)
|
||||||
self?.write(waiter.msg, withType: waiter.type, withData: waiter.data)
|
|
||||||
|
if this.postWait.count != 0 {
|
||||||
|
this.flushWaitingForPostToWebSocket()
|
||||||
}
|
}
|
||||||
|
|
||||||
self?.probeWait.removeAll(keepCapacity: false)
|
|
||||||
|
|
||||||
if self?.postWait.count != 0 {
|
|
||||||
self?.flushWaitingForPostToWebSocket()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -329,21 +326,22 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
SocketLogger.log("POSTing: \(postStr)", client: self)
|
SocketLogger.log("POSTing: \(postStr)", client: self)
|
||||||
|
|
||||||
self.session.dataTaskWithRequest(req) {[weak self] data, res, err in
|
self.session.dataTaskWithRequest(req) {[weak self] data, res, err in
|
||||||
if self == nil {
|
if let this = self {
|
||||||
return
|
if err != nil && this.polling {
|
||||||
} else if err != nil && self!.polling {
|
this.handlePollingFailed(err.localizedDescription)
|
||||||
self?.handlePollingFailed(err.localizedDescription)
|
|
||||||
return
|
return
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
NSLog(err.localizedDescription)
|
NSLog(err.localizedDescription)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
self?.waitingForPost = false
|
this.waitingForPost = false
|
||||||
dispatch_async(self!.emitQueue) {[weak self] in
|
|
||||||
if !self!.fastUpgrade {
|
dispatch_async(this.emitQueue) {[weak this] in
|
||||||
self?.flushWaitingForPost()
|
if !(this?.fastUpgrade ?? true) {
|
||||||
self?.doPoll()
|
this?.flushWaitingForPost()
|
||||||
|
this?.doPoll()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}}.resume()
|
}}.resume()
|
||||||
}
|
}
|
||||||
@ -468,14 +466,12 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func parseEngineData(data:NSData) {
|
private func parseEngineData(data:NSData) {
|
||||||
if self.client == nil {
|
if let client = self.client {
|
||||||
return
|
dispatch_async(client.handleQueue) {[weak self] in
|
||||||
}
|
|
||||||
|
|
||||||
dispatch_async(self.client!.handleQueue) {[weak self] in
|
|
||||||
self?.client?.parseBinaryData(data.subdataWithRange(NSMakeRange(1, data.length - 1)))
|
self?.client?.parseBinaryData(data.subdataWithRange(NSMakeRange(1, data.length - 1)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private func parseEngineMessage(var message:String, fromPolling:Bool) {
|
private func parseEngineMessage(var message:String, fromPolling:Bool) {
|
||||||
SocketLogger.log("Got message: \(message)", client: self)
|
SocketLogger.log("Got message: \(message)", client: self)
|
||||||
@ -490,13 +486,11 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
// Remove message type
|
// Remove message type
|
||||||
message.removeAtIndex(message.startIndex)
|
message.removeAtIndex(message.startIndex)
|
||||||
|
|
||||||
if self.client == nil {
|
if let client = self.client {
|
||||||
return
|
dispatch_async(client.handleQueue) {[weak self] in
|
||||||
}
|
|
||||||
|
|
||||||
dispatch_async(self.client!.handleQueue) {[weak self] in
|
|
||||||
self?.client?.parseSocketMessage(message)
|
self?.client?.parseSocketMessage(message)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else if type == PacketType.NOOP {
|
} else if type == PacketType.NOOP {
|
||||||
self.doPoll()
|
self.doPoll()
|
||||||
return
|
return
|
||||||
@ -555,11 +549,8 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
end: advance(message.startIndex, 2)))
|
end: advance(message.startIndex, 2)))
|
||||||
|
|
||||||
if let data = NSData(base64EncodedString: message,
|
if let data = NSData(base64EncodedString: message,
|
||||||
options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
|
options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters), client = self.client {
|
||||||
where self.client != nil {
|
dispatch_async(client.handleQueue) {[weak self] in
|
||||||
// println("sending \(data)")
|
|
||||||
|
|
||||||
dispatch_async(self.client!.handleQueue) {[weak self] in
|
|
||||||
self?.client?.parseBinaryData(data)
|
self?.client?.parseBinaryData(data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -635,15 +626,13 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
|
|
||||||
self.pingTimer?.invalidate()
|
self.pingTimer?.invalidate()
|
||||||
dispatch_async(dispatch_get_main_queue()) {[weak self] in
|
dispatch_async(dispatch_get_main_queue()) {[weak self] in
|
||||||
if self == nil {
|
if let this = self {
|
||||||
return
|
this.pingTimer = NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(this.pingInterval!),
|
||||||
}
|
target: this,
|
||||||
|
|
||||||
self?.pingTimer = NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(self!.pingInterval!),
|
|
||||||
target: self!,
|
|
||||||
selector: Selector("sendPing"), userInfo: nil, repeats: true)
|
selector: Selector("sendPing"), userInfo: nil, repeats: true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func stopPolling() {
|
func stopPolling() {
|
||||||
self.session.invalidateAndCancel()
|
self.session.invalidateAndCancel()
|
||||||
@ -665,16 +654,14 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
*/
|
*/
|
||||||
public func write(msg:String, withType type:PacketType, withData data:ContiguousArray<NSData>?) {
|
public func write(msg:String, withType type:PacketType, withData data:ContiguousArray<NSData>?) {
|
||||||
dispatch_async(self.emitQueue) {[weak self] in
|
dispatch_async(self.emitQueue) {[weak self] in
|
||||||
if self == nil || !self!.connected {
|
if let this = self where this.connected {
|
||||||
return
|
if this.websocket {
|
||||||
}
|
SocketLogger.log("Writing ws: \(msg):\(data)", client: this)
|
||||||
|
this.sendWebSocketMessage(msg, withType: type, datas: data)
|
||||||
if self!.websocket {
|
|
||||||
SocketLogger.log("Writing ws: \(msg):\(data)", client: self!)
|
|
||||||
self?.sendWebSocketMessage(msg, withType: type, datas: data)
|
|
||||||
} else {
|
} else {
|
||||||
SocketLogger.log("Writing poll: \(msg):\(data)", client: self!)
|
SocketLogger.log("Writing poll: \(msg):\(data)", client: this)
|
||||||
self?.sendPollMessage(msg, withType: type, datas: data)
|
this.sendPollMessage(msg, withType: type, datas: data)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user