refactor socketpacket
This commit is contained in:
parent
12f9207d8a
commit
c795f9fb25
@ -252,9 +252,9 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
handleEvent("error", data: ["Tried emitting \(event) when not connected"], isInternalMessage: true)
|
handleEvent("error", data: ["Tried emitting \(event) when not connected"], isInternalMessage: true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch_async(emitQueue) {
|
dispatch_async(emitQueue) {[emitData = [event] + items] in
|
||||||
self._emit([event] + items)
|
self._emit(emitData)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,10 +278,10 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketParsable
|
|||||||
handleEvent("error", data: ["Tried emitting when not connected"], isInternalMessage: true)
|
handleEvent("error", data: ["Tried emitting when not connected"], isInternalMessage: true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let packet = SocketPacket.packetFromEmit(data, id: ack ?? -1, nsp: nsp, ack: false)
|
let packet = SocketPacket.packetFromEmit(data, id: ack ?? -1, nsp: nsp, ack: false)
|
||||||
let str = packet.packetString
|
let str = packet.packetString
|
||||||
|
|
||||||
DefaultSocketLogger.Logger.log("Emitting: %@", type: logType, args: str)
|
DefaultSocketLogger.Logger.log("Emitting: %@", type: logType, args: str)
|
||||||
|
|
||||||
engine?.send(str, withData: packet.binary)
|
engine?.send(str, withData: packet.binary)
|
||||||
|
|||||||
@ -161,7 +161,7 @@ extension Set where Element: ClientOption {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extension NSDictionary {
|
extension NSDictionary {
|
||||||
static func keyValueToSocketIOClientOption(key: String, value: AnyObject) -> SocketIOClientOption? {
|
private static func keyValueToSocketIOClientOption(key: String, value: AnyObject) -> SocketIOClientOption? {
|
||||||
switch (key, value) {
|
switch (key, value) {
|
||||||
case let ("connectParams", params as [String: AnyObject]):
|
case let ("connectParams", params as [String: AnyObject]):
|
||||||
return .ConnectParams(params)
|
return .ConnectParams(params)
|
||||||
|
|||||||
@ -87,38 +87,28 @@ struct SocketPacket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func completeMessage(message: String, ack: Bool) -> String {
|
private func completeMessage(message: String, ack: Bool) -> String {
|
||||||
var restOfMessage = ""
|
let restOfMessage: String
|
||||||
|
|
||||||
if data.count == 0 {
|
if data.count == 0 {
|
||||||
return message + "]"
|
return message + "[]"
|
||||||
}
|
}
|
||||||
|
|
||||||
for arg in data {
|
do {
|
||||||
if arg is NSDictionary || arg is [AnyObject] {
|
let jsonSend = try NSJSONSerialization.dataWithJSONObject(data,
|
||||||
do {
|
options: NSJSONWritingOptions(rawValue: 0))
|
||||||
let jsonSend = try NSJSONSerialization.dataWithJSONObject(arg,
|
guard let jsonString = String(data: jsonSend, encoding: NSUTF8StringEncoding) else {
|
||||||
options: NSJSONWritingOptions(rawValue: 0))
|
return "[]"
|
||||||
let jsonString = String(data: jsonSend, encoding: NSUTF8StringEncoding)
|
|
||||||
|
|
||||||
restOfMessage += jsonString! + ","
|
|
||||||
} catch {
|
|
||||||
DefaultSocketLogger.Logger.error("Error creating JSON object in SocketPacket.completeMessage",
|
|
||||||
type: SocketPacket.logType)
|
|
||||||
}
|
|
||||||
} else if let str = arg as? String {
|
|
||||||
restOfMessage += "\"" + (((str["\n"] <~ "\\\\n")["\r"] <~ "\\\\r")["\""] <~ "\\\\\"") + "\","
|
|
||||||
} else if arg is NSNull {
|
|
||||||
restOfMessage += "null,"
|
|
||||||
} else {
|
|
||||||
restOfMessage += "\(arg),"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
restOfMessage = jsonString
|
||||||
|
} catch {
|
||||||
|
DefaultSocketLogger.Logger.error("Error creating JSON object in SocketPacket.completeMessage",
|
||||||
|
type: SocketPacket.logType)
|
||||||
|
|
||||||
|
restOfMessage = "[]"
|
||||||
}
|
}
|
||||||
|
|
||||||
if restOfMessage != "" {
|
return message + restOfMessage
|
||||||
restOfMessage.removeAtIndex(restOfMessage.endIndex.predecessor())
|
|
||||||
}
|
|
||||||
|
|
||||||
return message + restOfMessage + "]"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func createAck() -> String {
|
private func createAck() -> String {
|
||||||
@ -126,15 +116,15 @@ struct SocketPacket {
|
|||||||
|
|
||||||
if type == .Ack {
|
if type == .Ack {
|
||||||
if nsp == "/" {
|
if nsp == "/" {
|
||||||
message = "3\(id)["
|
message = "3\(id)"
|
||||||
} else {
|
} else {
|
||||||
message = "3\(nsp),\(id)["
|
message = "3\(nsp),\(id)"
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if nsp == "/" {
|
if nsp == "/" {
|
||||||
message = "6\(binary.count)-\(id)["
|
message = "6\(binary.count)-\(id)"
|
||||||
} else {
|
} else {
|
||||||
message = "6\(binary.count)-\(nsp),\(id)["
|
message = "6\(binary.count)-\(nsp),\(id)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,29 +138,29 @@ struct SocketPacket {
|
|||||||
if type == .Event {
|
if type == .Event {
|
||||||
if nsp == "/" {
|
if nsp == "/" {
|
||||||
if id == -1 {
|
if id == -1 {
|
||||||
message = "2["
|
message = "2"
|
||||||
} else {
|
} else {
|
||||||
message = "2\(id)["
|
message = "2\(id)"
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if id == -1 {
|
if id == -1 {
|
||||||
message = "2\(nsp),["
|
message = "2\(nsp),"
|
||||||
} else {
|
} else {
|
||||||
message = "2\(nsp),\(id)["
|
message = "2\(nsp),\(id)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if nsp == "/" {
|
if nsp == "/" {
|
||||||
if id == -1 {
|
if id == -1 {
|
||||||
message = "5\(binary.count)-["
|
message = "5\(binary.count)-"
|
||||||
} else {
|
} else {
|
||||||
message = "5\(binary.count)-\(id)["
|
message = "5\(binary.count)-\(id)"
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if id == -1 {
|
if id == -1 {
|
||||||
message = "5\(binary.count)-\(nsp),["
|
message = "5\(binary.count)-\(nsp),"
|
||||||
} else {
|
} else {
|
||||||
message = "5\(binary.count)-\(nsp),\(id)["
|
message = "5\(binary.count)-\(nsp),\(id)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user