diff --git a/.travis.yml b/.travis.yml index 7fb4381..8f27aad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,11 @@ language: objective-c podfile: Example/Podfile -osx_image: xcode8.2 +osx_image: xcode9.2 rvm: - 2.2.2 before_install: - gem install cocoapods xcpretty --no-ri --no-rdoc -- travis_wait pod repo update --silent -- pod install --project-directory=Example +- pod install --project-directory=Example --repo-update script: - set -o pipefail && xcodebuild test -workspace Example/CentrifugeiOS.xcworkspace -scheme CentrifugeiOS-Example -sdk iphonesimulator -destination 'platform=iOS Simulator,OS=10.0,name=iPhone SE' ONLY_ACTIVE_ARCH=NO | xcpretty -- pod lib lint +- pod lib lint --allow-warnings diff --git a/CentrifugeiOS.podspec b/CentrifugeiOS.podspec index 57cbff2..e8db347 100644 --- a/CentrifugeiOS.podspec +++ b/CentrifugeiOS.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "CentrifugeiOS" - s.version = "3.1.0" + s.version = "4.0.0" s.summary = "Swifty iOS client for Centrifuge." s.description = <<-DESC iOS client for Centrifuge https://github.com/centrifugal/Centrifuge. It uses SwiftWebSocket and helpers classes to communicate with Centrifuge server. @@ -16,6 +16,6 @@ Pod::Spec.new do |s| s.source_files = 'CentrifugeiOS/Classes/**/*' - s.dependency 'SwiftWebSocket', '~>2.6.0' - s.dependency 'IDZSwiftCommonCrypto', '~>0.9.0' + s.dependency 'IDZSwiftCommonCrypto', '~>0.10.0' + s.dependency 'Starscream', '~>3.0.4' end diff --git a/CentrifugeiOS/Classes/Builder/CentrifugeClientMessageBuilderImpl.swift b/CentrifugeiOS/Classes/Builder/CentrifugeClientMessageBuilderImpl.swift index 56e0e92..52aa97f 100644 --- a/CentrifugeiOS/Classes/Builder/CentrifugeClientMessageBuilderImpl.swift +++ b/CentrifugeiOS/Classes/Builder/CentrifugeClientMessageBuilderImpl.swift @@ -2,7 +2,7 @@ // CentrifugeClientMessageBuilderImpl.swift // Pods // -// Created by Herman Saprykin on 18/04/16. +// Created by German Saprykin on 18/04/16. // // @@ -33,48 +33,48 @@ class CentrifugeClientMessageBuilderImpl: CentrifugeClientMessageBuilder { params["info"] = info } - return buildMessage(method: .Connect, params: params) + return buildMessage(method: .сonnect, params: params) } func buildDisconnectMessage() -> CentrifugeClientMessage { - return buildMessage(method: .Disconnect, params: [:]) + return buildMessage(method: .disconnect, params: [:]) } func buildSubscribeMessageTo(channel: String) -> CentrifugeClientMessage { let params = ["channel" : channel] - return buildMessage(method: .Subscribe, params: params) + return buildMessage(method: .subscribe, params: params) } func buildSubscribeMessageTo(channel: String, lastMessageUUID: String) -> CentrifugeClientMessage { let params: [String : Any] = ["channel" : channel, "recover" : true, "last" : lastMessageUUID] - return buildMessage(method: .Subscribe, params: params) + return buildMessage(method: .subscribe, params: params) } func buildUnsubscribeMessageFrom(channel: String) -> CentrifugeClientMessage { let params = ["channel" : channel] - return buildMessage(method: .Unsubscribe, params: params) + return buildMessage(method: .unsubscribe, params: params) } func buildPublishMessageTo(channel: String, data: [String : Any]) -> CentrifugeClientMessage { let params = ["channel" : channel, "data" : data] as [String : Any] - return buildMessage(method: .Publish, params: params) + return buildMessage(method: .publish, params: params) } func buildPresenceMessage(channel: String) -> CentrifugeClientMessage { let params = ["channel" : channel] - return buildMessage(method: .Presence, params: params) + return buildMessage(method: .presence, params: params) } func buildHistoryMessage(channel: String) -> CentrifugeClientMessage { let params = ["channel" : channel] - return buildMessage(method: .History, params: params) + return buildMessage(method: .history, params: params) } func buildPingMessage() -> CentrifugeClientMessage { - return buildMessage(method: .Ping, params: [:]) + return buildMessage(method: .ping, params: [:]) } private func buildMessage(method: CentrifugeMethod, params: [String: Any]) -> CentrifugeClientMessage { diff --git a/CentrifugeiOS/Classes/Builder/CentrifugoServerMessageParserImpl.swift b/CentrifugeiOS/Classes/Builder/CentrifugoServerMessageParserImpl.swift index 9c33add..0da5370 100644 --- a/CentrifugeiOS/Classes/Builder/CentrifugoServerMessageParserImpl.swift +++ b/CentrifugeiOS/Classes/Builder/CentrifugoServerMessageParserImpl.swift @@ -2,7 +2,7 @@ // CentrifugeServerMessageParserImpl.swift // Pods // -// Created by Herman Saprykin on 19/04/16. +// Created by German Saprykin on 19/04/16. // // diff --git a/CentrifugeiOS/Classes/Centrifuge.swift b/CentrifugeiOS/Classes/Centrifuge.swift index ed70e1a..fe479d7 100644 --- a/CentrifugeiOS/Classes/Centrifuge.swift +++ b/CentrifugeiOS/Classes/Centrifuge.swift @@ -2,7 +2,7 @@ // Centrifugal.swift // Pods // -// Created by Herman Saprykin on 18/04/16. +// Created by German Saprykin on 18/04/16. // // @@ -25,7 +25,6 @@ public class Centrifuge { client.parser = CentrifugeServerMessageParserImpl() client.creds = creds client.url = url - // TODO: Check references cycle client.delegate = delegate return client diff --git a/CentrifugeiOS/Classes/CentrifugeClientImpl.swift b/CentrifugeiOS/Classes/CentrifugeClientImpl.swift index b9f6ea3..10c96b9 100644 --- a/CentrifugeiOS/Classes/CentrifugeClientImpl.swift +++ b/CentrifugeiOS/Classes/CentrifugeClientImpl.swift @@ -2,16 +2,16 @@ // Clients.swift // Pods // -// Created by Herman Saprykin on 20/04/16. +// Created by German Saprykin on 20/04/16. // // -import SwiftWebSocket +import Starscream typealias CentrifugeBlockingHandler = ([CentrifugeServerMessage]?, NSError?) -> Void -class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient { - var ws: CentrifugeWebSocket! +class CentrifugeClientImpl: NSObject, CentrifugeClient, WebSocketDelegate { + var ws: WebSocket! var url: String! var creds: CentrifugeCredentials! var builder: CentrifugeClientMessageBuilder! @@ -32,13 +32,13 @@ class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient { func connect(withCompletion completion: @escaping CentrifugeMessageHandler) { blockingHandler = connectionProcessHandler connectionCompletion = completion - ws = CentrifugeWebSocket(url) + ws = WebSocket(url: URL(string: url)!) ws.delegate = self + ws.connect() } func disconnect() { - ws.delegate = nil - ws.close() + ws.disconnect() } func ping(withCompletion completion: @escaping CentrifugeMessageHandler) { @@ -54,7 +54,7 @@ class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient { messageCallbacks[message.uid] = completion send(message: message) } - + func subscribe(toChannel channel: String, delegate: CentrifugeChannelDelegate, lastMessageUID uid: String, completion: @escaping CentrifugeMessageHandler) { let message = builder.buildSubscribeMessageTo(channel: channel, lastMessageUUID: uid) subscription[channel] = delegate @@ -92,7 +92,11 @@ class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient { } func send(message: CentrifugeClientMessage) { - try! ws.send(centrifugeMessage: message) + let dict: [String:Any] = ["uid" : message.uid, + "method" : message.method.rawValue, + "params" : message.params] + let data = try! JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions()) + ws.write(data: data) } func setupConnectedState() { @@ -177,32 +181,32 @@ class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient { handled = true } - if (handled && (message.method != .Unsubscribe && message.method != .Disconnect)) { + if (handled && (message.method != .unsubscribe && message.method != .disconnect)) { return } switch message.method { // Channel events - case .Message: + case .message: guard let channel = message.body?["channel"] as? String, let delegate = subscription[channel] else { assertionFailure("Error: Invalid \(message.method) handler") return } delegate.client(self, didReceiveMessageInChannel: channel, message: message) - case .Join: + case .join: guard let channel = message.body?["channel"] as? String, let delegate = subscription[channel] else { assertionFailure("Error: Invalid \(message.method) handler") return } delegate.client(self, didReceiveJoinInChannel: channel, message: message) - case .Leave: + case .leave: guard let channel = message.body?["channel"] as? String, let delegate = subscription[channel] else { assertionFailure("Error: Invalid \(message.method) handler") return } delegate.client(self, didReceiveLeaveInChannel: channel, message: message) - case .Unsubscribe: + case .unsubscribe: guard let channel = message.body?["channel"] as? String, let delegate = subscription[channel] else { assertionFailure("Error: Invalid \(message.method) handler") return @@ -211,11 +215,11 @@ class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient { unsubscribeFrom(channel: channel) // Client events - case .Disconnect: + case .disconnect: delegate?.client(self, didDisconnect: message) - ws.close() + ws.disconnect() resetState() - case .Refresh: + case .refresh: delegate?.client(self, didReceiveRefresh: message) default: assertionFailure("Error: Invalid method type") @@ -223,31 +227,35 @@ class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient { } //MARK: - WebSocketDelegate - func webSocketOpen() { + + func websocketDidConnect(socket: WebSocketClient) { let message = builder.buildConnectMessage(credentials: creds) send(message: message) } - func webSocketMessageText(_ text: String) { + func websocketDidDisconnect(socket: WebSocketClient, error: Error?) { + guard let handler = blockingHandler else { return } + var err: NSError? + if let localizedDescription = error?.localizedDescription { + err = NSError(domain: CentrifugeWebSocketErrorDomain, code: 0, userInfo: [NSLocalizedDescriptionKey : localizedDescription]) + } + handler(nil, err) + } + + func websocketDidReceiveMessage(socket: WebSocketClient, text: String) { let data = text.data(using: String.Encoding.utf8)! let messages = try! parser.parse(data: data) - + if let handler = blockingHandler { handler(messages, nil) } } - func webSocketClose(_ code: Int, reason: String, wasClean: Bool) { - if let handler = blockingHandler { - let error = NSError(domain: CentrifugeWebSocketErrorDomain, code: code, userInfo: [NSLocalizedDescriptionKey : reason]) - handler(nil, error) - } + func websocketDidReceiveData(socket: WebSocketClient, data: Data) { + let messages = try! parser.parse(data: data) - } - - func webSocketError(_ error: NSError) { if let handler = blockingHandler { - handler(nil, error) + handler(messages, nil) } } } diff --git a/CentrifugeiOS/Classes/Models.swift b/CentrifugeiOS/Classes/Models.swift index 88239cc..d425cd9 100644 --- a/CentrifugeiOS/Classes/Models.swift +++ b/CentrifugeiOS/Classes/Models.swift @@ -2,7 +2,7 @@ // Messages.swift // Pods // -// Created by Herman Saprykin on 18/04/16. +// Created by German Saprykin on 18/04/16. // // @@ -51,18 +51,18 @@ public struct CentrifugeCredentials { } public enum CentrifugeMethod : String { - case Connect = "connect" - case Disconnect = "disconnect" - case Subscribe = "subscribe" - case Unsubscribe = "unsubscribe" - case Publish = "publish" - case Presence = "presence" - case History = "history" - case Join = "join" - case Leave = "leave" - case Message = "message" - case Refresh = "refresh" - case Ping = "ping" + case сonnect = "connect" + case disconnect = "disconnect" + case subscribe = "subscribe" + case unsubscribe = "unsubscribe" + case publish = "publish" + case presence = "presence" + case history = "history" + case join = "join" + case leave = "leave" + case message = "message" + case refresh = "refresh" + case ping = "ping" } class CentrifugeWrapper { diff --git a/CentrifugeiOS/Classes/Protocols.swift b/CentrifugeiOS/Classes/Protocols.swift index 0ec505c..29838c6 100644 --- a/CentrifugeiOS/Classes/Protocols.swift +++ b/CentrifugeiOS/Classes/Protocols.swift @@ -2,7 +2,7 @@ // Protocols.swift // Pods // -// Created by Herman Saprykin on 26/04/16. +// Created by German Saprykin on 26/04/16. // // diff --git a/CentrifugeiOS/Classes/WebSocket.swift b/CentrifugeiOS/Classes/WebSocket.swift deleted file mode 100644 index 88da4b4..0000000 --- a/CentrifugeiOS/Classes/WebSocket.swift +++ /dev/null @@ -1,21 +0,0 @@ -// -// WebSocket.swift -// Pods -// -// Created by Herman Saprykin on 18/04/16. -// -// - -import SwiftWebSocket - -class CentrifugeWebSocket: WebSocket { - - func send(centrifugeMessage message: CentrifugeClientMessage) throws { - let dict: [String:Any] = ["uid" : message.uid, - "method" : message.method.rawValue, - "params" : message.params] - let data = try JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions()) - - send(data: data) - } -} diff --git a/Example/CentrifugeiOS.xcodeproj/project.pbxproj b/Example/CentrifugeiOS.xcodeproj/project.pbxproj index e38c210..a19eb65 100644 --- a/Example/CentrifugeiOS.xcodeproj/project.pbxproj +++ b/Example/CentrifugeiOS.xcodeproj/project.pbxproj @@ -231,16 +231,16 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0730; - LastUpgradeCheck = 0810; + LastUpgradeCheck = 0900; ORGANIZATIONNAME = CocoaPods; TargetAttributes = { 607FACCF1AFB9204008FA782 = { CreatedOnToolsVersion = 6.3.1; - LastSwiftMigration = 0810; + LastSwiftMigration = 0900; }; 607FACE41AFB9204008FA782 = { CreatedOnToolsVersion = 6.3.1; - LastSwiftMigration = 0810; + LastSwiftMigration = 0900; TestTargetID = 607FACCF1AFB9204008FA782; }; }; @@ -291,9 +291,16 @@ files = ( ); inputPaths = ( + "${SRCROOT}/Pods/Target Support Files/Pods-CentrifugeiOS_Example/Pods-CentrifugeiOS_Example-frameworks.sh", + "${BUILT_PRODUCTS_DIR}/CentrifugeiOS/CentrifugeiOS.framework", + "${BUILT_PRODUCTS_DIR}/IDZSwiftCommonCrypto/IDZSwiftCommonCrypto.framework", + "${BUILT_PRODUCTS_DIR}/Starscream/Starscream.framework", ); name = "[CP] Embed Pods Frameworks"; outputPaths = ( + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/CentrifugeiOS.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/IDZSwiftCommonCrypto.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Starscream.framework", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; @@ -306,13 +313,16 @@ files = ( ); inputPaths = ( + "${PODS_PODFILE_DIR_PATH}/Podfile.lock", + "${PODS_ROOT}/Manifest.lock", ); name = "[CP] Check Pods Manifest.lock"; outputPaths = ( + "$(DERIVED_FILE_DIR)/Pods-CentrifugeiOS_Example-checkManifestLockResult.txt", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n"; + shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; showEnvVarsInLog = 0; }; C82D15DBF4CD35FC4A384A43 /* [CP] Copy Pods Resources */ = { @@ -351,13 +361,16 @@ files = ( ); inputPaths = ( + "${PODS_PODFILE_DIR_PATH}/Podfile.lock", + "${PODS_ROOT}/Manifest.lock", ); name = "[CP] Check Pods Manifest.lock"; outputPaths = ( + "$(DERIVED_FILE_DIR)/Pods-CentrifugeiOS_Tests-checkManifestLockResult.txt", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n"; + shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; showEnvVarsInLog = 0; }; F7C77600DD5EC9486A927B36 /* [CP] Embed Pods Frameworks */ = { @@ -366,9 +379,16 @@ files = ( ); inputPaths = ( + "${SRCROOT}/Pods/Target Support Files/Pods-CentrifugeiOS_Tests/Pods-CentrifugeiOS_Tests-frameworks.sh", + "${BUILT_PRODUCTS_DIR}/CentrifugeiOS/CentrifugeiOS.framework", + "${BUILT_PRODUCTS_DIR}/IDZSwiftCommonCrypto/IDZSwiftCommonCrypto.framework", + "${BUILT_PRODUCTS_DIR}/Starscream/Starscream.framework", ); name = "[CP] Embed Pods Frameworks"; outputPaths = ( + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/CentrifugeiOS.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/IDZSwiftCommonCrypto.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Starscream.framework", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; @@ -418,14 +438,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -466,14 +492,20 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -510,6 +542,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.CentrifugeiOS-Example"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -524,6 +557,7 @@ MODULE_NAME = ExampleApp; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.CentrifugeiOS-Example"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; }; name = Release; }; @@ -543,6 +577,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -558,6 +593,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 4.0; }; name = Release; }; diff --git a/Example/CentrifugeiOS.xcodeproj/xcshareddata/xcschemes/CentrifugeiOS-Example.xcscheme b/Example/CentrifugeiOS.xcodeproj/xcshareddata/xcschemes/CentrifugeiOS-Example.xcscheme index 8214367..3dbc0c1 100644 --- a/Example/CentrifugeiOS.xcodeproj/xcshareddata/xcschemes/CentrifugeiOS-Example.xcscheme +++ b/Example/CentrifugeiOS.xcodeproj/xcshareddata/xcschemes/CentrifugeiOS-Example.xcscheme @@ -1,6 +1,6 @@ 0 { + if let nick = self.nickTextField.text, nick.count > 0 { return nick }else { return "anonymous" @@ -51,7 +51,7 @@ class ViewController: UIViewController, CentrifugeChannelDelegate, CentrifugeCli func publish(_ text: String) { client.publish(toChannel: channel, data: ["nick" : nickName, "input" : text]) { message, error in - print("publish message: \(message)") + print("publish message: \(String(describing: message))") } } @@ -130,7 +130,7 @@ class ViewController: UIViewController, CentrifugeChannelDelegate, CentrifugeCli //MARK:- Interactions with user @IBAction func sendButtonDidPress(_ sender: AnyObject) { - if let text = messageTextField.text, text.characters.count > 0 { + if let text = messageTextField.text, text.count > 0 { messageTextField.text = "" publish(text) } diff --git a/Example/Podfile b/Example/Podfile index 67a2607..88e41e8 100644 --- a/Example/Podfile +++ b/Example/Podfile @@ -1,21 +1,10 @@ +platform :ios, '9.3' use_frameworks! target 'CentrifugeiOS_Example' do - pod 'CentrifugeiOS', :path => '../' + pod 'CentrifugeiOS', :path => '../' end target 'CentrifugeiOS_Tests' do - pod 'CentrifugeiOS', :path => '../' -end - -post_install do |installer| - installer.pods_project.targets.each do |target| - target.build_configurations.each do |configuration| - target.build_settings(configuration.name)['SWIFT_VERSION'] = '3.0' - - if target.name == 'CentrifugeiOS' - target.build_settings(configuration.name)['ENABLE_TESTABILITY'] = 'YES' - end - end - end + pod 'CentrifugeiOS', :path => '../' end diff --git a/Example/Tests/CentrifugoClientImplTests.swift b/Example/Tests/CentrifugoClientImplTests.swift index 14512bd..88303d4 100644 --- a/Example/Tests/CentrifugoClientImplTests.swift +++ b/Example/Tests/CentrifugoClientImplTests.swift @@ -2,12 +2,12 @@ // CentrifugeClientImplTests.swift // CentrifugeiOS // -// Created by Herman Saprykin on 25/04/16. +// Created by German Saprykin on 25/04/16. // Copyright © 2016 CocoaPods. All rights reserved. // import XCTest -import SwiftWebSocket +import Starscream @testable import CentrifugeiOS class CentrifugeClientImplTests: XCTestCase { @@ -18,7 +18,7 @@ class CentrifugeClientImplTests: XCTestCase { override func setUp() { super.setUp() parser = ParserMock() - + client = CentrifugeClientImpl() client.parser = parser } @@ -30,11 +30,11 @@ class CentrifugeClientImplTests: XCTestCase { //MARK: - Public interface func testConnectCallsWebSocketOpenAndSetupsHandler() { // given - let url = "asdasd" + let url = "https://example.com" client.url = url // when - client.connect { _ in } + client.connect { _,_ in } // then XCTAssertNotNil(client.ws.delegate) @@ -53,7 +53,7 @@ class CentrifugeClientImplTests: XCTestCase { let message = CentrifugeClientMessage.testMessage() - builder.buildPingHandler = { _ in return message } + builder.buildPingHandler = { return message } ws.sendHandler = { aMessage in validMessageDidSend = (aMessage == message) @@ -76,14 +76,13 @@ class CentrifugeClientImplTests: XCTestCase { ws.delegate = client ws.closeHandler = { closeDidCall = true - } + } as (() -> Void) // when client.disconnect() // then XCTAssertTrue(closeDidCall) - XCTAssertNil(ws.delegate) } func testSubscribeProcessValid() { @@ -245,7 +244,7 @@ class CentrifugeClientImplTests: XCTestCase { let message = CentrifugeClientMessage.testMessage() - builder.buildPublishHandler = { _ in return message } + builder.buildPublishHandler = { _,_ in return message } ws.sendHandler = { aMessage in validMessageDidSend = (aMessage == message) @@ -278,7 +277,7 @@ class CentrifugeClientImplTests: XCTestCase { // given let channel = "myChannel" - let message = CentrifugeServerMessage(uid: UUID().uuidString, method: .Publish, error: "decription", body: ["channel":channel]) + let message = CentrifugeServerMessage(uid: UUID().uuidString, method: .publish, error: "decription", body: ["channel":channel]) var receivedMessage: CentrifugeServerMessage? var receivedError: NSError? @@ -319,7 +318,7 @@ class CentrifugeClientImplTests: XCTestCase { let expectedChannel = "myChannel" var receivedChannel = "" - let message = CentrifugeServerMessage(uid: nil, method: .Message, error: nil, body: ["channel" : expectedChannel]) + let message = CentrifugeServerMessage(uid: nil, method: .message, error: nil, body: ["channel" : expectedChannel]) let delegate = ChannelDelegateMock() delegate.messageHandler = { _, channel, _ in @@ -337,7 +336,7 @@ class CentrifugeClientImplTests: XCTestCase { func testDefaultProcessHandlerProcessesDisconnect() { // given - let message = CentrifugeServerMessage(uid: nil, method: .Disconnect, error: nil, body: [ : ]) + let message = CentrifugeServerMessage(uid: nil, method: .disconnect, error: nil, body: [ : ]) var closeDidCall = false let delegate = ClientDelegateMock() client.delegate = delegate @@ -350,7 +349,7 @@ class CentrifugeClientImplTests: XCTestCase { ws.closeHandler = { closeDidCall = true - } + } as (() -> Void) // when client.defaultProcessHandler(messages: [message], error: nil) @@ -363,7 +362,7 @@ class CentrifugeClientImplTests: XCTestCase { func testDefaultProcessHandlerCallsRefreshClientDelegate() { // given - let message = CentrifugeServerMessage(uid: nil, method: .Refresh, error: nil, body: [ : ]) + let message = CentrifugeServerMessage(uid: nil, method: .refresh, error: nil, body: [ : ]) let delegate = ClientDelegateMock() client.delegate = delegate @@ -380,7 +379,7 @@ class CentrifugeClientImplTests: XCTestCase { let expectedChannel = "myChannel" var receivedChannel = "" - let message = CentrifugeServerMessage(uid: nil, method: .Join, error: nil, body: ["channel" : expectedChannel]) + let message = CentrifugeServerMessage(uid: nil, method: .join, error: nil, body: ["channel" : expectedChannel]) let delegate = ChannelDelegateMock() delegate.joinHandler = { _, channel, _ in @@ -401,7 +400,7 @@ class CentrifugeClientImplTests: XCTestCase { let expectedChannel = "myChannel" var receivedChannel = "" - let message = CentrifugeServerMessage(uid: nil, method: .Leave, error: nil, body: ["channel" : expectedChannel]) + let message = CentrifugeServerMessage(uid: nil, method: .leave, error: nil, body: ["channel" : expectedChannel]) let delegate = ChannelDelegateMock() delegate.leaveHandler = { _, channel, _ in @@ -424,7 +423,7 @@ class CentrifugeClientImplTests: XCTestCase { var handlerCalled = false let uid = UUID().uuidString - let message = CentrifugeServerMessage(uid: uid, method: .Unsubscribe, error: nil, body: ["channel" : expectedChannel]) + let message = CentrifugeServerMessage(uid: uid, method: .unsubscribe, error: nil, body: ["channel" : expectedChannel]) let delegate = ChannelDelegateMock() delegate.unsubscribeHandler = { _, channel, _ in @@ -450,7 +449,7 @@ class CentrifugeClientImplTests: XCTestCase { var handlerCalled = false let uid = UUID().uuidString - let message = CentrifugeServerMessage(uid: uid, method: .Disconnect, error: nil, body: [ : ]) + let message = CentrifugeServerMessage(uid: uid, method: .disconnect, error: nil, body: [ : ]) let delegate = ClientDelegateMock() @@ -473,9 +472,8 @@ class CentrifugeClientImplTests: XCTestCase { func testConnectionProcessHandlerResetsStateIfError() { // given let error = NSError(domain: "", code: 1, userInfo: nil) - - client.connectionCompletion = { _ in - } + + client.connectionCompletion = { _, _ in} client.blockingHandler = client.connectionProcessHandler // when @@ -569,7 +567,7 @@ class CentrifugeClientImplTests: XCTestCase { } // when - client.webSocketOpen() + client.websocketDidConnect(socket: ws) // then XCTAssertTrue(validMessageDidSend) @@ -583,7 +581,7 @@ class CentrifugeClientImplTests: XCTestCase { handlerCalled = true } // when - client.webSocketMessageText("") + client.websocketDidReceiveMessage(socket: WebSocketMock(), text: "") // then XCTAssertTrue(handlerCalled) @@ -603,7 +601,7 @@ class CentrifugeClientImplTests: XCTestCase { } // when - client.webSocketMessageText(text) + client.websocketDidReceiveMessage(socket: WebSocketMock(), text: text) // then XCTAssertTrue(handlerCalled) @@ -630,7 +628,7 @@ class CentrifugeClientImplTests: XCTestCase { } // when - client.webSocketMessageText(text) + client.websocketDidReceiveMessage(socket: WebSocketMock(), text: text) // then XCTAssertTrue(validMessagesDidReceive) @@ -639,64 +637,64 @@ class CentrifugeClientImplTests: XCTestCase { func testClientWebSocketCloseUsesHandler() { // given var handlerCalled = false - let code = 111 - let reason = "Hello, world" - - var receivedCode = -1 - var receivedReason = "" + var receivedError: Error? client.blockingHandler = { _, error in - if let err = error { - receivedCode = err.code - receivedReason = err.localizedDescription - } - handlerCalled = true + receivedError = error } // when - client.webSocketClose(code, reason: reason, wasClean: true) + client.websocketDidDisconnect(socket: WebSocketMock(), error: nil) // then XCTAssertTrue(handlerCalled) - XCTAssertEqual(receivedReason, reason) - XCTAssertEqual(receivedCode, code) + XCTAssertNil(receivedError) } func testClientWebSocketErrorUsesHandler() { // given var handlerCalled = false var receivedError: NSError? - let error = NSError(domain: "", code: 0, userInfo: nil) + let error = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey : "Hello"]) client.blockingHandler = { _, error in handlerCalled = true receivedError = error } // when - client.webSocketError(error) + client.websocketDidDisconnect(socket: WebSocketMock(), error: error) // then XCTAssertTrue(handlerCalled) - XCTAssertEqual(receivedError, error) + XCTAssertEqual(receivedError?.localizedDescription, "Hello") } //MARK: - Helpers - class WebSocketMock: CentrifugeWebSocket { - var openHandler: ((String) -> Void)? - var closeHandler: ((Void) -> Void)? + class WebSocketMock: WebSocket { + var openHandler: (() -> Void)? + var closeHandler: (() -> Void)? var sendHandler: ((CentrifugeClientMessage) -> Void)? - override func open(_ url: String) { - self.openHandler?(url) + init() { + let url = URL(string:"https://example.com")! + let request = URLRequest(url: url) + super.init(request: request) + } + + + override func connect() { + openHandler?() } - override func close(_ code : Int = 1000, reason : String = "Normal Closure"){ - self.closeHandler?() + override func disconnect(forceTimeout: TimeInterval? = nil, closeCode: UInt16 = CloseCode.normal.rawValue) { + closeHandler?() } - override func send(centrifugeMessage message: CentrifugeClientMessage) throws { - self.sendHandler?(message) + override func write(data: Data, completion: (() -> ())? = nil) { + let dict = try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any] + let message = CentrifugeClientMessage(uid: dict["uid"] as! String, method: CentrifugeMethod(rawValue: dict["method"] as! String)!, params: dict["params"] as! [String:Any]) + sendHandler?(message) } } @@ -706,12 +704,12 @@ class CentrifugeClientImplTests: XCTestCase { return buildConnectHandler(credentials) } - var buildPingHandler: ( (Void) -> CentrifugeClientMessage )! + var buildPingHandler: ( () -> CentrifugeClientMessage )! override func buildPingMessage() -> CentrifugeClientMessage { return buildPingHandler() } - var buildDisconnectHandler: ( (Void) -> CentrifugeClientMessage )! + var buildDisconnectHandler: ( () -> CentrifugeClientMessage )! override func buildDisconnectMessage() -> CentrifugeClientMessage { return buildDisconnectHandler() } @@ -808,16 +806,16 @@ class CentrifugeClientImplTests: XCTestCase { extension CentrifugeClientMessage { static func testMessage() -> CentrifugeClientMessage { - return CentrifugeClientMessage(uid: NSUUID().uuidString, method: .Connect, params: [:]) + return CentrifugeClientMessage(uid: NSUUID().uuidString, method: .сonnect, params: [:]) } } extension CentrifugeServerMessage { static func testMessage() -> CentrifugeServerMessage { - return CentrifugeServerMessage(uid: NSUUID().uuidString, method: .Connect, error: nil, body: [:]) + return CentrifugeServerMessage(uid: NSUUID().uuidString, method: .сonnect, error: nil, body: [:]) } static func errorMessage(_ decription: String) -> CentrifugeServerMessage { - return CentrifugeServerMessage(uid: NSUUID().uuidString, method: .Connect, error: decription, body: [:]) + return CentrifugeServerMessage(uid: NSUUID().uuidString, method: .сonnect, error: decription, body: [:]) } } diff --git a/Example/Tests/CentrifugoClientMessageBuilderImplTests.swift b/Example/Tests/CentrifugoClientMessageBuilderImplTests.swift index cf8d506..d61c673 100644 --- a/Example/Tests/CentrifugoClientMessageBuilderImplTests.swift +++ b/Example/Tests/CentrifugoClientMessageBuilderImplTests.swift @@ -25,7 +25,7 @@ class CentrifugeClientMessageBuilderImplTests: XCTestCase { let message = builder.buildConnectMessage(credentials: cred) // then - XCTAssertEqual(message.method, CentrifugeMethod.Connect) + XCTAssertEqual(message.method, CentrifugeMethod.сonnect) XCTAssertEqual(message.params["token"] as? String, "token") XCTAssertNotNil(message.params["user"]) XCTAssertNotNil(message.params["timestamp"]) @@ -40,7 +40,7 @@ class CentrifugeClientMessageBuilderImplTests: XCTestCase { let message = builder.buildConnectMessage(credentials: cred) // then - XCTAssertEqual(message.method, CentrifugeMethod.Connect) + XCTAssertEqual(message.method, CentrifugeMethod.сonnect) XCTAssertEqual(message.params["token"] as? String, "token") XCTAssertNotNil(message.params["user"]) XCTAssertNotNil(message.params["timestamp"]) diff --git a/Example/Tests/Helpers.swift b/Example/Tests/Helpers.swift index f7f2882..3aa38db 100644 --- a/Example/Tests/Helpers.swift +++ b/Example/Tests/Helpers.swift @@ -2,11 +2,11 @@ // Helpers.swift // CentrifugeiOS // -// Created by Herman Saprykin on 25/04/16. +// Created by German Saprykin on 25/04/16. // Copyright © 2016 CocoaPods. All rights reserved. // import Foundation -let DefaultTimeout = 0.125 \ No newline at end of file +let DefaultTimeout = 0.125 diff --git a/LICENSE b/LICENSE index 0c45385..bf7a777 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2016 Herman Saprykin +Copyright (c) 2016 German Saprykin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal