@@ -62,7 +62,7 @@ final class SocketPacket: Printable {
6262 }
6363 }
6464 }
65-
65+
6666 init ( type: PacketType ? , data: [ AnyObject ] ? = nil , nsp: String = " " ,
6767 placeholders: Int ? = nil , id: Int ? = nil ) {
6868 self . type = type
@@ -71,34 +71,34 @@ final class SocketPacket: Printable {
7171 self . placeholders = placeholders
7272 self . id = id
7373 }
74-
74+
7575 func getEvent( ) -> String {
7676 return data? . removeAtIndex ( 0 ) as! String
7777 }
78-
78+
7979 func addData( data: NSData ) -> Bool {
8080 if self . placeholders == self . currentPlace {
8181 return true
8282 }
83-
83+
8484 self . binary. append ( data)
8585 self . currentPlace++
86-
86+
8787 if self . placeholders == self . currentPlace {
8888 self . currentPlace = 0
8989 return true
9090 } else {
9191 return false
9292 }
9393 }
94-
94+
9595 func createMessageForEvent( event: String ) -> String {
9696 let message : String
9797 var jsonSendError : NSError ?
98-
98+
9999 if self . binary. count == 0 {
100100 self . type = PacketType . EVENT
101-
101+
102102 if self . nsp == " / " {
103103 if self . id == nil {
104104 message = " 2[ \" \( event) \" "
@@ -114,7 +114,7 @@ final class SocketPacket: Printable {
114114 }
115115 } else {
116116 self . type = PacketType . BINARY_EVENT
117-
117+
118118 if self . nsp == " / " {
119119 if self . id == nil {
120120 message = " 5 \( self . binary. count) -[ \" \( event) \" "
@@ -129,87 +129,88 @@ final class SocketPacket: Printable {
129129 }
130130 }
131131 }
132-
132+
133133 return self . completeMessage ( message)
134134 }
135-
135+
136136 func createAck( ) -> String {
137137 var msg : String
138-
138+
139139 if self . binary. count == 0 {
140140 self . type = PacketType . ACK
141-
141+
142142 if nsp == " / " {
143143 msg = " 3 \( self . id!) [ "
144144 } else {
145145 msg = " 3/ \( self . nsp) , \( self . id!) [ "
146146 }
147147 } else {
148148 self . type = PacketType . BINARY_ACK
149-
149+
150150 if nsp == " / " {
151151 msg = " 6 \( self . binary. count) - \( self . id!) [ "
152152 } else {
153153 msg = " 6 \( self . binary. count) -/ \( self . nsp) , \( self . id!) [ "
154154 }
155155 }
156-
156+
157157 return self . completeMessage ( msg, ack: true )
158158 }
159-
159+
160160 func completeMessage( var message: String , ack: Bool = false ) -> String {
161161 var err : NSError ?
162-
162+
163163 if self . data == nil || self . data!. count == 0 {
164164 return message + " ] "
165165 } else if !ack {
166166 message += " , "
167167 }
168-
168+
169169 for arg in self . data! {
170170 if arg is NSDictionary || arg is [ AnyObject ] {
171171 let jsonSend = NSJSONSerialization . dataWithJSONObject ( arg,
172172 options: NSJSONWritingOptions ( 0 ) , error: & err)
173173 let jsonString = NSString ( data: jsonSend!, encoding: NSUTF8StringEncoding)
174-
174+
175175 message += jsonString! as String
176176 message += " , "
177177 continue
178+ } else if arg is NSNull {
179+ message += " null, "
180+ continue
178181 }
179-
182+
180183 if arg is String {
181184 message += " \" \( arg) \" "
182185 message += " , "
183186 continue
184187 }
185-
188+
186189 message += " \( arg) "
187190 message += " , "
188191 }
189-
192+
190193 if message != " " {
191194 message. removeAtIndex ( message. endIndex. predecessor ( ) )
192195 }
193-
196+
194197 return message + " ] "
195198 }
196-
199+
197200 func fillInPlaceholders( ) {
198201 var newArr = NSMutableArray ( array: self . data!)
199-
202+
200203 for i in 0 ..< self . data!. count {
201- if let str = self . data ? [ i] as? String {
202- if let num = str [ " ~~( \\ d) " ] . groups ( ) {
203- newArr [ i] = self . binary [ num [ 1 ] . toInt ( ) !]
204- }
204+ if let str = self . data ? [ i] as? String , num = str [ " ~~( \\ d) " ] . groups ( ) {
205+ newArr [ i] = self . binary [ num [ 1 ] . toInt ( ) !]
205206 } else if self. data ? [ i] is NSDictionary || self . data ? [ i] is NSArray {
206207 newArr [ i] = self . _fillInPlaceholders ( self . data![ i] )
207208 }
208209 }
209-
210+
210211 self. data = newArr as [ AnyObject ]
211212 }
212-
213+
213214 private func _fillInPlaceholders( data: AnyObject) - > AnyObject {
214215 if let str = data as? String {
215216 if let num = str [ " ~~( \\ d) " ] . groups ( ) {
@@ -219,19 +220,19 @@ final class SocketPacket: Printable {
219220 }
220221 } else if let dict = data as? NSDictionary {
221222 var newDict = NSMutableDictionary ( dictionary: dict)
222-
223+
223224 for (key, value) in dict {
224225 newDict [ key as! NSCopying ] = _fillInPlaceholders ( value)
225226 }
226-
227+
227228 return newDict
228229 } else if let arr = data as? NSArray {
229230 var newArr = NSMutableArray ( array: arr)
230-
231+
231232 for i in 0 ..< arr. count {
232233 newArr [ i] = _fillInPlaceholders ( arr [ i] )
233234 }
234-
235+
235236 return newArr
236237 } else {
237238 return data
0 commit comments