@@ -87,54 +87,44 @@ struct SocketPacket {
8787 }
8888
8989 private func completeMessage(message: String, ack: Bool) -> String {
90- var restOfMessage = ""
90+ let restOfMessage: String
9191
9292 if data.count == 0 {
93- return message + "]"
93+ return message + "[ ]"
9494 }
9595
96- for arg in data {
97- if arg is NSDictionary || arg is [AnyObject] {
98- do {
99- let jsonSend = try NSJSONSerialization.dataWithJSONObject(arg,
100- options: NSJSONWritingOptions(rawValue: 0))
101- let jsonString = String(data: jsonSend, encoding: NSUTF8StringEncoding)
102-
103- restOfMessage += jsonString! + ","
104- } catch {
105- DefaultSocketLogger.Logger.error("Error creating JSON object in SocketPacket.completeMessage",
106- type: SocketPacket.logType)
107- }
108- } else if let str = arg as? String {
109- restOfMessage += "\"" + (((str["\n"] <~ "\\\\n")["\r"] <~ "\\\\r")["\""] <~ "\\\\\"") + "\","
110- } else if arg is NSNull {
111- restOfMessage += "null,"
112- } else {
113- restOfMessage += "\(arg),"
96+ do {
97+ let jsonSend = try NSJSONSerialization.dataWithJSONObject(data,
98+ options: NSJSONWritingOptions(rawValue: 0))
99+ guard let jsonString = String(data: jsonSend, encoding: NSUTF8StringEncoding) else {
100+ return "[]"
114101 }
102+
103+ restOfMessage = jsonString
104+ } catch {
105+ DefaultSocketLogger.Logger.error("Error creating JSON object in SocketPacket.completeMessage",
106+ type: SocketPacket.logType)
107+
108+ restOfMessage = "[]"
115109 }
116110
117- if restOfMessage != "" {
118- restOfMessage.removeAtIndex(restOfMessage.endIndex.predecessor())
119- }
120-
121- return message + restOfMessage + "]"
111+ return message + restOfMessage
122112 }
123113
124114 private func createAck() -> String {
125115 let message: String
126116
127117 if type == .Ack {
128118 if nsp == "/" {
129- message = "3\(id)[ "
119+ message = "3\(id)"
130120 } else {
131- message = "3\(nsp),\(id)[ "
121+ message = "3\(nsp),\(id)"
132122 }
133123 } else {
134124 if nsp == "/" {
135- message = "6\(binary.count)-\(id)[ "
125+ message = "6\(binary.count)-\(id)"
136126 } else {
137- message = "6\(binary.count)-\(nsp),\(id)[ "
127+ message = "6\(binary.count)-\(nsp),\(id)"
138128 }
139129 }
140130
@@ -148,29 +138,29 @@ struct SocketPacket {
148138 if type == .Event {
149139 if nsp == "/" {
150140 if id == -1 {
151- message = "2[ "
141+ message = "2"
152142 } else {
153- message = "2\(id)[ "
143+ message = "2\(id)"
154144 }
155145 } else {
156146 if id == -1 {
157- message = "2\(nsp),[ "
147+ message = "2\(nsp),"
158148 } else {
159- message = "2\(nsp),\(id)[ "
149+ message = "2\(nsp),\(id)"
160150 }
161151 }
162152 } else {
163153 if nsp == "/" {
164154 if id == -1 {
165- message = "5\(binary.count)-[ "
155+ message = "5\(binary.count)-"
166156 } else {
167- message = "5\(binary.count)-\(id)[ "
157+ message = "5\(binary.count)-\(id)"
168158 }
169159 } else {
170160 if id == -1 {
171- message = "5\(binary.count)-\(nsp),[ "
161+ message = "5\(binary.count)-\(nsp),"
172162 } else {
173- message = "5\(binary.count)-\(nsp),\(id)[ "
163+ message = "5\(binary.count)-\(nsp),\(id)"
174164 }
175165 }
176166 }
0 commit comments