Skip to content

Commit

Permalink
Merge pull request #373 from socketio/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
nuclearace committed May 24, 2016
2 parents c237197 + ab536e2 commit 31e4c60
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 315 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Carthage
-----------------
Add this line to your `Cartfile`:
```
github "socketio/socket.io-client-swift" ~> 6.1.1 # Or latest version
github "socketio/socket.io-client-swift" ~> 6.1.2 # Or latest version
```

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

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

Install pods:
Expand Down Expand Up @@ -130,7 +130,7 @@ CocoaSeeds
Add this line to your `Seedfile`:

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

Run `seed install`.
Expand Down
4 changes: 2 additions & 2 deletions Socket.IO-Client-Swift.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "Socket.IO-Client-Swift"
s.module_name = "SocketIOClientSwift"
s.version = "6.1.1"
s.version = "6.1.2"
s.summary = "Socket.IO-client for iOS and OS X"
s.description = <<-DESC
Socket.IO-client for iOS and OS X.
Expand All @@ -14,7 +14,7 @@ Pod::Spec.new do |s|
s.ios.deployment_target = '8.0'
s.osx.deployment_target = '10.10'
s.tvos.deployment_target = '9.0'
s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v6.1.1' }
s.source = { :git => "https://github.com/socketio/socket.io-client-swift.git", :tag => 'v6.1.2' }
s.source_files = "Source/**/*.swift"
s.requires_arc = true
# s.dependency 'Starscream', '~> 0.9' # currently this repo includes Starscream swift files
Expand Down
12 changes: 0 additions & 12 deletions Socket.IO-Client-Swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@
74171EC51C10CD240062D398 /* SocketTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E601C10CD240062D398 /* SocketTypes.swift */; };
74171EC71C10CD240062D398 /* SocketTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E601C10CD240062D398 /* SocketTypes.swift */; };
74171EC81C10CD240062D398 /* SocketTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E601C10CD240062D398 /* SocketTypes.swift */; };
74171EC91C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; };
74171ECA1C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; };
74171ECB1C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; };
74171ECD1C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; };
74171ECE1C10CD240062D398 /* SwiftRegex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E611C10CD240062D398 /* SwiftRegex.swift */; };
74171ECF1C10CD240062D398 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E621C10CD240062D398 /* WebSocket.swift */; };
74171ED01C10CD240062D398 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E621C10CD240062D398 /* WebSocket.swift */; };
74171ED11C10CD240062D398 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74171E621C10CD240062D398 /* WebSocket.swift */; };
Expand Down Expand Up @@ -190,7 +185,6 @@
74171E5E1C10CD240062D398 /* SocketParsable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketParsable.swift; path = Source/SocketParsable.swift; sourceTree = "<group>"; };
74171E5F1C10CD240062D398 /* SocketStringReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketStringReader.swift; path = Source/SocketStringReader.swift; sourceTree = "<group>"; };
74171E601C10CD240062D398 /* SocketTypes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketTypes.swift; path = Source/SocketTypes.swift; sourceTree = "<group>"; };
74171E611C10CD240062D398 /* SwiftRegex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SwiftRegex.swift; path = Source/SwiftRegex.swift; sourceTree = "<group>"; };
74171E621C10CD240062D398 /* WebSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WebSocket.swift; path = Source/WebSocket.swift; sourceTree = "<group>"; };
741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketEngineTest.swift; sourceTree = "<group>"; };
7420CB781C49629E00956AA4 /* SocketEnginePollable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketEnginePollable.swift; path = Source/SocketEnginePollable.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -375,7 +369,6 @@
74171E5E1C10CD240062D398 /* SocketParsable.swift */,
74171E5F1C10CD240062D398 /* SocketStringReader.swift */,
74171E601C10CD240062D398 /* SocketTypes.swift */,
74171E611C10CD240062D398 /* SwiftRegex.swift */,
74171E621C10CD240062D398 /* WebSocket.swift */,
);
name = Source;
Expand Down Expand Up @@ -638,7 +631,6 @@
74171EAB1C10CD240062D398 /* SocketLogger.swift in Sources */,
74171E991C10CD240062D398 /* SocketIOClient.swift in Sources */,
74171E8D1C10CD240062D398 /* SocketEventHandler.swift in Sources */,
74171EC91C10CD240062D398 /* SwiftRegex.swift in Sources */,
74171E7B1C10CD240062D398 /* SocketEngineClient.swift in Sources */,
74171ECF1C10CD240062D398 /* WebSocket.swift in Sources */,
74171EB11C10CD240062D398 /* SocketPacket.swift in Sources */,
Expand All @@ -663,7 +655,6 @@
74171E8E1C10CD240062D398 /* SocketEventHandler.swift in Sources */,
74171E7C1C10CD240062D398 /* SocketEngineClient.swift in Sources */,
74171E821C10CD240062D398 /* SocketEnginePacketType.swift in Sources */,
74171ECA1C10CD240062D398 /* SwiftRegex.swift in Sources */,
74171EB21C10CD240062D398 /* SocketPacket.swift in Sources */,
741F39EE1BD025D80026C9CC /* SocketEngineTest.swift in Sources */,
74171EBE1C10CD240062D398 /* SocketStringReader.swift in Sources */,
Expand Down Expand Up @@ -697,7 +688,6 @@
74171EAD1C10CD240062D398 /* SocketLogger.swift in Sources */,
74171E9B1C10CD240062D398 /* SocketIOClient.swift in Sources */,
74171E8F1C10CD240062D398 /* SocketEventHandler.swift in Sources */,
74171ECB1C10CD240062D398 /* SwiftRegex.swift in Sources */,
74171E7D1C10CD240062D398 /* SocketEngineClient.swift in Sources */,
74171ED11C10CD240062D398 /* WebSocket.swift in Sources */,
74171EB31C10CD240062D398 /* SocketPacket.swift in Sources */,
Expand Down Expand Up @@ -742,7 +732,6 @@
74171EAF1C10CD240062D398 /* SocketLogger.swift in Sources */,
74171E9D1C10CD240062D398 /* SocketIOClient.swift in Sources */,
74171E911C10CD240062D398 /* SocketEventHandler.swift in Sources */,
74171ECD1C10CD240062D398 /* SwiftRegex.swift in Sources */,
74171E7F1C10CD240062D398 /* SocketEngineClient.swift in Sources */,
74171ED31C10CD240062D398 /* WebSocket.swift in Sources */,
74171EB51C10CD240062D398 /* SocketPacket.swift in Sources */,
Expand All @@ -767,7 +756,6 @@
74171E921C10CD240062D398 /* SocketEventHandler.swift in Sources */,
74171E801C10CD240062D398 /* SocketEngineClient.swift in Sources */,
74171E861C10CD240062D398 /* SocketEnginePacketType.swift in Sources */,
74171ECE1C10CD240062D398 /* SwiftRegex.swift in Sources */,
74171EB61C10CD240062D398 /* SocketPacket.swift in Sources */,
57634A2A1BD9B46D00E19CD7 /* SocketEngineTest.swift in Sources */,
74171EC21C10CD240062D398 /* SocketStringReader.swift in Sources */,
Expand Down
16 changes: 16 additions & 0 deletions SocketIO-MacTests/SocketBasicPacketTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,20 @@ class SocketBasicPacketTest: XCTestCase {
XCTAssertEqual(packet.packetString, expectedSendString)
XCTAssertEqual(packet.binary, [data2, data])
}

