use int for enum
This commit is contained in:
parent
eb5d03c88b
commit
14487a1beb
@ -74,14 +74,22 @@ public class SocketEngine: NSObject, WebSocketDelegate {
|
|||||||
}
|
}
|
||||||
var ws:WebSocket?
|
var ws:WebSocket?
|
||||||
|
|
||||||
public enum PacketType:String {
|
public enum PacketType:Int {
|
||||||
case OPEN = "0"
|
case OPEN = 0
|
||||||
case CLOSE = "1"
|
case CLOSE = 1
|
||||||
case PING = "2"
|
case PING = 2
|
||||||
case PONG = "3"
|
case PONG = 3
|
||||||
case MESSAGE = "4"
|
case MESSAGE = 4
|
||||||
case UPGRADE = "5"
|
case UPGRADE = 5
|
||||||
case NOOP = "6"
|
case NOOP = 6
|
||||||
|
|
||||||
|
init(str:String) {
|
||||||
|
if let value = str.toInt() {
|
||||||
|
self = PacketType(rawValue: value)!
|
||||||
|
} else {
|
||||||
|
self = PacketType.NOOP
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(client:SocketEngineClient, forcePolling:Bool,
|
public init(client:SocketEngineClient, forcePolling:Bool,
|
||||||
@ -442,10 +450,76 @@ public class SocketEngine: NSObject, WebSocketDelegate {
|
|||||||
fixDoubleUTF8(&message)
|
fixDoubleUTF8(&message)
|
||||||
}
|
}
|
||||||
|
|
||||||
let type = message["^(\\d)"].groups()?[1]
|
let type = PacketType(str: (message["^(\\d)"].groups()?[1])!)
|
||||||
|
|
||||||
if type != PacketType.MESSAGE.rawValue {
|
if type == PacketType.MESSAGE {
|
||||||
// TODO Handle other packets
|
// Remove message type
|
||||||
|
message.removeAtIndex(message.startIndex)
|
||||||
|
|
||||||
|
if self.client == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dispatch_async(self.client!.handleQueue) {[weak self] in
|
||||||
|
self?.client?.parseSocketMessage(message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else if type == PacketType.NOOP {
|
||||||
|
self.doPoll()
|
||||||
|
return
|
||||||
|
} else if type == PacketType.PONG {
|
||||||
|
// We should upgrade
|
||||||
|
if message == "3probe" {
|
||||||
|
self.upgradeTransport()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
} else if type == PacketType.OPEN {
|
||||||
|
var err:NSError?
|
||||||
|
|
||||||
|
message.removeAtIndex(message.startIndex)
|
||||||
|
let mesData = message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
|
||||||
|
|
||||||
|
if let json = NSJSONSerialization.JSONObjectWithData(mesData,
|
||||||
|
options: NSJSONReadingOptions.AllowFragments, error: &err) as? NSDictionary {
|
||||||
|
if let sid = json["sid"] as? String {
|
||||||
|
// println(json)
|
||||||
|
self.sid = sid
|
||||||
|
self._connected = true
|
||||||
|
if !self.forcePolling && !self.forceWebsockets {
|
||||||
|
self.createWebsocket(andConnect: true)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
NSLog("Error handshaking")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if let pingInterval = json["pingInterval"] as? Int {
|
||||||
|
self.pingInterval = pingInterval / 1000
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fatalError("Error parsing engine connect")
|
||||||
|
}
|
||||||
|
|
||||||
|
self.startPingTimer()
|
||||||
|
|
||||||
|
if !self.forceWebsockets {
|
||||||
|
self.doPoll()
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
} else if type == PacketType.CLOSE {
|
||||||
|
if self.client == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.polling {
|
||||||
|
self.client!.didForceClose("Disconnect")
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
} else {
|
||||||
if message.hasPrefix("b4") {
|
if message.hasPrefix("b4") {
|
||||||
// binary in base64 string
|
// binary in base64 string
|
||||||
|
|
||||||
@ -453,90 +527,16 @@ public class SocketEngine: NSObject, WebSocketDelegate {
|
|||||||
end: advance(message.startIndex, 2)))
|
end: advance(message.startIndex, 2)))
|
||||||
|
|
||||||
if let data = NSData(base64EncodedString: message,
|
if let data = NSData(base64EncodedString: message,
|
||||||
options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters) {
|
options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
|
||||||
|
where self.client != nil {
|
||||||
// println("sending \(data)")
|
// println("sending \(data)")
|
||||||
|
|
||||||
if self.client == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatch_async(self.client!.handleQueue) {[weak self] in
|
dispatch_async(self.client!.handleQueue) {[weak self] in
|
||||||
self?.client?.parseBinaryData(data)
|
self?.client?.parseBinaryData(data)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
|
||||||
} else if type == PacketType.NOOP.rawValue {
|
|
||||||
self.doPoll()
|
|
||||||
return
|
|
||||||
} else if type == PacketType.PONG.rawValue {
|
|
||||||
// We should upgrade
|
|
||||||
if message == "3probe" {
|
|
||||||
self.upgradeTransport()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
} else if type == PacketType.OPEN.rawValue {
|
|
||||||
var err:NSError?
|
|
||||||
|
|
||||||
message.removeAtIndex(message.startIndex)
|
|
||||||
let mesData = message.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
|
|
||||||
|
|
||||||
if let json = NSJSONSerialization.JSONObjectWithData(mesData,
|
|
||||||
options: NSJSONReadingOptions.AllowFragments, error: &err) as? NSDictionary {
|
|
||||||
if let sid = json["sid"] as? String {
|
|
||||||
// println(json)
|
|
||||||
self.sid = sid
|
|
||||||
self._connected = true
|
|
||||||
if !self.forcePolling && !self.forceWebsockets {
|
|
||||||
self.createWebsocket(andConnect: true)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
NSLog("Error handshaking")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if let pingInterval = json["pingInterval"] as? Int {
|
|
||||||
self.pingInterval = pingInterval / 1000
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fatalError("Error parsing engine connect")
|
|
||||||
}
|
|
||||||
|
|
||||||
self.startPingTimer()
|
|
||||||
|
|
||||||
if !self.forceWebsockets {
|
|
||||||
self.doPoll()
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
} else if type == PacketType.CLOSE.rawValue {
|
|
||||||
if self.client == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.polling {
|
|
||||||
self.client!.didForceClose("Disconnect")
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
// println("Got something idk what to do with")
|
|
||||||
// println(messageString)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove message type
|
|
||||||
message.removeAtIndex(message.startIndex)
|
|
||||||
|
|
||||||
if self.client == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatch_async(self.client!.handleQueue) {[weak self] in
|
|
||||||
self?.client?.parseSocketMessage(message)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user