Move SocketPacketType to SocketPacket.swift
This commit is contained in:
parent
20d1ad76af
commit
2a322ff5e5
@ -299,7 +299,7 @@ public class SocketIOClient: NSObject, SocketEngineClient {
|
|||||||
SocketParser.parseForEmit(packet)
|
SocketParser.parseForEmit(packet)
|
||||||
str = packet.createMessageForEvent(event)
|
str = packet.createMessageForEvent(event)
|
||||||
|
|
||||||
if packet.type == SocketPacketType.BINARY_EVENT {
|
if packet.type == SocketPacket.PacketType.BINARY_EVENT {
|
||||||
self.engine?.send(str, withData: packet.binary)
|
self.engine?.send(str, withData: packet.binary)
|
||||||
} else {
|
} else {
|
||||||
self.engine?.send(str, withData: nil)
|
self.engine?.send(str, withData: nil)
|
||||||
@ -319,7 +319,7 @@ public class SocketIOClient: NSObject, SocketEngineClient {
|
|||||||
SocketParser.parseForEmit(packet)
|
SocketParser.parseForEmit(packet)
|
||||||
str = packet.createAck()
|
str = packet.createAck()
|
||||||
|
|
||||||
if packet.type == SocketPacketType.BINARY_ACK {
|
if packet.type == SocketPacket.PacketType.BINARY_ACK {
|
||||||
self?.engine?.send(str, withData: packet.binary)
|
self?.engine?.send(str, withData: packet.binary)
|
||||||
} else {
|
} else {
|
||||||
self?.engine?.send(str, withData: nil)
|
self?.engine?.send(str, withData: nil)
|
||||||
|
|||||||
@ -32,9 +32,27 @@ class SocketPacket {
|
|||||||
var justAck = false
|
var justAck = false
|
||||||
var nsp = ""
|
var nsp = ""
|
||||||
var placeholders:Int?
|
var placeholders:Int?
|
||||||
var type:SocketPacketType?
|
var type:PacketType?
|
||||||
|
|
||||||
init(type:SocketPacketType?, data:[AnyObject]? = nil, nsp:String = "",
|
enum PacketType:Int {
|
||||||
|
case CONNECT = 0
|
||||||
|
case DISCONNECT = 1
|
||||||
|
case EVENT = 2
|
||||||
|
case ACK = 3
|
||||||
|
case ERROR = 4
|
||||||
|
case BINARY_EVENT = 5
|
||||||
|
case BINARY_ACK = 6
|
||||||
|
|
||||||
|
init(str:String) {
|
||||||
|
if let int = str.toInt() {
|
||||||
|
self = PacketType(rawValue: int)!
|
||||||
|
} else {
|
||||||
|
self = PacketType(rawValue: 4)!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
self.data = data
|
self.data = data
|
||||||
@ -76,7 +94,7 @@ class SocketPacket {
|
|||||||
var jsonSendError:NSError?
|
var jsonSendError:NSError?
|
||||||
|
|
||||||
if self.binary.count == 0 {
|
if self.binary.count == 0 {
|
||||||
self.type = SocketPacketType.EVENT
|
self.type = PacketType.EVENT
|
||||||
|
|
||||||
if self.nsp == "/" {
|
if self.nsp == "/" {
|
||||||
if self.id == nil {
|
if self.id == nil {
|
||||||
@ -92,7 +110,7 @@ class SocketPacket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.type = SocketPacketType.BINARY_EVENT
|
self.type = PacketType.BINARY_EVENT
|
||||||
|
|
||||||
if self.nsp == "/" {
|
if self.nsp == "/" {
|
||||||
if self.id == nil {
|
if self.id == nil {
|
||||||
@ -116,7 +134,7 @@ class SocketPacket {
|
|||||||
var msg:String
|
var msg:String
|
||||||
|
|
||||||
if self.binary.count == 0 {
|
if self.binary.count == 0 {
|
||||||
self.type = SocketPacketType.ACK
|
self.type = PacketType.ACK
|
||||||
|
|
||||||
if nsp == "/" {
|
if nsp == "/" {
|
||||||
msg = "3\(self.id!)["
|
msg = "3\(self.id!)["
|
||||||
@ -124,7 +142,7 @@ class SocketPacket {
|
|||||||
msg = "3/\(self.nsp),\(self.id!)["
|
msg = "3/\(self.nsp),\(self.id!)["
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.type = SocketPacketType.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!)["
|
||||||
|
|||||||
@ -85,7 +85,7 @@ class SocketParser {
|
|||||||
let type = String(arr[0])
|
let type = String(arr[0])
|
||||||
|
|
||||||
if arr.count == 1 {
|
if arr.count == 1 {
|
||||||
return SocketPacket(type: SocketPacketType(str: type))
|
return SocketPacket(type: SocketPacket.PacketType(str: type))
|
||||||
}
|
}
|
||||||
|
|
||||||
var id = nil as Int?
|
var id = nil as Int?
|
||||||
@ -124,7 +124,7 @@ class SocketParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if i + 1 >= arr.count {
|
if i + 1 >= arr.count {
|
||||||
return SocketPacket(type: SocketPacketType(str: type),
|
return SocketPacket(type: SocketPacket.PacketType(str: type),
|
||||||
nsp: nsp, placeholders: placeholders, id: id)
|
nsp: nsp, placeholders: placeholders, id: id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,7 +149,7 @@ class SocketParser {
|
|||||||
let noPlaceholders = d["(\\{\"_placeholder\":true,\"num\":(\\d*)\\})"] ~= "\"~~$2\""
|
let noPlaceholders = d["(\\{\"_placeholder\":true,\"num\":(\\d*)\\})"] ~= "\"~~$2\""
|
||||||
let data = SocketParser.parseData(noPlaceholders) as? [AnyObject] ?? [noPlaceholders]
|
let data = SocketParser.parseData(noPlaceholders) as? [AnyObject] ?? [noPlaceholders]
|
||||||
|
|
||||||
return SocketPacket(type: SocketPacketType(str: type), data: data,
|
return SocketPacket(type: SocketPacket.PacketType(str: type), data: data,
|
||||||
nsp: nsp, placeholders: placeholders, id: id)
|
nsp: nsp, placeholders: placeholders, id: id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,33 +187,33 @@ class SocketParser {
|
|||||||
|
|
||||||
let p = parseString(stringMessage) as SocketPacket!
|
let p = parseString(stringMessage) as SocketPacket!
|
||||||
|
|
||||||
if p.type == SocketPacketType.EVENT {
|
if p.type == SocketPacket.PacketType.EVENT {
|
||||||
if checkNSP(p.nsp) {
|
if checkNSP(p.nsp) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.handleEvent(p.getEvent(), data: p.data,
|
socket.handleEvent(p.getEvent(), data: p.data,
|
||||||
isInternalMessage: false, wantsAck: p.id)
|
isInternalMessage: false, wantsAck: p.id)
|
||||||
} else if p.type == SocketPacketType.ACK {
|
} else if p.type == SocketPacket.PacketType.ACK {
|
||||||
if checkNSP(p.nsp) {
|
if checkNSP(p.nsp) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.handleAck(p.id!, data: p.data)
|
socket.handleAck(p.id!, data: p.data)
|
||||||
} else if p.type == SocketPacketType.BINARY_EVENT {
|
} else if p.type == SocketPacket.PacketType.BINARY_EVENT {
|
||||||
if checkNSP(p.nsp) {
|
if checkNSP(p.nsp) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.waitingData.append(p)
|
socket.waitingData.append(p)
|
||||||
} else if p.type == SocketPacketType.BINARY_ACK {
|
} else if p.type == SocketPacket.PacketType.BINARY_ACK {
|
||||||
if checkNSP(p.nsp) {
|
if checkNSP(p.nsp) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
p.justAck = true
|
p.justAck = true
|
||||||
socket.waitingData.append(p)
|
socket.waitingData.append(p)
|
||||||
} else if p.type == SocketPacketType.CONNECT {
|
} else if p.type == SocketPacket.PacketType.CONNECT {
|
||||||
if p.nsp == "" && socket.nsp != "/" {
|
if p.nsp == "" && socket.nsp != "/" {
|
||||||
socket.joinNamespace()
|
socket.joinNamespace()
|
||||||
} else if p.nsp != "" && socket.nsp == "/" {
|
} else if p.nsp != "" && socket.nsp == "/" {
|
||||||
@ -221,9 +221,9 @@ class SocketParser {
|
|||||||
} else {
|
} else {
|
||||||
socket.didConnect()
|
socket.didConnect()
|
||||||
}
|
}
|
||||||
} else if p.type == SocketPacketType.DISCONNECT {
|
} else if p.type == SocketPacket.PacketType.DISCONNECT {
|
||||||
socket.engineDidForceClose("Got Disconnect")
|
socket.engineDidForceClose("Got Disconnect")
|
||||||
} else if p.type == SocketPacketType.ERROR {
|
} else if p.type == SocketPacket.PacketType.ERROR {
|
||||||
socket.didError(p.data == nil ? "Error" : p.data!)
|
socket.didError(p.data == nil ? "Error" : p.data!)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,21 +32,3 @@ public typealias AckCallback = @objc_block (NSArray?) -> Void
|
|||||||
public typealias AckEmitter = (AnyObject...) -> Void
|
public typealias AckEmitter = (AnyObject...) -> Void
|
||||||
public typealias NormalCallback = (NSArray?, AckEmitter?) -> Void
|
public typealias NormalCallback = (NSArray?, AckEmitter?) -> Void
|
||||||
public typealias OnAckCallback = (timeout:UInt64, callback:AckCallback) -> Void
|
public typealias OnAckCallback = (timeout:UInt64, callback:AckCallback) -> Void
|
||||||
|
|
||||||
enum SocketPacketType:Int {
|
|
||||||
case CONNECT = 0
|
|
||||||
case DISCONNECT = 1
|
|
||||||
case EVENT = 2
|
|
||||||
case ACK = 3
|
|
||||||
case ERROR = 4
|
|
||||||
case BINARY_EVENT = 5
|
|
||||||
case BINARY_ACK = 6
|
|
||||||
|
|
||||||
init(str:String) {
|
|
||||||
if let int = str.toInt() {
|
|
||||||
self = SocketPacketType(rawValue: int)!
|
|
||||||
} else {
|
|
||||||
self = SocketPacketType(rawValue: 4)!
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user