Skip to content

Development #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Socket.IO-Client-Swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
7420CB791C49629E00956AA4 /* SocketEnginePollable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7420CB781C49629E00956AA4 /* SocketEnginePollable.swift */; };
7420CB7A1C49629E00956AA4 /* SocketEnginePollable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7420CB781C49629E00956AA4 /* SocketEnginePollable.swift */; };
7420CB7B1C49629E00956AA4 /* SocketEnginePollable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7420CB781C49629E00956AA4 /* SocketEnginePollable.swift */; };
742D150C1CA5794B00BD987D /* SocketObjectiveCTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 742D150B1CA5794B00BD987D /* SocketObjectiveCTest.m */; };
74321DCB1C2D939A00CF6F43 /* SocketAckManagerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74321DC91C2D939A00CF6F43 /* SocketAckManagerTest.swift */; };
74321DCC1C2D939A00CF6F43 /* SocketParserTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74321DCA1C2D939A00CF6F43 /* SocketParserTest.swift */; };
7471CCEA1C39926300364B59 /* SocketClientSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74ABF7761C3991C10078C657 /* SocketClientSpec.swift */; };
Expand Down Expand Up @@ -187,6 +188,7 @@
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>"; };
742D150B1CA5794B00BD987D /* SocketObjectiveCTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SocketObjectiveCTest.m; sourceTree = "<group>"; };
74321DC91C2D939A00CF6F43 /* SocketAckManagerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocketAckManagerTest.swift; sourceTree = "<group>"; };
74321DCA1C2D939A00CF6F43 /* SocketParserTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SocketParserTest.swift; sourceTree = "<group>"; };
7472C65B1BCAB53E003CA70D /* SocketNamespacePacketTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketNamespacePacketTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -326,6 +328,7 @@
74F124EF1BC574CF002966F4 /* SocketBasicPacketTest.swift */,
741F39ED1BD025D80026C9CC /* SocketEngineTest.swift */,
7472C65B1BCAB53E003CA70D /* SocketNamespacePacketTest.swift */,
742D150B1CA5794B00BD987D /* SocketObjectiveCTest.m */,
74321DCA1C2D939A00CF6F43 /* SocketParserTest.swift */,
7472C65E1BCAC46E003CA70D /* SocketSideEffectTest.swift */,
572EF2471B51F18A00EEBB58 /* Supporting Files */,
Expand Down Expand Up @@ -513,7 +516,7 @@
572EF20E1B51F12F00EEBB58 /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0710;
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0720;
TargetAttributes = {
572EF2181B51F16C00EEBB58 = {
Expand Down Expand Up @@ -687,6 +690,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
742D150C1CA5794B00BD987D /* SocketObjectiveCTest.m in Sources */,
74321DCB1C2D939A00CF6F43 /* SocketAckManagerTest.swift in Sources */,
74321DCC1C2D939A00CF6F43 /* SocketParserTest.swift in Sources */,
7472C6601BCAC46E003CA70D /* SocketSideEffectTest.swift in Sources */,
Expand Down Expand Up @@ -1169,6 +1173,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "io.socket.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
};
name = Debug;
};
Expand Down
45 changes: 45 additions & 0 deletions SocketIO-MacTests/SocketObjectiveCTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// SocketObjectiveCTest.m
// Socket.IO-Client-Swift
//
// Created by Erik Little on 3/25/16.
//
// Merely tests whether the Objective-C api breaks
//

#import <XCTest/XCTest.h>
@import SocketIOClientSwift;

@interface SocketObjectiveCTest : XCTestCase

@property SocketIOClient* socket;

@end

@implementation SocketObjectiveCTest

- (void)setUp {
[super setUp];
NSURL* url = [[NSURL alloc] initWithString:@"http://localhost"];
self.socket = [[SocketIOClient alloc] initWithSocketURL:url options:nil];
}

- (void)testOnSyntax {
[self.socket on:@"someCallback" callback:^(NSArray* data, SocketAckEmitter* ack) {
[ack with:@[@1]];
}];
}

