From 922ed4b575d0673922c2b1daabe6465db008bdd6 Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 22 Apr 2015 15:26:34 -0400 Subject: [PATCH] fix socketio/socket.io-client-swift#61 --- SocketIOClientSwift/SocketEngine.swift | 40 +++++++-------- SocketIOClientSwift/SocketPacket.swift | 71 +++++++++++++------------- 2 files changed, 55 insertions(+), 56 deletions(-) diff --git a/SocketIOClientSwift/SocketEngine.swift b/SocketIOClientSwift/SocketEngine.swift index 535e1a8..a2f076a 100644 --- a/SocketIOClientSwift/SocketEngine.swift +++ b/SocketIOClientSwift/SocketEngine.swift @@ -39,6 +39,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { private let parseQueue = dispatch_queue_create("engineParseQueue", DISPATCH_QUEUE_SERIAL) private let handleQueue = dispatch_queue_create("engineHandleQueue", DISPATCH_QUEUE_SERIAL) private let session:NSURLSession! + private var closed = false private var _connected = false private var fastUpgrade = false @@ -157,7 +158,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { if params != nil { let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" ").invertedSet - + for (key, value) in params! { let keyEsc = key.stringByAddingPercentEncodingWithAllowedCharacters( allowedCharacterSet)! @@ -210,12 +211,12 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { self.waitingForPoll = true let req = NSMutableURLRequest(URL: NSURL(string: self.urlPolling! + "&sid=\(self.sid)&b64=1")!) - + if self.cookies != nil { let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(self.cookies!) req.allHTTPHeaderFields = headers } - + self.doRequest(req) } @@ -299,12 +300,12 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { self.postWait.removeAll(keepCapacity: false) let req = NSMutableURLRequest(URL: NSURL(string: self.urlPolling! + "&sid=\(self.sid)")!) - + if self.cookies != nil { let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(self.cookies!) req.allHTTPHeaderFields = headers } - + req.HTTPMethod = "POST" req.setValue("text/plain; charset=UTF-8", forHTTPHeaderField: "Content-Type") @@ -540,22 +541,19 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient { } return - } else { - if message.hasPrefix("b4") { - // binary in base64 string - - message.removeRange(Range(start: message.startIndex, - end: advance(message.startIndex, 2))) - - if let data = NSData(base64EncodedString: message, - options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters) - where self.client != nil { - // println("sending \(data)") - - dispatch_async(self.client!.handleQueue) {[weak self] in - self?.client?.parseBinaryData(data) - } - } + } else if message.hasPrefix("b4") { + // binary in base64 string + message.removeRange(Range(start: message.startIndex, + end: advance(message.startIndex, 2))) + + if let data = NSData(base64EncodedString: message, + options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters) + where self.client != nil { + // println("sending \(data)") + + dispatch_async(self.client!.handleQueue) {[weak self] in + self?.client?.parseBinaryData(data) + } } } } diff --git a/SocketIOClientSwift/SocketPacket.swift b/SocketIOClientSwift/SocketPacket.swift index ab5c471..efeddc5 100644 --- a/SocketIOClientSwift/SocketPacket.swift +++ b/SocketIOClientSwift/SocketPacket.swift @@ -62,7 +62,7 @@ final class SocketPacket: Printable { } } } - + init(type:PacketType?, data:[AnyObject]? = nil, nsp:String = "", placeholders:Int? = nil, id:Int? = nil) { self.type = type @@ -71,19 +71,19 @@ final class SocketPacket: Printable { self.placeholders = placeholders self.id = id } - + func getEvent() -> String { return data?.removeAtIndex(0) as! String } - + func addData(data:NSData) -> Bool { if self.placeholders == self.currentPlace { return true } - + self.binary.append(data) self.currentPlace++ - + if self.placeholders == self.currentPlace { self.currentPlace = 0 return true @@ -91,14 +91,14 @@ final class SocketPacket: Printable { return false } } - + func createMessageForEvent(event:String) -> String { let message:String var jsonSendError:NSError? - + if self.binary.count == 0 { self.type = PacketType.EVENT - + if self.nsp == "/" { if self.id == nil { message = "2[\"\(event)\"" @@ -114,7 +114,7 @@ final class SocketPacket: Printable { } } else { self.type = PacketType.BINARY_EVENT - + if self.nsp == "/" { if self.id == nil { message = "5\(self.binary.count)-[\"\(event)\"" @@ -129,16 +129,16 @@ final class SocketPacket: Printable { } } } - + return self.completeMessage(message) } - + func createAck() -> String { var msg:String - + if self.binary.count == 0 { self.type = PacketType.ACK - + if nsp == "/" { msg = "3\(self.id!)[" } else { @@ -146,70 +146,71 @@ final class SocketPacket: Printable { } } else { self.type = PacketType.BINARY_ACK - + if nsp == "/" { msg = "6\(self.binary.count)-\(self.id!)[" } else { msg = "6\(self.binary.count)-/\(self.nsp),\(self.id!)[" } } - + return self.completeMessage(msg, ack: true) } - + func completeMessage(var message:String, ack:Bool = false) -> String { var err:NSError? - + if self.data == nil || self.data!.count == 0 { return message + "]" } else if !ack { message += "," } - + for arg in self.data! { if arg is NSDictionary || arg is [AnyObject] { let jsonSend = NSJSONSerialization.dataWithJSONObject(arg, options: NSJSONWritingOptions(0), error: &err) let jsonString = NSString(data: jsonSend!, encoding: NSUTF8StringEncoding) - + message += jsonString! as String message += "," continue + } else if arg is NSNull { + message += "null," + continue } - + if arg is String { message += "\"\(arg)\"" message += "," continue } - + message += "\(arg)" message += "," } - + if message != "" { message.removeAtIndex(message.endIndex.predecessor()) } - + return message + "]" } - + func fillInPlaceholders() { var newArr = NSMutableArray(array: self.data!) - + for i in 0.. AnyObject { if let str = data as? String { if let num = str["~~(\\d)"].groups() { @@ -219,19 +220,19 @@ final class SocketPacket: Printable { } } else if let dict = data as? NSDictionary { var newDict = NSMutableDictionary(dictionary: dict) - + for (key, value) in dict { newDict[key as! NSCopying] = _fillInPlaceholders(value) } - + return newDict } else if let arr = data as? NSArray { var newArr = NSMutableArray(array: arr) - + for i in 0..