Skip to content

Commit d5cccf1

Browse files
committed
don't parse emitted event messages when disableEventMessageParsing is sent
1 parent c7bf57e commit d5cccf1

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

Source/SocketIO/Client/SocketIOClient.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
311311
return
312312
}
313313

314-
let packet = SocketPacket.packetFromEmit(data, id: ack ?? -1, nsp: nsp, ack: isAck, checkForBinary: binary)
314+
let packet = SocketPacket.packetFromEmit(data, id: ack ?? -1, nsp: nsp, ack: isAck, checkForBinary: binary, disableEventMessageParsing: self.manager?.disableEventMessageParsing ?? false)
315315
let str = packet.packetString
316316

317317
DefaultSocketLogger.Logger.log("Emitting: \(str), Ack: \(isAck)", type: logType)

Source/SocketIO/Client/SocketIOClientOption.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public enum SocketIOClientOption : ClientOption {
7777
/// Used to pass in a custom logger.
7878
case logger(SocketLogger)
7979

80-
/// If passed `true`, event message data will not be parsed, and all message events will be received with
80+
/// If passed `true`, incoming and outgoing message data will not be parsed, and all message events will be received with
8181
/// `event` value of `"rawMessage"`; listen with `socketClient.on("rawMessage") { ... }`
8282
case disableEventMessageParsing(Bool)
8383

Source/SocketIO/Manager/SocketManagerSpec.swift

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public protocol SocketManagerSpec : AnyObject, SocketEngineClient {
5151
/// Returns the socket associated with the default namespace ("/").
5252
var defaultSocket: SocketIOClient { get }
5353

54+
/// If passed `true`, event message data will not be parsed, all message events will be received with
55+
/// `event` = "rawMessage", and the eventName will be ignored on `emit()`.
56+
var disableEventMessageParsing: Bool { get set }
57+
5458
/// The engine for this manager.
5559
var engine: SocketEngineSpec? { get set }
5660

Source/SocketIO/Parse/SocketPacket.swift

+13-4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public struct SocketPacket : CustomStringConvertible {
5858
}
5959
}
6060

61+
/// don't do any additional encoding on non-binary events
62+
private let disableEventMessageParsing: Bool
63+
6164
private let placeholders: Int
6265

6366
/// A string representation of this packet.
@@ -77,13 +80,14 @@ public struct SocketPacket : CustomStringConvertible {
7780
}
7881

7982
init(type: PacketType, data: [Any] = [Any](), id: Int = -1, nsp: String, placeholders: Int = 0,
80-
binary: [Data] = [Data]()) {
83+
binary: [Data] = [Data](), disableEventMessageParsing: Bool = false) {
8184
self.data = data
8285
self.id = id
8386
self.nsp = nsp
8487
self.type = type
8588
self.placeholders = placeholders
8689
self.binary = binary
90+
self.disableEventMessageParsing = disableEventMessageParsing
8791
}
8892

8993
mutating func addData(_ data: Data) -> Bool {
@@ -102,6 +106,10 @@ public struct SocketPacket : CustomStringConvertible {
102106
}
103107

104108
private func completeMessage(_ message: String) -> String {
109+
if type == .event && disableEventMessageParsing {
110+
return message + (args.first as? String ?? "[]")
111+
}
112+
105113
guard data.count != 0 else { return message + "[]" }
106114
guard let jsonSend = try? data.toJSON(), let jsonString = String(data: jsonSend, encoding: .utf8) else {
107115
DefaultSocketLogger.Logger.error("Error creating JSON object in SocketPacket.completeMessage",
@@ -207,14 +215,15 @@ extension SocketPacket {
207215
}
208216
}
209217

210-
static func packetFromEmit(_ items: [Any], id: Int, nsp: String, ack: Bool, checkForBinary: Bool = true) -> SocketPacket {
218+
static func packetFromEmit(_ items: [Any], id: Int, nsp: String, ack: Bool, checkForBinary: Bool = true, disableEventMessageParsing: Bool = false) -> SocketPacket {
211219
if checkForBinary {
212220
let (parsedData, binary) = deconstructData(items)
213221

214222
return SocketPacket(type: findType(binary.count, ack: ack), data: parsedData, id: id, nsp: nsp,
215-
binary: binary)
223+
binary: binary, disableEventMessageParsing: disableEventMessageParsing)
216224
} else {
217-
return SocketPacket(type: findType(0, ack: ack), data: items, id: id, nsp: nsp)
225+
return SocketPacket(type: findType(0, ack: ack), data: items, id: id, nsp: nsp,
226+
disableEventMessageParsing: disableEventMessageParsing)
218227
}
219228
}
220229
}

0 commit comments

Comments
 (0)