Skip to content

Commit c7bf57e

Browse files
committed
add option to disableEventMessageParsing and receive all messages as a "rawMessage"
1 parent af5ce97 commit c7bf57e

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

Source/SocketIO/Client/SocketIOClientOption.swift

+8
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ 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
81+
/// `event` value of `"rawMessage"`; listen with `socketClient.on("rawMessage") { ... }`
82+
case disableEventMessageParsing(Bool)
83+
8084
/// A custom path to socket.io. Only use this if the socket.io server is configured to look for this path.
8185
case path(String)
8286

@@ -124,6 +128,8 @@ public enum SocketIOClientOption : ClientOption {
124128
description = "connectParams"
125129
case .cookies:
126130
description = "cookies"
131+
case .disableEventMessageParsing:
132+
description = "disableEventMessageParsing"
127133
case .extraHeaders:
128134
description = "extraHeaders"
129135
case .forceNew:
@@ -177,6 +183,8 @@ public enum SocketIOClientOption : ClientOption {
177183
value = params
178184
case let .cookies(cookies):
179185
value = cookies
186+
case let .disableEventMessageParsing(disable):
187+
value = disable
180188
case let .extraHeaders(headers):
181189
value = headers
182190
case let .forceNew(force):

Source/SocketIO/Manager/SocketManager.swift

+7
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ open class SocketManager: NSObject, SocketManagerSpec, SocketParsable, SocketDat
7979
}
8080
}
8181

82+
83+
/// If passed `true`, event message data will not be parsed, and all message events will be received with
84+
/// `event` = "rawMessage"
85+
public var disableEventMessageParsing = false
86+
8287
/// The engine for this manager.
8388
public var engine: SocketEngineSpec?
8489

@@ -539,6 +544,8 @@ open class SocketManager: NSObject, SocketManagerSpec, SocketParsable, SocketDat
539544
open func setConfigs(_ config: SocketIOClientConfiguration) {
540545
for option in config {
541546
switch option {
547+
case let .disableEventMessageParsing(disable):
548+
disableEventMessageParsing = disable
542549
case let .forceNew(new):
543550
forceNew = new
544551
case let .handleQueue(queue):

Source/SocketIO/Parse/SocketParsable.swift

+7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import Foundation
2424

2525
/// Defines that a type will be able to parse socket.io-protocol messages.
2626
public protocol SocketParsable : AnyObject {
27+
/// if `true`, disable parsing event payloads
28+
var disableEventMessageParsing: Bool { get }
29+
2730
// MARK: Methods
2831

2932
/// Called when the engine has received some binary data that should be attached to a packet.
@@ -118,6 +121,10 @@ public extension SocketParsable where Self: SocketManagerSpec & SocketDataBuffer
118121

119122
var dataArray = String(message.utf16[message.utf16.index(reader.currentIndex, offsetBy: 1)...])!
120123

124+
if type == .event, self.disableEventMessageParsing {
125+
return SocketPacket(type: type, data: ["rawMessage", dataArray], id: Int(idString) ?? -1, nsp: namespace, placeholders: placeholders)
126+
}
127+
121128
if (type == .error || type == .connect) && !dataArray.hasPrefix("[") && !dataArray.hasSuffix("]") {
122129
dataArray = "[" + dataArray + "]"
123130
}

0 commit comments

Comments
 (0)