@@ -62,7 +62,7 @@ final class SocketPacket: Printable {
62
62
}
63
63
}
64
64
}
65
-
65
+
66
66
init ( type: PacketType ? , data: [ AnyObject ] ? = nil , nsp: String = " " ,
67
67
placeholders: Int ? = nil , id: Int ? = nil ) {
68
68
self . type = type
@@ -71,34 +71,34 @@ final class SocketPacket: Printable {
71
71
self . placeholders = placeholders
72
72
self . id = id
73
73
}
74
-
74
+
75
75
func getEvent( ) -> String {
76
76
return data? . removeAtIndex ( 0 ) as! String
77
77
}
78
-
78
+
79
79
func addData( data: NSData ) -> Bool {
80
80
if self . placeholders == self . currentPlace {
81
81
return true
82
82
}
83
-
83
+
84
84
self . binary. append ( data)
85
85
self . currentPlace++
86
-
86
+
87
87
if self . placeholders == self . currentPlace {
88
88
self . currentPlace = 0
89
89
return true
90
90
} else {
91
91
return false
92
92
}
93
93
}
94
-
94
+
95
95
func createMessageForEvent( event: String ) -> String {
96
96
let message : String
97
97
var jsonSendError : NSError ?
98
-
98
+
99
99
if self . binary. count == 0 {
100
100
self . type = PacketType . EVENT
101
-
101
+
102
102
if self . nsp == " / " {
103
103
if self . id == nil {
104
104
message = " 2[ \" \( event) \" "
@@ -114,7 +114,7 @@ final class SocketPacket: Printable {
114
114
}
115
115
} else {
116
116
self . type = PacketType . BINARY_EVENT
117
-
117
+
118
118
if self . nsp == " / " {
119
119
if self . id == nil {
120
120
message = " 5 \( self . binary. count) -[ \" \( event) \" "
@@ -129,87 +129,88 @@ final class SocketPacket: Printable {
129
129
}
130
130
}
131
131
}
132
-
132
+
133
133
return self . completeMessage ( message)
134
134
}
135
-
135
+
136
136
func createAck( ) -> String {
137
137
var msg : String
138
-
138
+
139
139
if self . binary. count == 0 {
140
140
self . type = PacketType . ACK
141
-
141
+
142
142
if nsp == " / " {
143
143
msg = " 3 \( self . id!) [ "
144
144
} else {
145
145
msg = " 3/ \( self . nsp) , \( self . id!) [ "
146
146
}
147
147
} else {
148
148
self . type = PacketType . BINARY_ACK
149
-
149
+
150
150
if nsp == " / " {
151
151
msg = " 6 \( self . binary. count) - \( self . id!) [ "
152
152
} else {
153
153
msg = " 6 \( self . binary. count) -/ \( self . nsp) , \( self . id!) [ "
154
154
}
155
155
}
156
-
156
+
157
157
return self . completeMessage ( msg, ack: true )
158
158
}
159
-
159
+
160
160
func completeMessage( var message: String , ack: Bool = false ) -> String {
161
161
var err : NSError ?
162
-
162
+
163
163
if self . data == nil || self . data!. count == 0 {
164
164
return message + " ] "
165
165
} else if !ack {
166
166
message += " , "
167
167
}
168
-
168
+
169
169
for arg in self . data! {
170
170
if arg is NSDictionary || arg is [ AnyObject ] {
171
171
let jsonSend = NSJSONSerialization . dataWithJSONObject ( arg,
172
172
options: NSJSONWritingOptions ( 0 ) , error: & err)
173
173
let jsonString = NSString ( data: jsonSend!, encoding: NSUTF8StringEncoding)
174
-
174
+
175
175
message += jsonString! as String
176
176
message += " , "
177
177
continue
178
+ } else if arg is NSNull {
179
+ message += " null, "
180
+ continue
178
181
}
179
-
182
+
180
183
if arg is String {
181
184
message += " \" \( arg) \" "
182
185
message += " , "
183
186
continue
184
187
}
185
-
188
+
186
189
message += " \( arg) "
187
190
message += " , "
188
191
}
189
-
192
+
190
193
if message != " " {
191
194
message. removeAtIndex ( message. endIndex. predecessor ( ) )
192
195
}
193
-
196
+
194
197
return message + " ] "
195
198
}
196
-
199
+
197
200
func fillInPlaceholders( ) {
198
201
var newArr = NSMutableArray ( array: self . data!)
199
-
202
+
200
203
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 ( ) !]
205
206
} else if self. data ? [ i] is NSDictionary || self . data ? [ i] is NSArray {
206
207
newArr [ i] = self . _fillInPlaceholders ( self . data![ i] )
207
208
}
208
209
}
209
-
210
+
210
211
self. data = newArr as [ AnyObject ]
211
212
}
212
-
213
+
213
214
private func _fillInPlaceholders( data: AnyObject) - > AnyObject {
214
215
if let str = data as? String {
215
216
if let num = str [ " ~~( \\ d) " ] . groups ( ) {
@@ -219,19 +220,19 @@ final class SocketPacket: Printable {
219
220
}
220
221
} else if let dict = data as? NSDictionary {
221
222
var newDict = NSMutableDictionary ( dictionary: dict)
222
-
223
+
223
224
for (key, value) in dict {
224
225
newDict [ key as! NSCopying ] = _fillInPlaceholders ( value)
225
226
}
226
-
227
+
227
228
return newDict
228
229
} else if let arr = data as? NSArray {
229
230
var newArr = NSMutableArray ( array: arr)
230
-
231
+
231
232
for i in 0 ..< arr. count {
232
233
newArr [ i] = _fillInPlaceholders ( arr [ i] )
233
234
}
234
-
235
+
235
236
return newArr
236
237
} else {
237
238
return data
0 commit comments