func testBinaryStringPlaceholderInMessage() {
let engineString = "52-[\"test\",\"~~0\",{\"num\":0,\"_placeholder\":true},{\"num\":1,\"_placeholder\":true}]"
let socket = SocketIOClient(socketURL: NSURL())
socket.setTestable()

if case let .Right(packet) = socket.parseString(engineString) {
var packet = packet
XCTAssertEqual(packet.event, "test")
packet.addData(data)
packet.addData(data2)
XCTAssertEqual(packet.args[0] as? String, "~~0")
} else {
XCTFail()
}
}
}
9 changes: 5 additions & 4 deletions SocketIO-MacTests/SocketParserTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class SocketParserTest: XCTestCase {
"25[\"test\"]": ("/", ["test"], [], 5),
"2[\"test\",\"~~0\"]": ("/", ["test", "~~0"], [], -1),
"2/swift,[\"testArrayEmitReturn\",[\"test3\",\"test4\"]]": ("/swift", ["testArrayEmitReturn", ["test3", "test4"]], [], -1),
"51-/swift,[\"testMultipleItemsWithBufferEmitReturn\",[1,2],{\"test\":\"bob\"},25,\"polo\",{\"_placeholder\":true,\"num\":0}]": ("/swift", ["testMultipleItemsWithBufferEmitReturn", [1, 2], ["test": "bob"], 25, "polo", "~~0"], [], -1),
"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),
"3/swift,0[[\"test3\",\"test4\"]]": ("/swift", [["test3", "test4"]], [], 0),
"61-/swift,19[[1,2],{\"test\":\"bob\"},25,\"polo\",{\"_placeholder\":true,\"num\":0}]": ("/swift", [ [1, 2], ["test": "bob"], 25, "polo", "~~0"], [], 19),
"61-/swift,19[[1,2],{\"test\":\"bob\"},25,\"polo\",{\"_placeholder\":true,\"num\":0}]":
("/swift", [ [1, 2], ["test": "bob"], 25, "polo", ["_placeholder": true, "num": 0]], [], 19),
"4/swift,": ("/swift", [], [], -1),
"0/swift": ("/swift", [], [], -1),
"1/swift": ("/swift", [], [], -1),
Expand Down Expand Up @@ -123,8 +124,8 @@ class SocketParserTest: XCTestCase {
if case let .Right(packet) = packet {
XCTAssertEqual(packet.type, SocketPacket.PacketType(rawValue: Int(type) ?? -1)!)
XCTAssertEqual(packet.nsp, validValues.0)
XCTAssertTrue((packet.data as NSArray).isEqualToArray(validValues.1))
XCTAssertTrue((packet.binary as NSArray).isEqualToArray(validValues.2))
XCTAssertTrue((packet.data as NSArray).isEqualToArray(validValues.1), "\(packet.data)")
XCTAssertTrue((packet.binary as NSArray).isEqualToArray(validValues.2), "\(packet.binary)")
XCTAssertEqual(packet.id, validValues.3)
} else {
XCTFail()
Expand Down
6 changes: 2 additions & 4 deletions Source/SocketEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe

private weak var sessionDelegate: NSURLSessionDelegate?

private typealias Probe = (msg: String, type: SocketEnginePacketType, data: [NSData])
private typealias ProbeWaitQueue = [Probe]

private let logType = "SocketEngine"
private let url: NSURL

Expand All @@ -74,6 +71,7 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe
pongsMissedMax = Int(pingTimeout / (pingInterval ?? 25))
}
}

private var pongsMissed = 0
private var pongsMissedMax = 0
private var probeWait = ProbeWaitQueue()
Expand Down Expand Up @@ -387,7 +385,7 @@ public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWe
doPoll()
}

