This commit is contained in:
Erik 2015-04-22 15:26:34 -04:00
parent eab7311037
commit 922ed4b575
2 changed files with 55 additions and 56 deletions

View File

@ -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<String.Index>(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<String.Index>(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)
}
}
}
}

View File

@ -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..<self.data!.count {
if let str = self.data?[i] as? String {
if let num = str["~~(\\d)"].groups() {
newArr[i] = self.binary[num[1].toInt()!]
}
if let str = self.data?[i] as? String, num = str["~~(\\d)"].groups() {
newArr[i] = self.binary[num[1].toInt()!]
} else if self.data?[i] is NSDictionary || self.data?[i] is NSArray {
newArr[i] = self._fillInPlaceholders(self.data![i])
}
}
self.data = newArr as [AnyObject]
}
private func _fillInPlaceholders(data:AnyObject) -> 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..<arr.count {
newArr[i] = _fillInPlaceholders(arr[i])
}
return newArr
} else {
return data