Skip to content

Commit c9f653e

Browse files
committed
Merge pull request socketio#344 from socketio/development
Development
2 parents 6c779e8 + 3073130 commit c9f653e

19 files changed

+164
-149
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
7420CB791C49629E00956AA4 /* SocketEnginePollable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7420CB781C49629E00956AA4 /* SocketEnginePollable.swift */; };
117117
7420CB7A1C49629E00956AA4 /* SocketEnginePollable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7420CB781C49629E00956AA4 /* SocketEnginePollable.swift */; };
118118
7420CB7B1C49629E00956AA4 /* SocketEnginePollable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7420CB781C49629E00956AA4 /* SocketEnginePollable.swift */; };
119+
742D150C1CA5794B00BD987D /* SocketObjectiveCTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 742D150B1CA5794B00BD987D /* SocketObjectiveCTest.m */; };
119120
74321DCB1C2D939A00CF6F43 /* SocketAckManagerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74321DC91C2D939A00CF6F43 /* SocketAckManagerTest.swift */; };
120121
74321DCC1C2D939A00CF6F43 /* SocketParserTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74321DCA1C2D939A00CF6F43 /* SocketParserTest.swift */; };
121122
7471CCEA1C39926300364B59 /* SocketClientSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74ABF7761C3991C10078C657 /* SocketClientSpec.swift */; };
@@ -187,6 +188,7 @@
187188
74171E621C10CD240062D398 /* WebSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WebSocket.swift; path = Source/WebSocket.swift; sourceTree = "<group>"; };
188189
741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketEngineTest.swift; sourceTree = "<group>"; };
189190
7420CB781C49629E00956AA4 /* SocketEnginePollable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketEnginePollable.swift; path = Source/SocketEnginePollable.swift; sourceTree = "<group>"; };
191+
742D150B1CA5794B00BD987D /* SocketObjectiveCTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SocketObjectiveCTest.m; sourceTree = "<group>"; };
190192
74321DC91C2D939A00CF6F43 /* SocketAckManagerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocketAckManagerTest.swift; sourceTree = "<group>"; };
191193
74321DCA1C2D939A00CF6F43 /* SocketParserTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocketParserTest.swift; sourceTree = "<group>"; };
192194
7472C65B1BCAB53E003CA70D /* SocketNamespacePacketTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketNamespacePacketTest.swift; sourceTree = "<group>"; };
@@ -326,6 +328,7 @@
326328
74F124EF1BC574CF002966F4 /* SocketBasicPacketTest.swift */,
327329
741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */,
328330
7472C65B1BCAB53E003CA70D /* SocketNamespacePacketTest.swift */,
331+
742D150B1CA5794B00BD987D /* SocketObjectiveCTest.m */,
329332
74321DCA1C2D939A00CF6F43 /* SocketParserTest.swift */,
330333
7472C65E1BCAC46E003CA70D /* SocketSideEffectTest.swift */,
331334
572EF2471B51F18A00EEBB58 /* Supporting Files */,
@@ -513,7 +516,7 @@
513516
572EF20E1B51F12F00EEBB58 /* Project object */ = {
514517
isa = PBXProject;
515518
attributes = {
516-
LastSwiftUpdateCheck = 0710;
519+
LastSwiftUpdateCheck = 0730;
517520
LastUpgradeCheck = 0720;
518521
TargetAttributes = {
519522
572EF2181B51F16C00EEBB58 = {
@@ -687,6 +690,7 @@
687690
isa = PBXSourcesBuildPhase;
688691
buildActionMask = 2147483647;
689692
files = (
693+
742D150C1CA5794B00BD987D /* SocketObjectiveCTest.m in Sources */,
690694
74321DCB1C2D939A00CF6F43 /* SocketAckManagerTest.swift in Sources */,
691695
74321DCC1C2D939A00CF6F43 /* SocketParserTest.swift in Sources */,
692696
7472C6601BCAC46E003CA70D /* SocketSideEffectTest.swift in Sources */,
@@ -1169,6 +1173,7 @@
11691173
PRODUCT_BUNDLE_IDENTIFIER = "io.socket.$(PRODUCT_NAME:rfc1034identifier)";
11701174
PRODUCT_NAME = "$(TARGET_NAME)";
11711175
SDKROOT = macosx;
1176+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
11721177
};
11731178
name = Debug;
11741179
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// SocketObjectiveCTest.m
3+
// Socket.IO-Client-Swift
4+
//
5+
// Created by Erik Little on 3/25/16.
6+
//
7+
// Merely tests whether the Objective-C api breaks
8+
//
9+
10+
#import <XCTest/XCTest.h>
11+
@import SocketIOClientSwift;
12+
13+
@interface SocketObjectiveCTest : XCTestCase
14+
15+
@property SocketIOClient* socket;
16+
17+
@end
18+
19+
@implementation SocketObjectiveCTest
20+
21+
- (void)setUp {
22+
[super setUp];
23+
NSURL* url = [[NSURL alloc] initWithString:@"http://localhost"];
24+
self.socket = [[SocketIOClient alloc] initWithSocketURL:url options:nil];
25+
}
26+
27+
- (void)testOnSyntax {
28+
[self.socket on:@"someCallback" callback:^(NSArray* data, SocketAckEmitter* ack) {
29+
[ack with:@[@1]];
30+
}];
31+
}
32+
33+
- (void)testEmitSyntax {
34+
[self.socket emit:@"testEmit" withItems:@[@YES]];
35+
}
36+
37+
- (void)testEmitWithAckSyntax {
38+
[self.socket emitWithAck:@"testAckEmit" withItems:@[@YES]];
39+
}
40+
41+
- (void)testOffSyntax {
42+
[self.socket off:@"test"];
43+
}
44+
45+
@end

SocketIO-MacTests/SocketParserTest.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class SocketParserTest: XCTestCase {
2525
"0/swift": ("/swift", [], [], -1),
2626
"1/swift": ("/swift", [], [], -1),
2727
"4\"ERROR\"": ("/", ["ERROR"], [], -1),
28+
"4{\"test\":2}": ("/", [["test": 2]], [], -1),
2829
"41": ("/", [1], [], -1)]
2930

3031
func testDisconnect() {
@@ -87,6 +88,11 @@ class SocketParserTest: XCTestCase {
8788
validateParseResult(message)
8889
}
8990

91+
func testErrorTypeDictionary() {
92+
let message = "4{\"test\":2}"
93+
validateParseResult(message)
94+
}
95+
9096
func testErrorTypeInt() {
9197
let message = "41"
9298
validateParseResult(message)

Source/SocketAckEmitter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import Foundation
2626

27-
public final class SocketAckEmitter: NSObject {
27+
public final class SocketAckEmitter : NSObject {
2828
let socket: SocketIOClient
2929
let ackNum: Int
3030

Source/SocketAckManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import Foundation
2626

27-
private struct SocketAck: Hashable, Equatable {
27+
private struct SocketAck : Hashable, Equatable {
2828
let ack: Int
2929
var callback: AckCallback!
3030
var hashValue: Int {

Source/SocketAnyEvent.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import Foundation
2626

27-
public final class SocketAnyEvent: NSObject {
27+
public final class SocketAnyEvent : NSObject {
2828
public let event: String
2929
public let items: NSArray?
3030
override public var description: String {

Source/SocketClientSpec.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
// THE SOFTWARE.
2424

25-
protocol SocketClientSpec: class {
25+
protocol SocketClientSpec : class {
2626
var nsp: String { get set }
2727
var waitingPackets: [SocketPacket] { get set }
2828

Source/SocketEngine.swift

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import Foundation
2626

27-
public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWebsocket {
27+
public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWebsocket {
2828
public let emitQueue = dispatch_queue_create("com.socketio.engineEmitQueue", DISPATCH_QUEUE_SERIAL)
2929
public let handleQueue = dispatch_queue_create("com.socketio.engineHandleQueue", DISPATCH_QUEUE_SERIAL)
3030
public let parseQueue = dispatch_queue_create("com.socketio.engineParseQueue", DISPATCH_QUEUE_SERIAL)
@@ -47,7 +47,6 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
4747
public private(set) var forcePolling = false
4848
public private(set) var forceWebsockets = false
4949
public private(set) var invalidated = false
50-
public private(set) var pingTimer: NSTimer?
5150
public private(set) var polling = true
5251
public private(set) var probing = false
5352
public private(set) var session: NSURLSession?
@@ -123,18 +122,6 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
123122
self.init(client: client, url: url, options: options?.toSocketOptionsSet() ?? [])
124123
}
125124

126-
@available(*, deprecated=5.3, message="Please use the NSURL based init")
127-
public convenience init(client: SocketEngineClient, urlString: String, options: Set<SocketIOClientOption>) {
128-
guard let url = NSURL(string: urlString) else { fatalError("Incorrect url") }
129-
self.init(client: client, url: url, options: options)
130-
}
131-
132-
@available(*, deprecated=5.3, message="Please use the NSURL based init")
133-
public convenience init(client: SocketEngineClient, urlString: String, options: NSDictionary?) {
134-
guard let url = NSURL(string: urlString) else { fatalError("Incorrect url") }
135-
self.init(client: client, url: url, options: options?.toSocketOptionsSet() ?? [])
136-
}
137-
138125
deinit {
139126
DefaultSocketLogger.Logger.log("Engine is being released", type: logType)
140127
closed = true
@@ -184,16 +171,12 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
184171
return false
185172
}
186173
}
187-
188-
public func close(reason: String) {
189-
disconnect(reason)
190-
}
191174

192175
/// Starts the connection to the server
193176
public func connect() {
194177
if connected {
195178
DefaultSocketLogger.Logger.error("Engine tried opening while connected. Assuming this was a reconnect", type: logType)
196-
close("reconnect")
179+
disconnect("reconnect")
197180
}
198181

199182
DefaultSocketLogger.Logger.log("Starting engine", type: logType)
@@ -285,7 +268,7 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
285268
public func didError(error: String) {
286269
DefaultSocketLogger.Logger.error(error, type: logType)
287270
client?.engineDidError(error)
288-
close(error)
271+
disconnect(error)
289272
}
290273

291274
public func disconnect(reason: String) {
@@ -295,7 +278,6 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
295278
invalidated = true
296279
connected = false
297280

298-
pingTimer?.invalidate()
299281
ws?.disconnect()
300282
stopPolling()
301283
client?.engineDidClose(reason)
@@ -401,8 +383,7 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
401383
createWebsocketAndConnect()
402384
}
403385

404-
405-
startPingTimer()
386+
sendPing()
406387

407388
if !forceWebsockets {
408389
doPoll()
@@ -424,11 +405,7 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
424405
upgradeTransport()
425406
}
426407
}
427-
428-
public func open() {
429-
connect()
430-
}
431-
408+
432409
public func parseEngineData(data: NSData) {
433410
DefaultSocketLogger.Logger.log("Got binary data: %@", type: "SocketEngine", args: data)
434411
client?.parseEngineBinaryData(data.subdataWithRange(NSMakeRange(1, data.length - 1)))
@@ -487,30 +464,28 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
487464
websocket = false
488465
}
489466

490-
@objc private func sendPing() {
467+
private func sendPing() {
468+
if !connected {
469+
return
470+
}
471+
491472
//Server is not responding
492473
if pongsMissed > pongsMissedMax {
493-
pingTimer?.invalidate()
494474
client?.engineDidClose("Ping timeout")
495475
return
496476
}
497-
498-
pongsMissed += 1
499-
write("", withType: .Ping, withData: [])
500-
}
501-
502-
private func startPingTimer() {
477+
503478
if let pingInterval = pingInterval {
504-
pingTimer?.invalidate()
505-
pingTimer = nil
479+
pongsMissed += 1
480+
write("", withType: .Ping, withData: [])
506481

507-
dispatch_async(dispatch_get_main_queue()) {
508-
self.pingTimer = NSTimer.scheduledTimerWithTimeInterval(pingInterval, target: self,
509-
selector: #selector(SocketEngine.sendPing), userInfo: nil, repeats: true)
482+
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(pingInterval * Double(NSEC_PER_SEC)))
483+
dispatch_after(time, dispatch_get_main_queue()) {[weak self] in
484+
self?.sendPing()
510485
}
511486
}
512487
}
513-
488+
514489
// Moves from long-polling to websockets
515490
private func upgradeTransport() {
516491
if ws?.isConnected ?? false {
@@ -562,7 +537,6 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
562537
}
563538

564539
if websocket {
565-
pingTimer?.invalidate()
566540
connected = false
567541
websocket = false
568542

Source/SocketEnginePacketType.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525

2626
import Foundation
2727

28-
@objc public enum SocketEnginePacketType: Int {
28+
@objc public enum SocketEnginePacketType : Int {
2929
case Open, Close, Ping, Pong, Message, Upgrade, Noop
3030
}

Source/SocketEnginePollable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import Foundation
2626

2727
/// Protocol that is used to implement socket.io polling support
28-
public protocol SocketEnginePollable: SocketEngineSpec {
28+
public protocol SocketEnginePollable : SocketEngineSpec {
2929
var invalidated: Bool { get }
3030
/// Holds strings waiting to be sent over polling.
3131
/// You shouldn't need to mess with this.

Source/SocketEngineSpec.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import Foundation
3737
var forcePolling: Bool { get }
3838
var forceWebsockets: Bool { get }
3939
var parseQueue: dispatch_queue_t! { get }
40-
var pingTimer: NSTimer? { get }
4140
var polling: Bool { get }
4241
var probing: Bool { get }
4342
var emitQueue: dispatch_queue_t! { get }
@@ -50,13 +49,11 @@ import Foundation
5049

5150
init(client: SocketEngineClient, url: NSURL, options: NSDictionary?)
5251

53-
@available(*, deprecated=5.5, message="Please use disconnect") func close(reason: String)
5452
func connect()
5553
func didError(error: String)
5654
func disconnect(reason: String)
5755
func doFastUpgrade()
5856
func flushWaitingForPostToWebSocket()
59-
@available(*, deprecated=5.5, message="Please use connect") func open()
6057
func parseEngineData(data: NSData)
6158
func parseEngineMessage(message: String, fromPolling: Bool)
6259
func write(msg: String, withType type: SocketEnginePacketType, withData data: [NSData])

Source/SocketEngineWebsocket.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import Foundation
2727

2828
/// Protocol that is used to implement socket.io WebSocket support
29-
public protocol SocketEngineWebsocket: SocketEngineSpec, WebSocketDelegate {
29+
public protocol SocketEngineWebsocket : SocketEngineSpec, WebSocketDelegate {
3030
var ws: WebSocket? { get }
3131

3232
func sendWebSocketMessage(str: String, withType type: SocketEnginePacketType, withData datas: [NSData])

0 commit comments

Comments
 (0)