clean up namespace logic

This commit is contained in:
Erik 2015-07-09 12:29:26 -04:00
parent 24583f19c1
commit 8daa7da426
3 changed files with 23 additions and 24 deletions

View File

@ -99,11 +99,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
self.log = log
}
if var nsp = opts?["nsp"] as? String {
if nsp != "/" && nsp.hasPrefix("/") {
nsp.removeAtIndex(nsp.startIndex)
}
if let nsp = opts?["nsp"] as? String {
self.nsp = nsp
}
@ -402,7 +398,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
*/
public func leaveNamespace() {
if nsp != "/" {
engine?.send("1/\(nsp)", withData: nil)
engine?.send("1\(nsp)", withData: nil)
nsp = "/"
}
}
@ -414,7 +410,7 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
SocketLogger.log("Joining namespace", client: self)
if nsp != "/" {
engine?.send("0/\(nsp)", withData: nil)
engine?.send("0\(nsp)", withData: nil)
}
}

View File

@ -25,6 +25,9 @@
import Foundation
struct SocketPacket {
let nsp:String
let id:Int
enum PacketType:Int {
case CONNECT = 0
case DISCONNECT = 1
@ -46,8 +49,6 @@ struct SocketPacket {
var currentPlace = 0
var binary:[NSData]
var data:[AnyObject]
var id:Int = -1
var nsp = ""
var placeholders:Int
var type:PacketType
var description:String {
@ -149,7 +150,7 @@ struct SocketPacket {
if nsp == "/" {
msg = "3\(id)["
} else {
msg = "3/\(nsp),\(id)["
msg = "3\(nsp),\(id)["
}
} else {
if nsp == "/" {
@ -175,9 +176,9 @@ struct SocketPacket {
}
} else {
if id == -1 {
message = "2/\(nsp),[\"\(event)\""
message = "2\(nsp),[\"\(event)\""
} else {
message = "2/\(nsp),\(id)[\"\(event)\""
message = "2\(nsp),\(id)[\"\(event)\""
}
}
} else {
@ -189,9 +190,9 @@ struct SocketPacket {
}
} else {
if id == -1 {
message = "5\(binary.count)-/\(nsp),[\"\(event)\""
message = "5\(binary.count)-\(nsp),[\"\(event)\""
} else {
message = "5\(binary.count)-/\(nsp),\(id)[\"\(event)\""
message = "5\(binary.count)-\(nsp),\(id)[\"\(event)\""
}
}
}

View File

@ -23,12 +23,12 @@
import Foundation
class SocketParser {
private static func checkNSP(nsp:String, _ socket:SocketIOClient) -> Bool {
return nsp == "" && socket.nsp != "/"
private static func isCorrectNamespace(nsp:String, _ socket:SocketIOClient) -> Bool {
return nsp == socket.nsp
}
private static func handleAck(p:SocketPacket, socket:SocketIOClient) {
if checkNSP(p.nsp, socket) {
if !isCorrectNamespace(p.nsp, socket) {
return
}
@ -36,7 +36,7 @@ class SocketParser {
}
private static func handleBinaryAck(p:SocketPacket, socket:SocketIOClient) {
if checkNSP(p.nsp, socket) {
if !isCorrectNamespace(p.nsp, socket) {
return
}
@ -44,7 +44,7 @@ class SocketParser {
}
private static func handleBinaryEvent(p:SocketPacket, socket:SocketIOClient) {
if checkNSP(p.nsp, socket) {
if !isCorrectNamespace(p.nsp, socket) {
return
}
@ -62,7 +62,7 @@ class SocketParser {
}
private static func handleEvent(p:SocketPacket, socket:SocketIOClient) {
if checkNSP(p.nsp, socket) {
if !isCorrectNamespace(p.nsp, socket) {
return
}
@ -80,7 +80,7 @@ class SocketParser {
}
var id = nil as Int?
var nsp = ""
var nsp:String?
var i = 0
var placeholders = -1
@ -103,6 +103,8 @@ class SocketParser {
}
if arr[i + 1] == "/" {
nsp = ""
while ++i < arr.count {
let c = arr[i]
@ -110,13 +112,13 @@ class SocketParser {
break
}
nsp += String(c)
nsp! += String(c)
}
}
if i + 1 >= arr.count {
return SocketPacket(type: SocketPacket.PacketType(str: type)!, id: id ?? -1,
nsp: nsp, placeholders: placeholders)
nsp: nsp ?? "/", placeholders: placeholders)
}
let next = String(arr[i + 1])
@ -141,7 +143,7 @@ class SocketParser {
let data = SocketParser.parseData(noPlaceholders) as? [AnyObject] ?? [noPlaceholders]
return SocketPacket(type: SocketPacket.PacketType(str: type)!, data: data, id: id ?? -1,
nsp: nsp, placeholders: placeholders)
nsp: nsp ?? "/", placeholders: placeholders)
}
return nil