Skip to content

Commit 206e1ee

Browse files
committed
Tell users what namespace was connected to
1 parent 1a42580 commit 206e1ee

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

Source/SocketIOClient.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,12 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
203203
return OnAckCallback(ackNumber: currentAck, items: items, socket: self)
204204
}
205205

206-
func didConnect() {
206+
func didConnect(toNamespace namespace: String) {
207207
DefaultSocketLogger.Logger.log("Socket connected", type: logType)
208208

209209
status = .connected
210210

211-
handleClientEvent(.connect, data: [])
211+
handleClientEvent(.connect, data: [namespace])
212212
}
213213

214214
func didDisconnect(reason: String) {

Source/SocketIOClientSpec.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protocol SocketIOClientSpec : class {
2929
var nsp: String { get set }
3030
var waitingPackets: [SocketPacket] { get set }
3131

32-
func didConnect()
32+
func didConnect(toNamespace namespace: String)
3333
func didDisconnect(reason: String)
3434
func didError(reason: String)
3535
func handleAck(_ ack: Int, data: [Any])

Source/SocketParsable.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ extension SocketParsable where Self: SocketIOClientSpec {
3131
private func isCorrectNamespace(_ nsp: String) -> Bool {
3232
return nsp == self.nsp
3333
}
34-
34+
3535
private func handleConnect(_ packetNamespace: String) {
3636
if packetNamespace == "/" && nsp != "/" {
3737
joinNamespace(nsp)
3838
} else {
39-
didConnect()
39+
didConnect(toNamespace: packetNamespace)
4040
}
4141
}
42-
42+
4343
private func handlePacket(_ pack: SocketPacket) {
4444
switch pack.type {
4545
case .event where isCorrectNamespace(pack.nsp):
@@ -60,40 +60,40 @@ extension SocketParsable where Self: SocketIOClientSpec {
6060
DefaultSocketLogger.Logger.log("Got invalid packet: %@", type: "SocketParser", args: pack.description)
6161
}
6262
}
63-
63+
6464
/// Parses a messsage from the engine. Returning either a string error or a complete SocketPacket
6565
func parseString(_ message: String) -> Either<String, SocketPacket> {
6666
var reader = SocketStringReader(message: message)
67-
67+
6868
guard let type = Int(reader.read(count: 1)).flatMap({ SocketPacket.PacketType(rawValue: $0) }) else {
6969
return .left("Invalid packet type")
7070
}
71-
71+
7272
if !reader.hasNext {
7373
return .right(SocketPacket(type: type, nsp: "/"))
7474
}
75-
75+
7676
var namespace = "/"
7777
var placeholders = -1
78-
78+
7979
if type == .binaryEvent || type == .binaryAck {
8080
if let holders = Int(reader.readUntilOccurence(of: "-")) {
8181
placeholders = holders
8282
} else {
8383
return .left("Invalid packet")
8484
}
8585
}
86-
86+
8787
if reader.currentCharacter == "/" {
88-
namespace = reader.readUntilOccurence(of: ",")
88+
namespace = reader.readUntilOccurence(of: ",")
8989
}
90-
90+
9191
if !reader.hasNext {
9292
return .right(SocketPacket(type: type, nsp: namespace, placeholders: placeholders))
9393
}
94-
94+
9595
var idString = ""
96-
96+
9797
if type == .error {
9898
reader.advance(by: -1)
9999
} else {
@@ -106,13 +106,13 @@ extension SocketParsable where Self: SocketIOClientSpec {
106106
}
107107
}
108108
}
109-
109+
110110
var dataArray = String(message.utf16[message.utf16.index(reader.currentIndex, offsetBy: 1)..<message.utf16.endIndex])!
111-
111+
112112
if type == .error && !dataArray.hasPrefix("[") && !dataArray.hasSuffix("]") {
113113
dataArray = "[" + dataArray + "]"
114114
}
115-
115+
116116
switch parseData(dataArray) {
117117
case let .left(err):
118118
return .left(err)
@@ -121,7 +121,7 @@ extension SocketParsable where Self: SocketIOClientSpec {
121121
nsp: namespace, placeholders: placeholders))
122122
}
123123
}
124-
124+
125125
// Parses data for events
126126
private func parseData(_ data: String) -> Either<String, [Any]> {
127127
do {
@@ -130,13 +130,13 @@ extension SocketParsable where Self: SocketIOClientSpec {
130130
return .left("Error parsing data for packet")
131131
}
132132
}
133-
133+
134134
// Parses messages recieved
135135
func parseSocketMessage(_ message: String) {
136136
guard !message.isEmpty else { return }
137-
137+
138138
DefaultSocketLogger.Logger.log("Parsing %@", type: "SocketParser", args: message)
139-
139+
140140
switch parseString(message) {
141141
case let .left(err):
142142
DefaultSocketLogger.Logger.error("\(err): %@", type: "SocketParser", args: message)
@@ -145,18 +145,18 @@ extension SocketParsable where Self: SocketIOClientSpec {
145145
handlePacket(pack)
146146
}
147147
}
148-
148+
149149
func parseBinaryData(_ data: Data) {
150150
guard !waitingPackets.isEmpty else {
151151
DefaultSocketLogger.Logger.error("Got data when not remaking packet", type: "SocketParser")
152152
return
153153
}
154-
154+
155155
// Should execute event?
156156
guard waitingPackets[waitingPackets.count - 1].addData(data) else { return }
157-
157+
158158
let packet = waitingPackets.removeLast()
159-
159+
160160
if packet.type != .binaryAck {
161161
handleEvent(packet.event, data: packet.args, isInternalMessage: false, withAck: packet.id)
162162
} else {

0 commit comments

Comments
 (0)