refactors: refactor creating packet string, various other refactors
This commit is contained in:
parent
0564ded492
commit
71f9058723
@ -153,8 +153,7 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe
|
||||
// binary in base64 string
|
||||
let noPrefix = message[message.startIndex.advancedBy(2)..<message.endIndex]
|
||||
|
||||
if let data = NSData(base64EncodedString: noPrefix,
|
||||
options: .IgnoreUnknownCharacters) {
|
||||
if let data = NSData(base64EncodedString: noPrefix, options: .IgnoreUnknownCharacters) {
|
||||
client?.parseEngineBinaryData(data)
|
||||
}
|
||||
|
||||
@ -535,13 +534,11 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe
|
||||
connected = false
|
||||
websocket = false
|
||||
|
||||
let reason = error?.localizedDescription ?? "Socket Disconnected"
|
||||
|
||||
if error != nil {
|
||||
if let reason = error?.localizedDescription {
|
||||
didError(reason)
|
||||
} else {
|
||||
client?.engineDidClose("Socket Disconnected")
|
||||
}
|
||||
|
||||
client?.engineDidClose(reason)
|
||||
} else {
|
||||
flushProbeWait()
|
||||
}
|
||||
|
||||
@ -213,9 +213,7 @@ extension SocketEnginePollable {
|
||||
fixedMessage = message
|
||||
}
|
||||
|
||||
let strMsg = "\(type.rawValue)\(fixedMessage)"
|
||||
|
||||
postWait.append(strMsg)
|
||||
postWait.append(String(type.rawValue) + fixedMessage)
|
||||
|
||||
for data in datas {
|
||||
if case let .Right(bin) = createBinaryDataForSend(data) {
|
||||
|
||||
@ -197,8 +197,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable
|
||||
handleEvent("disconnect", data: [reason], isInternalMessage: true)
|
||||
}
|
||||
|
||||
/// Disconnects the socket. Only reconnect the same socket if you know what you're doing.
|
||||
/// Will turn off automatic reconnects.
|
||||
/// Disconnects the socket.
|
||||
public func disconnect() {
|
||||
assert(status != .NotConnected, "Tried closing a NotConnected client")
|
||||
|
||||
|
||||
@ -111,73 +111,31 @@ struct SocketPacket {
|
||||
return message + restOfMessage
|
||||
}
|
||||
|
||||
private func createAck() -> String {
|
||||
let message: String
|
||||
|
||||
if type == .Ack {
|
||||
if nsp == "/" {
|
||||
message = "3\(id)"
|
||||
} else {
|
||||
message = "3\(nsp),\(id)"
|
||||
}
|
||||
} else {
|
||||
if nsp == "/" {
|
||||
message = "6\(binary.count)-\(id)"
|
||||
} else {
|
||||
message = "6\(binary.count)-\(nsp),\(id)"
|
||||
}
|
||||
}
|
||||
|
||||
return completeMessage(message)
|
||||
}
|
||||
|
||||
|
||||
private func createMessageForEvent() -> String {
|
||||
let message: String
|
||||
|
||||
if type == .Event {
|
||||
if nsp == "/" {
|
||||
if id == -1 {
|
||||
message = "2"
|
||||
} else {
|
||||
message = "2\(id)"
|
||||
}
|
||||
} else {
|
||||
if id == -1 {
|
||||
message = "2\(nsp),"
|
||||
} else {
|
||||
message = "2\(nsp),\(id)"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if nsp == "/" {
|
||||
if id == -1 {
|
||||
message = "5\(binary.count)-"
|
||||
} else {
|
||||
message = "5\(binary.count)-\(id)"
|
||||
}
|
||||
} else {
|
||||
if id == -1 {
|
||||
message = "5\(binary.count)-\(nsp),"
|
||||
} else {
|
||||
message = "5\(binary.count)-\(nsp),\(id)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return completeMessage(message)
|
||||
}
|
||||
|
||||
private func createPacketString() -> String {
|
||||
let str: String
|
||||
let typeString = String(type.rawValue)
|
||||
let binaryCountString: String
|
||||
let nspString: String
|
||||
let idString: String
|
||||
|
||||
if type == .Event || type == .BinaryEvent {
|
||||
str = createMessageForEvent()
|
||||
if type == .BinaryEvent || type == .BinaryAck {
|
||||
binaryCountString = typeString + String(binary.count) + "-"
|
||||
} else {
|
||||
str = createAck()
|
||||
binaryCountString = typeString
|
||||
}
|
||||
|
||||
return str
|
||||
if nsp != "/" {
|
||||
nspString = binaryCountString + nsp + ","
|
||||
} else {
|
||||
nspString = binaryCountString
|
||||
}
|
||||
|
||||
if id != -1 {
|
||||
idString = nspString + String(id)
|
||||
} else {
|
||||
idString = nspString
|
||||
}
|
||||
|
||||
return completeMessage(idString)
|
||||
}
|
||||
|
||||
// Called when we have all the binary data for a packet
|
||||
|
||||
@ -162,9 +162,7 @@ extension SocketParsable {
|
||||
}
|
||||
|
||||
// Should execute event?
|
||||
guard waitingPackets[waitingPackets.count - 1].addData(data) else {
|
||||
return
|
||||
}
|
||||
guard waitingPackets[waitingPackets.count - 1].addData(data) else { return }
|
||||
|
||||
let packet = waitingPackets.removeLast()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user