Skip to content

Commit 31e4c60

Browse files
committed
Merge pull request #373 from socketio/development
Development
2 parents c237197 + ab536e2 commit 31e4c60

13 files changed

+135
-315
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Carthage
8888
-----------------
8989
Add this line to your `Cartfile`:
9090
```
91-
github "socketio/socket.io-client-swift" ~> 6.1.1 # Or latest version
91+
github "socketio/socket.io-client-swift" ~> 6.1.2 # Or latest version
9292
```
9393

9494
Run `carthage update --platform ios,macosx`.
@@ -102,7 +102,7 @@ source 'https://github.com/CocoaPods/Specs.git'
102102
platform :ios, '8.0'
103103
use_frameworks!
104104

105-
pod 'Socket.IO-Client-Swift', '~> 6.1.1' # Or latest version
105+
pod 'Socket.IO-Client-Swift', '~> 6.1.2' # Or latest version
106106
```
107107

108108
Install pods:
@@ -130,7 +130,7 @@ CocoaSeeds
130130
Add this line to your `Seedfile`:
131131

132132
```
133-
github "socketio/socket.io-client-swift", "v6.1.1", :files => "Source/*.swift" # Or latest version
133+
github "socketio/socket.io-client-swift", "v6.1.2", :files => "Source/*.swift" # Or latest version
134134
```
135135

136136
Run `seed install`.

Socket.IO-Client-Swift.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22
s.name = "Socket.IO-Client-Swift"
33
s.module_name = "SocketIOClientSwift"
4-
s.version = "6.1.1"
4+
s.version = "6.1.2"
55
s.summary = "Socket.IO-client for iOS and OS X"
66
s.description = <<-DESC
77
Socket.IO-client for iOS and OS X.
@@ -14,7 +14,7 @@ Pod::Spec.new do |s|
1414
s.ios.deployment_target = '8.0'
1515
s.osx.deployment_target = '10.10'
1616
s.tvos.deployment_target = '9.0'
17-
s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v6.1.1' }
17+
s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v6.1.2' }
1818
s.source_files = "Source/**/*.swift"
1919
s.requires_arc = true
2020
# s.dependency 'Starscream', '~> 0.9' # currently this repo includes Starscream swift files

Socket.IO-Client-Swift.xcodeproj/project.pbxproj

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,6 @@
101101
74171EC51C10CD240062D398 /* SocketTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E601C10CD240062D398 /* SocketTypes.swift */; };
102102
74171EC71C10CD240062D398 /* SocketTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E601C10CD240062D398 /* SocketTypes.swift */; };
103103
74171EC81C10CD240062D398 /* SocketTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E601C10CD240062D398 /* SocketTypes.swift */; };
104-
74171EC91C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; };
105-
74171ECA1C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; };
106-
74171ECB1C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; };
107-
74171ECD1C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; };
108-
74171ECE1C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; };
109104
74171ECF1C10CD240062D398 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E621C10CD240062D398 /* WebSocket.swift */; };
110105
74171ED01C10CD240062D398 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E621C10CD240062D398 /* WebSocket.swift */; };
111106
74171ED11C10CD240062D398 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E621C10CD240062D398 /* WebSocket.swift */; };
@@ -190,7 +185,6 @@
190185
74171E5E1C10CD240062D398 /* SocketParsable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketParsable.swift; path = Source/SocketParsable.swift; sourceTree = "<group>"; };
191186
74171E5F1C10CD240062D398 /* SocketStringReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketStringReader.swift; path = Source/SocketStringReader.swift; sourceTree = "<group>"; };
192187
74171E601C10CD240062D398 /* SocketTypes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketTypes.swift; path = Source/SocketTypes.swift; sourceTree = "<group>"; };
193-
74171E611C10CD240062D398 /* SwiftRegex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SwiftRegex.swift; path = Source/SwiftRegex.swift; sourceTree = "<group>"; };
194188
74171E621C10CD240062D398 /* WebSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WebSocket.swift; path = Source/WebSocket.swift; sourceTree = "<group>"; };
195189
741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketEngineTest.swift; sourceTree = "<group>"; };
196190
7420CB781C49629E00956AA4 /* SocketEnginePollable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketEnginePollable.swift; path = Source/SocketEnginePollable.swift; sourceTree = "<group>"; };
@@ -375,7 +369,6 @@
375369
74171E5E1C10CD240062D398 /* SocketParsable.swift */,
376370
74171E5F1C10CD240062D398 /* SocketStringReader.swift */,
377371
74171E601C10CD240062D398 /* SocketTypes.swift */,
378-
74171E611C10CD240062D398 /* SwiftRegex.swift */,
379372
74171E621C10CD240062D398 /* WebSocket.swift */,
380373
);
381374
name = Source;
@@ -638,7 +631,6 @@
638631
74171EAB1C10CD240062D398 /* SocketLogger.swift in Sources */,
639632
74171E991C10CD240062D398 /* SocketIOClient.swift in Sources */,
640633
74171E8D1C10CD240062D398 /* SocketEventHandler.swift in Sources */,
641-
74171EC91C10CD240062D398 /* SwiftRegex.swift in Sources */,
642634
74171E7B1C10CD240062D398 /* SocketEngineClient.swift in Sources */,
643635
74171ECF1C10CD240062D398 /* WebSocket.swift in Sources */,
644636
74171EB11C10CD240062D398 /* SocketPacket.swift in Sources */,
@@ -663,7 +655,6 @@
663655
74171E8E1C10CD240062D398 /* SocketEventHandler.swift in Sources */,
664656
74171E7C1C10CD240062D398 /* SocketEngineClient.swift in Sources */,
665657
74171E821C10CD240062D398 /* SocketEnginePacketType.swift in Sources */,
666-
74171ECA1C10CD240062D398 /* SwiftRegex.swift in Sources */,
667658
74171EB21C10CD240062D398 /* SocketPacket.swift in Sources */,
668659
741F39EE1BD025D80026C9CC /* SocketEngineTest.swift in Sources */,
669660
74171EBE1C10CD240062D398 /* SocketStringReader.swift in Sources */,
@@ -697,7 +688,6 @@
697688
74171EAD1C10CD240062D398 /* SocketLogger.swift in Sources */,
698689
74171E9B1C10CD240062D398 /* SocketIOClient.swift in Sources */,
699690
74171E8F1C10CD240062D398 /* SocketEventHandler.swift in Sources */,
700-
74171ECB1C10CD240062D398 /* SwiftRegex.swift in Sources */,
701691
74171E7D1C10CD240062D398 /* SocketEngineClient.swift in Sources */,
702692
74171ED11C10CD240062D398 /* WebSocket.swift in Sources */,
703693
74171EB31C10CD240062D398 /* SocketPacket.swift in Sources */,
@@ -742,7 +732,6 @@
742732
74171EAF1C10CD240062D398 /* SocketLogger.swift in Sources */,
743733
74171E9D1C10CD240062D398 /* SocketIOClient.swift in Sources */,
744734
74171E911C10CD240062D398 /* SocketEventHandler.swift in Sources */,
745-
74171ECD1C10CD240062D398 /* SwiftRegex.swift in Sources */,
746735
74171E7F1C10CD240062D398 /* SocketEngineClient.swift in Sources */,
747736
74171ED31C10CD240062D398 /* WebSocket.swift in Sources */,
748737
74171EB51C10CD240062D398 /* SocketPacket.swift in Sources */,
@@ -767,7 +756,6 @@
767756
74171E921C10CD240062D398 /* SocketEventHandler.swift in Sources */,
768757
74171E801C10CD240062D398 /* SocketEngineClient.swift in Sources */,
769758
74171E861C10CD240062D398 /* SocketEnginePacketType.swift in Sources */,
770-
74171ECE1C10CD240062D398 /* SwiftRegex.swift in Sources */,
771759
74171EB61C10CD240062D398 /* SocketPacket.swift in Sources */,
772760
57634A2A1BD9B46D00E19CD7 /* SocketEngineTest.swift in Sources */,
773761
74171EC21C10CD240062D398 /* SocketStringReader.swift in Sources */,

SocketIO-MacTests/SocketBasicPacketTest.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,20 @@ class SocketBasicPacketTest: XCTestCase {
145145
XCTAssertEqual(packet.packetString, expectedSendString)
146146
XCTAssertEqual(packet.binary, [data2, data])
147147
}
148+
149+
func testBinaryStringPlaceholderInMessage() {
150+
let engineString = "52-[\"test\",\"~~0\",{\"num\":0,\"_placeholder\":true},{\"num\":1,\"_placeholder\":true}]"
151+
let socket = SocketIOClient(socketURL: NSURL())
152+
socket.setTestable()
153+
154+
if case let .Right(packet) = socket.parseString(engineString) {
155+
var packet = packet
156+
XCTAssertEqual(packet.event, "test")
157+
packet.addData(data)
158+
packet.addData(data2)
159+
XCTAssertEqual(packet.args[0] as? String, "~~0")
160+
} else {
161+
XCTFail()
162+
}
163+
}
148164
}

SocketIO-MacTests/SocketParserTest.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ class SocketParserTest: XCTestCase {
1818
"25[\"test\"]": ("/", ["test"], [], 5),
1919
"2[\"test\",\"~~0\"]": ("/", ["test", "~~0"], [], -1),
2020
"2/swift,[\"testArrayEmitReturn\",[\"test3\",\"test4\"]]": ("/swift", ["testArrayEmitReturn", ["test3", "test4"]], [], -1),
21-
"51-/swift,[\"testMultipleItemsWithBufferEmitReturn\",[1,2],{\"test\":\"bob\"},25,\"polo\",{\"_placeholder\":true,\"num\":0}]": ("/swift", ["testMultipleItemsWithBufferEmitReturn", [1, 2], ["test": "bob"], 25, "polo", "~~0"], [], -1),
21+
"51-/swift,[\"testMultipleItemsWithBufferEmitReturn\",[1,2],{\"test\":\"bob\"},25,\"polo\",{\"_placeholder\":true,\"num\":0}]": ("/swift", ["testMultipleItemsWithBufferEmitReturn", [1, 2], ["test": "bob"], 25, "polo", ["_placeholder": true, "num": 0]], [], -1),
2222
"3/swift,0[[\"test3\",\"test4\"]]": ("/swift", [["test3", "test4"]], [], 0),
23-
"61-/swift,19[[1,2],{\"test\":\"bob\"},25,\"polo\",{\"_placeholder\":true,\"num\":0}]": ("/swift", [ [1, 2], ["test": "bob"], 25, "polo", "~~0"], [], 19),
23+
"61-/swift,19[[1,2],{\"test\":\"bob\"},25,\"polo\",{\"_placeholder\":true,\"num\":0}]":
24+
("/swift", [ [1, 2], ["test": "bob"], 25, "polo", ["_placeholder": true, "num": 0]], [], 19),
2425
"4/swift,": ("/swift", [], [], -1),
2526
"0/swift": ("/swift", [], [], -1),
2627
"1/swift": ("/swift", [], [], -1),
@@ -123,8 +124,8 @@ class SocketParserTest: XCTestCase {
123124
if case let .Right(packet) = packet {
124125
XCTAssertEqual(packet.type, SocketPacket.PacketType(rawValue: Int(type) ?? -1)!)
125126
XCTAssertEqual(packet.nsp, validValues.0)
126-
XCTAssertTrue((packet.data as NSArray).isEqualToArray(validValues.1))
127-
XCTAssertTrue((packet.binary as NSArray).isEqualToArray(validValues.2))
127+
XCTAssertTrue((packet.data as NSArray).isEqualToArray(validValues.1), "\(packet.data)")
128+
XCTAssertTrue((packet.binary as NSArray).isEqualToArray(validValues.2), "\(packet.binary)")
128129
XCTAssertEqual(packet.id, validValues.3)
129130
} else {
130131
XCTFail()

Source/SocketEngine.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe
6262

6363
private weak var sessionDelegate: NSURLSessionDelegate?
6464

65-
private typealias Probe = (msg: String, type: SocketEnginePacketType, data: [NSData])
66-
private typealias ProbeWaitQueue = [Probe]
67-
6865
private let logType = "SocketEngine"
6966
private let url: NSURL
7067

@@ -74,6 +71,7 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe
7471
pongsMissedMax = Int(pingTimeout / (pingInterval ?? 25))
7572
}
7673
}
74+
7775
private var pongsMissed = 0
7876
private var pongsMissedMax = 0
7977
private var probeWait = ProbeWaitQueue()
@@ -387,7 +385,7 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe
387385
doPoll()
388386
}
389387

390-
client?.engineDidOpen?("Connect")
388+
client?.engineDidOpen("Connect")
391389
}
392390
} catch {
393391
didError("Error parsing open packet")

Source/SocketEngineClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import Foundation
2828
@objc public protocol SocketEngineClient {
2929
func engineDidError(reason: String)
3030
func engineDidClose(reason: String)
31-
optional func engineDidOpen(reason: String)
31+
func engineDidOpen(reason: String)
3232
func parseEngineMessage(msg: String)
3333
func parseEngineBinaryData(data: NSData)
3434
}

Source/SocketIOClient.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable
191191
DefaultSocketLogger.Logger.log("Disconnected: %@", type: logType, args: reason)
192192

193193
status = .Disconnected
194-
reconnects = false
195194

196195
// Make sure the engine is actually dead.
197196
engine?.disconnect(reason)
@@ -205,7 +204,6 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable
205204

206205
DefaultSocketLogger.Logger.log("Closing socket", type: logType)
207206

208-
reconnects = false
209207
didDisconnect("Disconnect")
210208
}
211209

@@ -286,6 +284,10 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable
286284

287285
handleEvent("error", data: [reason], isInternalMessage: true)
288286
}
287+
288+
public func engineDidOpen(reason: String) {
289+
DefaultSocketLogger.Logger.log(reason, type: "SocketEngineClient")
290+
}
289291

290292
// Called when the socket gets an ack for something it sent
291293
func handleAck(ack: Int, data: [AnyObject]) {
@@ -298,9 +300,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable
298300

299301
/// Causes an event to be handled. Only use if you know what you're doing.
300302
public func handleEvent(event: String, data: [AnyObject], isInternalMessage: Bool, withAck ack: Int = -1) {
301-
guard status == .Connected || isInternalMessage else {
302-
return
303-
}
303+
guard status == .Connected || isInternalMessage else { return }
304304

305305
DefaultSocketLogger.Logger.log("Handling event: %@ with data: %@", type: logType, args: event, data ?? "")
306306

@@ -335,14 +335,14 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable
335335
public func off(event: String) {
336336
DefaultSocketLogger.Logger.log("Removing handler for event: %@", type: logType, args: event)
337337

338-
handlers = handlers.filter { $0.event != event }
338+
handlers = handlers.filter({ $0.event != event })
339339
}
340340

341341
/// Removes a handler with the specified UUID gotten from an `on` or `once`
342342
public func off(id id: NSUUID) {
343343
DefaultSocketLogger.Logger.log("Removing handler with id: %@", type: logType, args: id)
344344

345-
handlers = handlers.filter { $0.id != id }
345+
handlers = handlers.filter({ $0.id != id })
346346
}
347347

348348
/// Adds a handler for an event.

Source/SocketPacket.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct SocketPacket {
6161
return createPacketString()
6262
}
6363

64-
init(type: SocketPacket.PacketType, data: [AnyObject] = [AnyObject](), id: Int = -1,
64+
init(type: PacketType, data: [AnyObject] = [AnyObject](), id: Int = -1,
6565
nsp: String, placeholders: Int = 0, binary: [NSData] = [NSData]()) {
6666
self.data = data
6767
self.id = id
@@ -97,7 +97,7 @@ struct SocketPacket {
9797
let jsonSend = try NSJSONSerialization.dataWithJSONObject(data,
9898
options: NSJSONWritingOptions(rawValue: 0))
9999
guard let jsonString = String(data: jsonSend, encoding: NSUTF8StringEncoding) else {
100-
return "[]"
100+
return message + "[]"
101101
}
102102

103103
restOfMessage = jsonString
@@ -187,19 +187,21 @@ struct SocketPacket {
187187
data = data.map(_fillInPlaceholders)
188188
}
189189

190-
// Helper method that looks for placeholder strings
190+
// Helper method that looks for placeholders
191191
// If object is a collection it will recurse
192-
// Returns the object if it is not a placeholder string or the corresponding
192+
// Returns the object if it is not a placeholder or the corresponding
193193
// binary data
194194
private func _fillInPlaceholders(object: AnyObject) -> AnyObject {
195195
switch object {
196-
case let string as String where string["~~(\\d)"].groups() != nil:
197-
return binary[Int(string["~~(\\d)"].groups()![1])!]
198196
case let dict as NSDictionary:
199-
return dict.reduce(NSMutableDictionary(), combine: {cur, keyValue in
200-
cur[keyValue.0 as! NSCopying] = _fillInPlaceholders(keyValue.1)
201-
return cur
202-
})
197+
if dict["_placeholder"] as? Bool ?? false {
198+
return binary[dict["num"] as! Int]
199+
} else {
200+
return dict.reduce(NSMutableDictionary(), combine: {cur, keyValue in
201+
cur[keyValue.0 as! NSCopying] = _fillInPlaceholders(keyValue.1)
202+
return cur
203+
})
204+
}
203205
case let arr as [AnyObject]:
204206
return arr.map(_fillInPlaceholders)
205207
default:
@@ -227,7 +229,7 @@ extension SocketPacket {
227229
static func packetFromEmit(items: [AnyObject], id: Int, nsp: String, ack: Bool) -> SocketPacket {
228230
let (parsedData, binary) = deconstructData(items)
229231
let packet = SocketPacket(type: findType(binary.count, ack: ack), data: parsedData,
230-
id: id, nsp: nsp, placeholders: -1, binary: binary)
232+
id: id, nsp: nsp, binary: binary)
231233

232234
return packet
233235
}

Source/SocketParsable.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ extension SocketParsable {
3535
private func handleConnect(p: SocketPacket) {
3636
if p.nsp == "/" && nsp != "/" {
3737
joinNamespace(nsp)
38-
} else if p.nsp != "/" && nsp == "/" {
39-
didConnect()
4038
} else {
4139
didConnect()
4240
}
@@ -91,8 +89,7 @@ extension SocketParsable {
9189
}
9290

9391
if !parser.hasNext {
94-
return .Right(SocketPacket(type: type, id: -1,
95-
nsp: namespace ?? "/", placeholders: placeholders))
92+
return .Right(SocketPacket(type: type, nsp: namespace, placeholders: placeholders))
9693
}
9794

9895
var idString = ""
@@ -111,12 +108,11 @@ extension SocketParsable {
111108
}
112109

113110
let d = message[parser.currentIndex.advancedBy(1)..<message.endIndex]
114-
let noPlaceholders = d["(\\{\"_placeholder\":true,\"num\":(\\d*)\\})"] <~ "\"~~$2\""
115111

116-
switch parseData(noPlaceholders) {
112+
switch parseData(d) {
117113
case let .Left(err):
118114
// Errors aren't always enclosed in an array
119-
if case let .Right(data) = parseData("\([noPlaceholders as AnyObject])") {
115+
if case let .Right(data) = parseData("\([d as AnyObject])") {
120116
return .Right(SocketPacket(type: type, data: data, id: Int(idString) ?? -1,
121117
nsp: namespace, placeholders: placeholders))
122118
} else {

0 commit comments

Comments
 (0)