This commit is contained in:
parent
eab7311037
commit
922ed4b575
@ -39,6 +39,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
private let parseQueue = dispatch_queue_create("engineParseQueue", DISPATCH_QUEUE_SERIAL)
|
private let parseQueue = dispatch_queue_create("engineParseQueue", DISPATCH_QUEUE_SERIAL)
|
||||||
private let handleQueue = dispatch_queue_create("engineHandleQueue", DISPATCH_QUEUE_SERIAL)
|
private let handleQueue = dispatch_queue_create("engineHandleQueue", DISPATCH_QUEUE_SERIAL)
|
||||||
private let session:NSURLSession!
|
private let session:NSURLSession!
|
||||||
|
|
||||||
private var closed = false
|
private var closed = false
|
||||||
private var _connected = false
|
private var _connected = false
|
||||||
private var fastUpgrade = false
|
private var fastUpgrade = false
|
||||||
@ -157,7 +158,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
|
|
||||||
if params != nil {
|
if params != nil {
|
||||||
let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" ").invertedSet
|
let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" ").invertedSet
|
||||||
|
|
||||||
for (key, value) in params! {
|
for (key, value) in params! {
|
||||||
let keyEsc = key.stringByAddingPercentEncodingWithAllowedCharacters(
|
let keyEsc = key.stringByAddingPercentEncodingWithAllowedCharacters(
|
||||||
allowedCharacterSet)!
|
allowedCharacterSet)!
|
||||||
@ -210,12 +211,12 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
|
|
||||||
self.waitingForPoll = true
|
self.waitingForPoll = true
|
||||||
let req = NSMutableURLRequest(URL: NSURL(string: self.urlPolling! + "&sid=\(self.sid)&b64=1")!)
|
let req = NSMutableURLRequest(URL: NSURL(string: self.urlPolling! + "&sid=\(self.sid)&b64=1")!)
|
||||||
|
|
||||||
if self.cookies != nil {
|
if self.cookies != nil {
|
||||||
let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(self.cookies!)
|
let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(self.cookies!)
|
||||||
req.allHTTPHeaderFields = headers
|
req.allHTTPHeaderFields = headers
|
||||||
}
|
}
|
||||||
|
|
||||||
self.doRequest(req)
|
self.doRequest(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -299,12 +300,12 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
self.postWait.removeAll(keepCapacity: false)
|
self.postWait.removeAll(keepCapacity: false)
|
||||||
|
|
||||||
let req = NSMutableURLRequest(URL: NSURL(string: self.urlPolling! + "&sid=\(self.sid)")!)
|
let req = NSMutableURLRequest(URL: NSURL(string: self.urlPolling! + "&sid=\(self.sid)")!)
|
||||||
|
|
||||||
if self.cookies != nil {
|
if self.cookies != nil {
|
||||||
let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(self.cookies!)
|
let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(self.cookies!)
|
||||||
req.allHTTPHeaderFields = headers
|
req.allHTTPHeaderFields = headers
|
||||||
}
|
}
|
||||||
|
|
||||||
req.HTTPMethod = "POST"
|
req.HTTPMethod = "POST"
|
||||||
req.setValue("text/plain; charset=UTF-8", forHTTPHeaderField: "Content-Type")
|
req.setValue("text/plain; charset=UTF-8", forHTTPHeaderField: "Content-Type")
|
||||||
|
|
||||||
@ -540,22 +541,19 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
} else {
|
} else if message.hasPrefix("b4") {
|
||||||
if message.hasPrefix("b4") {
|
// binary in base64 string
|
||||||
// binary in base64 string
|
message.removeRange(Range<String.Index>(start: message.startIndex,
|
||||||
|
end: advance(message.startIndex, 2)))
|
||||||
message.removeRange(Range<String.Index>(start: message.startIndex,
|
|
||||||
end: advance(message.startIndex, 2)))
|
if let data = NSData(base64EncodedString: message,
|
||||||
|
options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
|
||||||
if let data = NSData(base64EncodedString: message,
|
where self.client != nil {
|
||||||
options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
|
// println("sending \(data)")
|
||||||
where self.client != nil {
|
|
||||||
// println("sending \(data)")
|
dispatch_async(self.client!.handleQueue) {[weak self] in
|
||||||
|
self?.client?.parseBinaryData(data)
|
||||||
dispatch_async(self.client!.handleQueue) {[weak self] in
|
}
|
||||||
self?.client?.parseBinaryData(data)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -62,7 +62,7 @@ final class SocketPacket: Printable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
init(type:PacketType?, data:[AnyObject]? = nil, nsp:String = "",
|
init(type:PacketType?, data:[AnyObject]? = nil, nsp:String = "",
|
||||||
placeholders:Int? = nil, id:Int? = nil) {
|
placeholders:Int? = nil, id:Int? = nil) {
|
||||||
self.type = type
|
self.type = type
|
||||||
@ -71,19 +71,19 @@ final class SocketPacket: Printable {
|
|||||||
self.placeholders = placeholders
|
self.placeholders = placeholders
|
||||||
self.id = id
|
self.id = id
|
||||||
}
|
}
|
||||||
|
|
||||||
func getEvent() -> String {
|
func getEvent() -> String {
|
||||||
return data?.removeAtIndex(0) as! String
|
return data?.removeAtIndex(0) as! String
|
||||||
}
|
}
|
||||||
|
|
||||||
func addData(data:NSData) -> Bool {
|
func addData(data:NSData) -> Bool {
|
||||||
if self.placeholders == self.currentPlace {
|
if self.placeholders == self.currentPlace {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
self.binary.append(data)
|
self.binary.append(data)
|
||||||
self.currentPlace++
|
self.currentPlace++
|
||||||
|
|
||||||
if self.placeholders == self.currentPlace {
|
if self.placeholders == self.currentPlace {
|
||||||
self.currentPlace = 0
|
self.currentPlace = 0
|
||||||
return true
|
return true
|
||||||
@ -91,14 +91,14 @@ final class SocketPacket: Printable {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createMessageForEvent(event:String) -> String {
|
func createMessageForEvent(event:String) -> String {
|
||||||
let message:String
|
let message:String
|
||||||
var jsonSendError:NSError?
|
var jsonSendError:NSError?
|
||||||
|
|
||||||
if self.binary.count == 0 {
|
if self.binary.count == 0 {
|
||||||
self.type = PacketType.EVENT
|
self.type = PacketType.EVENT
|
||||||
|
|
||||||
if self.nsp == "/" {
|
if self.nsp == "/" {
|
||||||
if self.id == nil {
|
if self.id == nil {
|
||||||
message = "2[\"\(event)\""
|
message = "2[\"\(event)\""
|
||||||
@ -114,7 +114,7 @@ final class SocketPacket: Printable {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.type = PacketType.BINARY_EVENT
|
self.type = PacketType.BINARY_EVENT
|
||||||
|
|
||||||
if self.nsp == "/" {
|
if self.nsp == "/" {
|
||||||
if self.id == nil {
|
if self.id == nil {
|
||||||
message = "5\(self.binary.count)-[\"\(event)\""
|
message = "5\(self.binary.count)-[\"\(event)\""
|
||||||
@ -129,16 +129,16 @@ final class SocketPacket: Printable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return self.completeMessage(message)
|
return self.completeMessage(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createAck() -> String {
|
func createAck() -> String {
|
||||||
var msg:String
|
var msg:String
|
||||||
|
|
||||||
if self.binary.count == 0 {
|
if self.binary.count == 0 {
|
||||||
self.type = PacketType.ACK
|
self.type = PacketType.ACK
|
||||||
|
|
||||||
if nsp == "/" {
|
if nsp == "/" {
|
||||||
msg = "3\(self.id!)["
|
msg = "3\(self.id!)["
|
||||||
} else {
|
} else {
|
||||||
@ -146,70 +146,71 @@ final class SocketPacket: Printable {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.type = PacketType.BINARY_ACK
|
self.type = PacketType.BINARY_ACK
|
||||||
|
|
||||||
if nsp == "/" {
|
if nsp == "/" {
|
||||||
msg = "6\(self.binary.count)-\(self.id!)["
|
msg = "6\(self.binary.count)-\(self.id!)["
|
||||||
} else {
|
} else {
|
||||||
msg = "6\(self.binary.count)-/\(self.nsp),\(self.id!)["
|
msg = "6\(self.binary.count)-/\(self.nsp),\(self.id!)["
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return self.completeMessage(msg, ack: true)
|
return self.completeMessage(msg, ack: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func completeMessage(var message:String, ack:Bool = false) -> String {
|
func completeMessage(var message:String, ack:Bool = false) -> String {
|
||||||
var err:NSError?
|
var err:NSError?
|
||||||
|
|
||||||
if self.data == nil || self.data!.count == 0 {
|
if self.data == nil || self.data!.count == 0 {
|
||||||
return message + "]"
|
return message + "]"
|
||||||
} else if !ack {
|
} else if !ack {
|
||||||
message += ","
|
message += ","
|
||||||
}
|
}
|
||||||
|
|
||||||
for arg in self.data! {
|
for arg in self.data! {
|
||||||
if arg is NSDictionary || arg is [AnyObject] {
|
if arg is NSDictionary || arg is [AnyObject] {
|
||||||
let jsonSend = NSJSONSerialization.dataWithJSONObject(arg,
|
let jsonSend = NSJSONSerialization.dataWithJSONObject(arg,
|
||||||
options: NSJSONWritingOptions(0), error: &err)
|
options: NSJSONWritingOptions(0), error: &err)
|
||||||
let jsonString = NSString(data: jsonSend!, encoding: NSUTF8StringEncoding)
|
let jsonString = NSString(data: jsonSend!, encoding: NSUTF8StringEncoding)
|
||||||
|
|
||||||
message += jsonString! as String
|
message += jsonString! as String
|
||||||
message += ","
|
message += ","
|
||||||
continue
|
continue
|
||||||
|
} else if arg is NSNull {
|
||||||
|
message += "null,"
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if arg is String {
|
if arg is String {
|
||||||
message += "\"\(arg)\""
|
message += "\"\(arg)\""
|
||||||
message += ","
|
message += ","
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
message += "\(arg)"
|
message += "\(arg)"
|
||||||
message += ","
|
message += ","
|
||||||
}
|
}
|
||||||
|
|
||||||
if message != "" {
|
if message != "" {
|
||||||
message.removeAtIndex(message.endIndex.predecessor())
|
message.removeAtIndex(message.endIndex.predecessor())
|
||||||
}
|
}
|
||||||
|
|
||||||
return message + "]"
|
return message + "]"
|
||||||
}
|
}
|
||||||
|
|
||||||
func fillInPlaceholders() {
|
func fillInPlaceholders() {
|
||||||
var newArr = NSMutableArray(array: self.data!)
|
var newArr = NSMutableArray(array: self.data!)
|
||||||
|
|
||||||
for i in 0..<self.data!.count {
|
for i in 0..<self.data!.count {
|
||||||
if let str = self.data?[i] as? String {
|
if let str = self.data?[i] as? String, num = str["~~(\\d)"].groups() {
|
||||||
if let num = str["~~(\\d)"].groups() {
|
newArr[i] = self.binary[num[1].toInt()!]
|
||||||
newArr[i] = self.binary[num[1].toInt()!]
|
|
||||||
}
|
|
||||||
} else if self.data?[i] is NSDictionary || self.data?[i] is NSArray {
|
} else if self.data?[i] is NSDictionary || self.data?[i] is NSArray {
|
||||||
newArr[i] = self._fillInPlaceholders(self.data![i])
|
newArr[i] = self._fillInPlaceholders(self.data![i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.data = newArr as [AnyObject]
|
self.data = newArr as [AnyObject]
|
||||||
}
|
}
|
||||||
|
|
||||||
private func _fillInPlaceholders(data:AnyObject) -> AnyObject {
|
private func _fillInPlaceholders(data:AnyObject) -> AnyObject {
|
||||||
if let str = data as? String {
|
if let str = data as? String {
|
||||||
if let num = str["~~(\\d)"].groups() {
|
if let num = str["~~(\\d)"].groups() {
|
||||||
@ -219,19 +220,19 @@ final class SocketPacket: Printable {
|
|||||||
}
|
}
|
||||||
} else if let dict = data as? NSDictionary {
|
} else if let dict = data as? NSDictionary {
|
||||||
var newDict = NSMutableDictionary(dictionary: dict)
|
var newDict = NSMutableDictionary(dictionary: dict)
|
||||||
|
|
||||||
for (key, value) in dict {
|
for (key, value) in dict {
|
||||||
newDict[key as! NSCopying] = _fillInPlaceholders(value)
|
newDict[key as! NSCopying] = _fillInPlaceholders(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
return newDict
|
return newDict
|
||||||
} else if let arr = data as? NSArray {
|
} else if let arr = data as? NSArray {
|
||||||
var newArr = NSMutableArray(array: arr)
|
var newArr = NSMutableArray(array: arr)
|
||||||
|
|
||||||
for i in 0..<arr.count {
|
for i in 0..<arr.count {
|
||||||
newArr[i] = _fillInPlaceholders(arr[i])
|
newArr[i] = _fillInPlaceholders(arr[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
return newArr
|
return newArr
|
||||||
} else {
|
} else {
|
||||||
return data
|
return data
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user