client?.engineDidOpen?("Connect")
client?.engineDidOpen("Connect")
}
} catch {
didError("Error parsing open packet")
Expand Down
2 changes: 1 addition & 1 deletion Source/SocketEngineClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Foundation
@objc public protocol SocketEngineClient {
func engineDidError(reason: String)
func engineDidClose(reason: String)
optional func engineDidOpen(reason: String)
func engineDidOpen(reason: String)
func parseEngineMessage(msg: String)
func parseEngineBinaryData(data: NSData)
}
14 changes: 7 additions & 7 deletions Source/SocketIOClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable
DefaultSocketLogger.Logger.log("Disconnected: %@", type: logType, args: reason)

status = .Disconnected
reconnects = false

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

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

reconnects = false
didDisconnect("Disconnect")
}

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

handleEvent("error", data: [reason], isInternalMessage: true)
}

public func engineDidOpen(reason: String) {
DefaultSocketLogger.Logger.log(reason, type: "SocketEngineClient")
}

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

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

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

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

handlers = handlers.filter { $0.event != event }
handlers = handlers.filter({ $0.event != event })
}

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

handlers = handlers.filter { $0.id != id }
handlers = handlers.filter({ $0.id != id })
}

/// Adds a handler for an event.
Expand Down
24 changes: 13 additions & 11 deletions Source/SocketPacket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct SocketPacket {
return createPacketString()
}

