Skip to content

Commit 4832be5

Browse files
committed
better parsing
1 parent cbc59d6 commit 4832be5

File tree

5 files changed

+65
-93
lines changed

5 files changed

+65
-93
lines changed

SwiftIO/SocketEngine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public class SocketEngine: NSObject, WebSocketDelegate {
8181
}
8282
var ws:WebSocket?
8383

84-
init(client:SocketEngineClient, forcePolling:Bool = false, withCookies cookies:[NSHTTPCookie]?) {
84+
init(client:SocketEngineClient, forcePolling:Bool, withCookies cookies:[NSHTTPCookie]?) {
8585
self.client = client
8686
self.forcePolling = forcePolling
8787
self.cookies = cookies

SwiftIO/SocketEvent.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,4 @@ class SocketEvent {
247247

248248
return false
249249
}
250-
}
250+
}

SwiftIO/SocketEventHandler.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@ public typealias NormalCallback = (NSArray?, AckEmitter?) -> Void
2828
public typealias AnyHandler = (event:String, items:AnyObject?)
2929
public typealias AckEmitter = (AnyObject...) -> Void
3030

31-
private func emitAckCallback(socket:SocketIOClient, num:Int, type:Int) -> AckEmitter {
32-
func emitter(items:AnyObject...) {
31+
private func emitAckCallback(socket:SocketIOClient, num:Int, type:Int)
32+
// Curried
33+
(items:AnyObject...) -> Void {
3334
socket.emitAck(num, withData: items, withAckType: type)
34-
}
35-
36-
return emitter
3735
}
3836

3937
class SocketEventHandler {

SwiftIO/SocketIOClient.swift

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public class SocketIOClient: NSObject, SocketEngineClient {
7575
return self._sid
7676
}
7777

78+
/**
79+
Create a new SocketIOClient. opts can be omitted
80+
*/
7881
public init(var socketURL:String, opts:NSDictionary? = nil) {
7982
if socketURL["https://"].matches().count != 0 {
8083
self._secure = true
@@ -127,7 +130,9 @@ public class SocketIOClient: NSObject, SocketEngineClient {
127130
self.init(socketURL: socketURL, opts: options)
128131
}
129132

130-
// Closes the socket
133+
/**
134+
Closes the socket. Only reopen the same socket if you know what you're doing.
135+
*/
131136
public func close() {
132137
self._closed = true
133138
self._connecting = false
@@ -136,7 +141,9 @@ public class SocketIOClient: NSObject, SocketEngineClient {
136141
self.engine?.close()
137142
}
138143

139-
// Connects to the server
144+
/**
145+
Connect to the server.
146+
*/
140147
public func connect() {
141148
if self.closed {
142149
println("Warning! This socket was previously closed. This might be dangerous!")
@@ -146,7 +153,9 @@ public class SocketIOClient: NSObject, SocketEngineClient {
146153
self.engine?.open()
147154
}
148155

149-
// Connect to the server using params
156+
/**
157+
Connect to the server with params that will be passed on connection.
158+
*/
150159
public func connectWithParams(params:[String: AnyObject]) {
151160
if self.closed {
152161
println("Warning! This socket was previously closed. This might be dangerous!")
@@ -174,7 +183,7 @@ public class SocketIOClient: NSObject, SocketEngineClient {
174183
self.handleEvent("connect", data: nil, isInternalMessage: false)
175184
}
176185

177-
// Server wants us to die
186+
/// Server wants us to die
178187
func didForceClose(#message:String) {
179188
self._closed = true
180189
self._connected = false
@@ -184,9 +193,9 @@ public class SocketIOClient: NSObject, SocketEngineClient {
184193
self.handleEvent("disconnect", data: message, isInternalMessage: true)
185194
}
186195

187-
// Sends a message with multiple args
188-
// If a message contains binary we have to send those
189-
// seperately.
196+
/**
197+
Send a message to the server
198+
*/
190199
public func emit(event:String, _ args:AnyObject...) {
191200
if !self.connected {
192201
return
@@ -198,11 +207,17 @@ public class SocketIOClient: NSObject, SocketEngineClient {
198207
}
199208
}
200209

201-
// Objc doesn't have variadics
210+
/**
211+
Same as emit, but meant for Objective-C
212+
*/
202213
public func emitObjc(event:String, withItems items:[AnyObject]) {
203214
self.emit(event, items)
204215
}
205216

217+
/**
218+
Sends a message to the server, requesting an ack. Use the onAck method of SocketAckHandler to add
219+
an ack.
220+
*/
206221
public func emitWithAck(event:String, _ args:AnyObject...) -> SocketAckHandler {
207222
if !self.connected {
208223
return SocketAckHandler(event: "fail", socket: self)
@@ -221,6 +236,9 @@ public class SocketIOClient: NSObject, SocketEngineClient {
221236
return ackHandler
222237
}
223238

239+
/**
240+
Same as emitWithAck, but for Objective-C
241+
*/
224242
public func emitWithAckObjc(event:String, withItems items:[AnyObject]) -> SocketAckHandler {
225243
return self.emitWithAck(event, items)
226244
}
@@ -265,6 +283,7 @@ public class SocketIOClient: NSObject, SocketEngineClient {
265283
return
266284
}
267285

286+
// println("sending ack: \(ack) \(data)")
268287
let (items, hasBinary, emitDatas) = SocketParser.parseEmitArgs(data!)
269288
var str:String
270289

@@ -311,7 +330,9 @@ public class SocketIOClient: NSObject, SocketEngineClient {
311330
}
312331
}
313332

314-
// Handles events
333+
/**
334+
Causes an event to be handled. Only use if you know what you're doing.
335+
*/
315336
public func handleEvent(event:String, data:AnyObject?, isInternalMessage:Bool = false,
316337
wantsAck ack:Int? = nil, withAckType ackType:Int = 3) {
317338
// println("Should do event: \(event) with data: \(data)")
@@ -363,18 +384,24 @@ public class SocketIOClient: NSObject, SocketEngineClient {
363384
}
364385
}
365386

366-
// Adds handler for an event
387+
/**
388+
Adds a handler for an event.
389+
*/
367390
public func on(name:String, callback:NormalCallback) {
368391
let handler = SocketEventHandler(event: name, callback: callback)
369392
self.handlers.append(handler)
370393
}
371394

372-
// Adds a handler for any event
395+
/**
396+
Adds a handler that will be called on every event.
397+
*/
373398
public func onAny(handler:(AnyHandler) -> Void) {
374399
self.anyHandler = handler
375400
}
376401

377-
// Opens the connection to the socket
402+
/**
403+
Same as connect
404+
*/
378405
public func open() {
379406
self.connect()
380407
}

SwiftIO/SocketParser.swift

Lines changed: 21 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -220,39 +220,20 @@ class SocketParser {
220220
return
221221
}
222222

223-
var messageGroups:[String]?
224-
225223
let type = stringMessage.removeAtIndex(stringMessage.startIndex)
226224

227225
if type == "2" {
228-
if let groups = stringMessage["(\\d*)\\/?(\\w*)?,?(\\d*)?\\[\"(.*?)\",?(.*?)?\\]$",
226+
if let groups = stringMessage["(\\/(\\w*))?,?(\\d*)?\\[\"(.*?)\",?(.*?)?\\]$",
229227
NSRegularExpressionOptions.DotMatchesLineSeparators].groups() {
230-
messageGroups = groups
231-
232-
var mesNum = messageGroups![1]
233-
var ackNum:String
234-
var namespace:String?
235-
236-
if mesNum == "" {
237-
ackNum = ""
238-
} else if messageGroups![3] != "" {
239-
ackNum = messageGroups![3]
240-
} else {
241-
let range = Range<String.Index>(start: mesNum.startIndex,
242-
end: advance(mesNum.startIndex, 1))
243-
mesNum.replaceRange(range, with: "")
244-
ackNum = mesNum
245-
}
246-
247-
namespace = messageGroups![2]
228+
let namespace = groups[2]
229+
let ackNum = groups[3]
230+
let event = groups[4]
231+
let data = "[\(groups[5])]"
248232

249233
if namespace == "" && socket.nsp != nil {
250234
return
251235
}
252236

253-
let event = messageGroups![4]
254-
let data = "[\(messageGroups![5])]"
255-
256237
if let parsed:AnyObject = self.parseData(data) {
257238
if ackNum == "" {
258239
socket.handleEvent(event, data: parsed)
@@ -264,25 +245,16 @@ class SocketParser {
264245
}
265246
}
266247
} else if type == "3" {
267-
if let ackGroup = stringMessage["(\\d*)\\/?(\\w*)?,?(\\d*)?\\[(.*?)?\\]$",
248+
if let ackGroup = stringMessage["(\\/(\\w*))?,?(\\d*)?\\[(.*?)?\\]$",
268249
NSRegularExpressionOptions.DotMatchesLineSeparators].groups() {
269-
messageGroups = ackGroup
270-
271-
let arr = Array(messageGroups![1])
272-
var ackNum:String
273-
let nsp = messageGroups![2]
250+
let nsp = ackGroup[2]
251+
let ackNum = ackGroup[3]
252+
let ackData:AnyObject? = self.parseData("[\(ackGroup[4])]")
274253

275254
if nsp == "" && socket.nsp != nil {
276255
return
277256
}
278257

279-
if nsp == "" {
280-
ackNum = String(arr[0...arr.count-1])
281-
} else {
282-
ackNum = messageGroups![3]
283-
}
284-
285-
let ackData:AnyObject? = self.parseData("[\(messageGroups![4])]")
286258
socket.handleAck(ackNum.toInt()!, data: ackData)
287259
}
288260
} else {
@@ -346,32 +318,17 @@ class SocketParser {
346318
// Tries to parse a message that contains binary
347319
class func parseBinaryMessage(var message:String, socket:SocketIOClient) {
348320
// NSLog(message)
349-
var binaryGroup:[String]?
350321

351322
let type = message.removeAtIndex(message.startIndex)
352323

353324
if type == "5" {
354-
if let groups = message["^(\\d*)-\\/?(\\w*)?,?(\\d*)?\\[(\".*?\")?,?(.*)?\\]$",
325+
if let groups = message["^(\\d*)-(\\/(\\w*))?,?(\\d*)?\\[\"(.*?)\",?(.*)?\\]$",
355326
NSRegularExpressionOptions.DotMatchesLineSeparators].groups() {
356-
binaryGroup = groups
357-
358-
var ackNum:String
359-
var event:String
360-
var mutMessageObject:String
361-
var namespace:String?
362-
let numberOfPlaceholders = binaryGroup![1]
363-
364-
namespace = binaryGroup![2]
365-
if binaryGroup![3] != "" {
366-
ackNum = binaryGroup![3] as String
367-
} else if socket.nsp == nil && binaryGroup![2] != "" {
368-
ackNum = binaryGroup![2]
369-
} else {
370-
ackNum = ""
371-
}
372-
373-
event = (binaryGroup![4]["\""] ~= "") as String
374-
mutMessageObject = binaryGroup![5]
327+
let numberOfPlaceholders = groups[1]
328+
let namespace = groups[3]
329+
let ackNum = groups[4]
330+
let event = groups[5]
331+
let mutMessageObject = groups[6]
375332

376333
if namespace == "" && socket.nsp != nil {
377334
return
@@ -393,26 +350,16 @@ class SocketParser {
393350
socket.waitingData.append(mes)
394351
}
395352
} else if type == "6" {
396-
if let groups = message["^(\\d*)-\\/?(\\w*)?,?(\\d*)?\\[(.*?)?\\]$",
353+
if let groups = message["^(\\d*)-(\\/(\\w*))?,?(\\d*)?\\[(.*?)?\\]$",
397354
NSRegularExpressionOptions.DotMatchesLineSeparators].groups() {
398-
binaryGroup = groups
399-
400-
let numberOfPlaceholders = binaryGroup![1]
401-
var ackNum:String
402-
var nsp:String
355+
let numberOfPlaceholders = groups[1]
356+
let namespace = groups[3]
357+
let ackNum = groups[4]
358+
let mutMessageObject = groups[5]
403359

404-
if binaryGroup![3] == "" {
405-
ackNum = binaryGroup![2]
406-
nsp = ""
407-
} else {
408-
ackNum = binaryGroup![3]
409-
nsp = binaryGroup![2]
410-
}
411-
412-
if nsp == "" && socket.nsp != nil {
360+
if namespace == "" && socket.nsp != nil {
413361
return
414362
}
415-
var mutMessageObject = binaryGroup![4]
416363
let placeholdersRemoved = mutMessageObject["(\\{\"_placeholder\":true,\"num\":(\\d*)\\})"]
417364
~= "\"~~$2\""
418365

0 commit comments

Comments
 (0)