@@ -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