init(type: SocketPacket.PacketType, data: [AnyObject] = [AnyObject](), id: Int = -1,
init(type: PacketType, data: [AnyObject] = [AnyObject](), id: Int = -1,
nsp: String, placeholders: Int = 0, binary: [NSData] = [NSData]()) {
self.data = data
self.id = id
Expand Down Expand Up @@ -97,7 +97,7 @@ struct SocketPacket {
let jsonSend = try NSJSONSerialization.dataWithJSONObject(data,
options: NSJSONWritingOptions(rawValue: 0))
guard let jsonString = String(data: jsonSend, encoding: NSUTF8StringEncoding) else {
return "[]"
return message + "[]"
}

restOfMessage = jsonString
Expand Down Expand Up @@ -187,19 +187,21 @@ struct SocketPacket {
data = data.map(_fillInPlaceholders)
}

// Helper method that looks for placeholder strings
// Helper method that looks for placeholders
// If object is a collection it will recurse
// Returns the object if it is not a placeholder string or the corresponding
// Returns the object if it is not a placeholder or the corresponding
// binary data
private func _fillInPlaceholders(object: AnyObject) -> AnyObject {
switch object {
case let string as String where string["~~(\\d)"].groups() != nil:
return binary[Int(string["~~(\\d)"].groups()![1])!]
case let dict as NSDictionary:
return dict.reduce(NSMutableDictionary(), combine: {cur, keyValue in
cur[keyValue.0 as! NSCopying] = _fillInPlaceholders(keyValue.1)
return cur
})
if dict["_placeholder"] as? Bool ?? false {
return binary[dict["num"] as! Int]
} else {
return dict.reduce(NSMutableDictionary(), combine: {cur, keyValue in
cur[keyValue.0 as! NSCopying] = _fillInPlaceholders(keyValue.1)
return cur
})
}
case let arr as [AnyObject]:
return arr.map(_fillInPlaceholders)
default:
Expand Down Expand Up @@ -227,7 +229,7 @@ extension SocketPacket {
static func packetFromEmit(items: [AnyObject], id: Int, nsp: String, ack: Bool) -> SocketPacket {
let (parsedData, binary) = deconstructData(items)
let packet = SocketPacket(type: findType(binary.count, ack: ack), data: parsedData,
id: id, nsp: nsp, placeholders: -1, binary: binary)
id: id, nsp: nsp, binary: binary)

return packet
}
Expand Down
10 changes: 3 additions & 7 deletions Source/SocketParsable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ extension SocketParsable {
private func handleConnect(p: SocketPacket) {
if p.nsp == "/" && nsp != "/" {
joinNamespace(nsp)
} else if p.nsp != "/" && nsp == "/" {
didConnect()
} else {
didConnect()
}
Expand Down Expand Up @@ -91,8 +89,7 @@ extension SocketParsable {
}

if !parser.hasNext {
return .Right(SocketPacket(type: type, id: -1,
nsp: namespace ?? "/", placeholders: placeholders))
return .Right(SocketPacket(type: type, nsp: namespace, placeholders: placeholders))
}

var idString = ""
Expand All @@ -111,12 +108,11 @@ extension SocketParsable {
}

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

switch parseData(noPlaceholders) {
switch parseData(d) {
case let .Left(err):
// Errors aren't always enclosed in an array
if case let .Right(data) = parseData("\([noPlaceholders as AnyObject])") {
if case let .Right(data) = parseData("\([d as AnyObject])") {
return .Right(SocketPacket(type: type, data: data, id: Int(idString) ?? -1,
nsp: namespace, placeholders: placeholders))
} else {
Expand Down
3 changes: 3 additions & 0 deletions Source/SocketTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public typealias AckCallback = ([AnyObject]) -> Void
public typealias NormalCallback = ([AnyObject], SocketAckEmitter) -> Void
public typealias OnAckCallback = (timeoutAfter: UInt64, callback: AckCallback) -> Void

typealias Probe = (msg: String, type: SocketEnginePacketType, data: [NSData])
typealias ProbeWaitQueue = [Probe]

enum Either<E, V> {
case Left(E)
case Right(V)
Expand Down
Loading

0 comments on commit 31e4c60

Please sign in to comment.