- (void)testEmitSyntax {
[self.socket emit:@"testEmit" withItems:@[@YES]];
}

- (void)testEmitWithAckSyntax {
[self.socket emitWithAck:@"testAckEmit" withItems:@[@YES]];
}

- (void)testOffSyntax {
[self.socket off:@"test"];
}

@end
6 changes: 6 additions & 0 deletions SocketIO-MacTests/SocketParserTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class SocketParserTest: XCTestCase {
"0/swift": ("/swift", [], [], -1),
"1/swift": ("/swift", [], [], -1),
"4\"ERROR\"": ("/", ["ERROR"], [], -1),
"4{\"test\":2}": ("/", [["test": 2]], [], -1),
"41": ("/", [1], [], -1)]

func testDisconnect() {
Expand Down Expand Up @@ -87,6 +88,11 @@ class SocketParserTest: XCTestCase {
validateParseResult(message)
}

func testErrorTypeDictionary() {
let message = "4{\"test\":2}"
validateParseResult(message)
}

func testErrorTypeInt() {
let message = "41"
validateParseResult(message)
Expand Down
2 changes: 1 addition & 1 deletion Source/SocketAckEmitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import Foundation

public final class SocketAckEmitter: NSObject {
public final class SocketAckEmitter : NSObject {
let socket: SocketIOClient
let ackNum: Int

Expand Down
2 changes: 1 addition & 1 deletion Source/SocketAckManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import Foundation

private struct SocketAck: Hashable, Equatable {
private struct SocketAck : Hashable, Equatable {
let ack: Int
var callback: AckCallback!
var hashValue: Int {
Expand Down
2 changes: 1 addition & 1 deletion Source/SocketAnyEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import Foundation

public final class SocketAnyEvent: NSObject {
public final class SocketAnyEvent : NSObject {
public let event: String
public let items: NSArray?
override public var description: String {
Expand Down
2 changes: 1 addition & 1 deletion Source/SocketClientSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

protocol SocketClientSpec: class {
protocol SocketClientSpec : class {
var nsp: String { get set }
var waitingPackets: [SocketPacket] { get set }

Expand Down
60 changes: 17 additions & 43 deletions Source/SocketEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import Foundation

public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWebsocket {
public final class SocketEngine : NSObject, SocketEnginePollable, SocketEngineWebsocket {
public let emitQueue = dispatch_queue_create("com.socketio.engineEmitQueue", DISPATCH_QUEUE_SERIAL)
public let handleQueue = dispatch_queue_create("com.socketio.engineHandleQueue", DISPATCH_QUEUE_SERIAL)
public let parseQueue = dispatch_queue_create("com.socketio.engineParseQueue", DISPATCH_QUEUE_SERIAL)
Expand All @@ -47,7 +47,6 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
public private(set) var forcePolling = false
public private(set) var forceWebsockets = false
public private(set) var invalidated = false
public private(set) var pingTimer: NSTimer?
public private(set) var polling = true
public private(set) var probing = false
public private(set) var session: NSURLSession?
Expand Down Expand Up @@ -123,18 +122,6 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
self.init(client: client, url: url, options: options?.toSocketOptionsSet() ?? [])
}

@available(*, deprecated=5.3, message="Please use the NSURL based init")
public convenience init(client: SocketEngineClient, urlString: String, options: Set<SocketIOClientOption>) {
guard let url = NSURL(string: urlString) else { fatalError("Incorrect url") }
self.init(client: client, url: url, options: options)
}

@available(*, deprecated=5.3, message="Please use the NSURL based init")
public convenience init(client: SocketEngineClient, urlString: String, options: NSDictionary?) {
guard let url = NSURL(string: urlString) else { fatalError("Incorrect url") }
self.init(client: client, url: url, options: options?.toSocketOptionsSet() ?? [])
}

deinit {
DefaultSocketLogger.Logger.log("Engine is being released", type: logType)
closed = true
Expand Down Expand Up @@ -184,16 +171,12 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
return false
}
}

public func close(reason: String) {
disconnect(reason)
}

/// Starts the connection to the server
public func connect() {
if connected {
DefaultSocketLogger.Logger.error("Engine tried opening while connected. Assuming this was a reconnect", type: logType)
close("reconnect")
disconnect("reconnect")
}

DefaultSocketLogger.Logger.log("Starting engine", type: logType)
Expand Down Expand Up @@ -285,7 +268,7 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
public func didError(error: String) {
DefaultSocketLogger.Logger.error(error, type: logType)
client?.engineDidError(error)
close(error)
disconnect(error)
}

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

pingTimer?.invalidate()
ws?.disconnect()
stopPolling()
client?.engineDidClose(reason)
Expand Down Expand Up @@ -401,8 +383,7 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
createWebsocketAndConnect()
}


startPingTimer()
sendPing()

if !forceWebsockets {
doPoll()
Expand All @@ -424,11 +405,7 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
upgradeTransport()
}
}

public func open() {
connect()
}


public func parseEngineData(data: NSData) {
DefaultSocketLogger.Logger.log("Got binary data: %@", type: "SocketEngine", args: data)
client?.parseEngineBinaryData(data.subdataWithRange(NSMakeRange(1, data.length - 1)))
Expand Down Expand Up @@ -487,30 +464,28 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
websocket = false
}

@objc private func sendPing() {
private func sendPing() {
if !connected {
return
}

//Server is not responding
if pongsMissed > pongsMissedMax {
pingTimer?.invalidate()
client?.engineDidClose("Ping timeout")
return
}

pongsMissed += 1
write("", withType: .Ping, withData: [])
}

private func startPingTimer() {

if let pingInterval = pingInterval {
pingTimer?.invalidate()
pingTimer = nil
pongsMissed += 1
write("", withType: .Ping, withData: [])

dispatch_async(dispatch_get_main_queue()) {
self.pingTimer = NSTimer.scheduledTimerWithTimeInterval(pingInterval, target: self,
selector: #selector(SocketEngine.sendPing), userInfo: nil, repeats: true)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(pingInterval * Double(NSEC_PER_SEC)))
dispatch_after(time, dispatch_get_main_queue()) {[weak self] in
self?.sendPing()
}
}
}

// Moves from long-polling to websockets
private func upgradeTransport() {
if ws?.isConnected ?? false {
Expand Down Expand Up @@ -562,7 +537,6 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
}

if websocket {
pingTimer?.invalidate()
connected = false
websocket = false

Expand Down
2 changes: 1 addition & 1 deletion Source/SocketEnginePacketType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@

import Foundation

@objc public enum SocketEnginePacketType: Int {
@objc public enum SocketEnginePacketType : Int {
case Open, Close, Ping, Pong, Message, Upgrade, Noop
}
2 changes: 1 addition & 1 deletion Source/SocketEnginePollable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import Foundation

/// Protocol that is used to implement socket.io polling support
public protocol SocketEnginePollable: SocketEngineSpec {
public protocol SocketEnginePollable : SocketEngineSpec {
var invalidated: Bool { get }
/// Holds strings waiting to be sent over polling.
/// You shouldn't need to mess with this.
Expand Down
3 changes: 0 additions & 3 deletions Source/SocketEngineSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import Foundation
var forcePolling: Bool { get }
var forceWebsockets: Bool { get }
var parseQueue: dispatch_queue_t! { get }
var pingTimer: NSTimer? { get }
var polling: Bool { get }
var probing: Bool { get }
var emitQueue: dispatch_queue_t! { get }
Expand All @@ -50,13 +49,11 @@ import Foundation

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

@available(*, deprecated=5.5, message="Please use disconnect") func close(reason: String)
func connect()
func didError(error: String)
func disconnect(reason: String)
func doFastUpgrade()
func flushWaitingForPostToWebSocket()
@available(*, deprecated=5.5, message="Please use connect") func open()
func parseEngineData(data: NSData)
func parseEngineMessage(message: String, fromPolling: Bool)
func write(msg: String, withType type: SocketEnginePacketType, withData data: [NSData])
Expand Down
2 changes: 1 addition & 1 deletion Source/SocketEngineWebsocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import Foundation

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

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