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