Skip to content

Commit 922ed4b

Browse files
committed
fix #61
1 parent eab7311 commit 922ed4b

File tree

2 files changed

+55
-56
lines changed

2 files changed

+55
-56
lines changed

SocketIOClientSwift/SocketEngine.swift

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
3939
private let parseQueue = dispatch_queue_create("engineParseQueue", DISPATCH_QUEUE_SERIAL)
4040
private let handleQueue = dispatch_queue_create("engineHandleQueue", DISPATCH_QUEUE_SERIAL)
4141
private let session:NSURLSession!
42+
4243
private var closed = false
4344
private var _connected = false
4445
private var fastUpgrade = false
@@ -157,7 +158,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
157158

158159
if params != nil {
159160
let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" ").invertedSet
160-
161+
161162
for (key, value) in params! {
162163
let keyEsc = key.stringByAddingPercentEncodingWithAllowedCharacters(
163164
allowedCharacterSet)!
@@ -210,12 +211,12 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
210211

211212
self.waitingForPoll = true
212213
let req = NSMutableURLRequest(URL: NSURL(string: self.urlPolling! + "&sid=\(self.sid)&b64=1")!)
213-
214+
214215
if self.cookies != nil {
215216
let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(self.cookies!)
216217
req.allHTTPHeaderFields = headers
217218
}
218-
219+
219220
self.doRequest(req)
220221
}
221222

@@ -299,12 +300,12 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
299300
self.postWait.removeAll(keepCapacity: false)
300301

301302
let req = NSMutableURLRequest(URL: NSURL(string: self.urlPolling! + "&sid=\(self.sid)")!)
302-
303+
303304
if self.cookies != nil {
304305
let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(self.cookies!)
305306
req.allHTTPHeaderFields = headers
306307
}
307-
308+
308309
req.HTTPMethod = "POST"
309310
req.setValue("text/plain; charset=UTF-8", forHTTPHeaderField: "Content-Type")
310311

@@ -540,22 +541,19 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
540541
}
541542

542543
return
543-
} else {
544-
if message.hasPrefix("b4") {
545-
// binary in base64 string
546-
547-
message.removeRange(Range<String.Index>(start: message.startIndex,
548-
end: advance(message.startIndex, 2)))
549-
550-
if let data = NSData(base64EncodedString: message,
551-
options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
552-
where self.client != nil {
553-
// println("sending \(data)")
554-
555-
dispatch_async(self.client!.handleQueue) {[weak self] in
556-
self?.client?.parseBinaryData(data)
557-
}
558-
}
544+
} else if message.hasPrefix("b4") {
545+
// binary in base64 string
546+
message.removeRange(Range<String.Index>(start: message.startIndex,
547+
end: advance(message.startIndex, 2)))
548+
549+
if let data = NSData(base64EncodedString: message,
550+
options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
551+
where self.client != nil {
552+
// println("sending \(data)")
553+
554+
dispatch_async(self.client!.handleQueue) {[weak self] in
555+
self?.client?.parseBinaryData(data)
556+
}
559557
}
560558
}
561559
}

SocketIOClientSwift/SocketPacket.swift

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)