Skip to content
This repository has been archived by the owner on Apr 4, 2020. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
mogol committed Jan 14, 2018
2 parents 0c9e5ae + 49168fc commit 45659c2
Show file tree
Hide file tree
Showing 20 changed files with 190 additions and 189 deletions.
10 changes: 4 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
language: objective-c
cache: cocoapods
podfile: Example/Podfile
osx_image: xcode8
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,name=iPhone 5' ONLY_ACTIVE_ARCH=NO | xcpretty
- pod lib lint
- 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 --allow-warnings
6 changes: 3 additions & 3 deletions CentrifugeiOS.podspec
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// CentrifugeClientMessageBuilderImpl.swift
// Pods
//
// Created by Herman Saprykin on 18/04/16.
// Created by German Saprykin on 18/04/16.
//
//

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// CentrifugeServerMessageParserImpl.swift
// Pods
//
// Created by Herman Saprykin on 19/04/16.
// Created by German Saprykin on 19/04/16.
//
//

Expand Down Expand Up @@ -41,20 +41,14 @@ class CentrifugeServerMessageParserImpl: CentrifugeServerMessageParser {

func messageParse(info: [String : AnyObject]) -> CentrifugeServerMessage? {
guard let uid = info["uid"] as? String? else {
print("Error: Invalid server response: Not valid message format")
print(info)
return nil
}

guard let methodName = info["method"] as? String else {
print("Error: Invalid server response: Not valid message format")
print(info)
return nil
}

guard let method = CentrifugeMethod(rawValue: methodName) else {
print("Error: Invalid server response: Not valid message format")
print(info)
return nil
}
var error: String?
Expand Down
3 changes: 1 addition & 2 deletions CentrifugeiOS/Classes/Centrifuge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Centrifugal.swift
// Pods
//
// Created by Herman Saprykin on 18/04/16.
// Created by German Saprykin on 18/04/16.
//
//

Expand All @@ -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
Expand Down
77 changes: 41 additions & 36 deletions CentrifugeiOS/Classes/CentrifugeClientImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
// 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!
var parser: CentrifugeServerMessageParser!

var delegate: CentrifugeClientDelegate!
weak var delegate: CentrifugeClientDelegate?

var messageCallbacks = [String : CentrifugeMessageHandler]()
var subscription = [String : CentrifugeChannelDelegate]()
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -143,7 +147,7 @@ class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient {
*/
func defaultProcessHandler(messages: [CentrifugeServerMessage]?, error: NSError?) {
if let err = error {
delegate.client(self, didReceiveError: err)
delegate?.client(self, didReceiveError: err)
return
}

Expand Down Expand Up @@ -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
Expand All @@ -211,46 +215,47 @@ class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient {
unsubscribeFrom(channel: channel)

// Client events
case .Disconnect:
delegate.client(self, didDisconnect: message)
ws.close()
case .disconnect:
delegate?.client(self, didDisconnect: message)
ws.disconnect()
resetState()
case .Refresh:
delegate.client(self, didReceiveRefresh: message)
case .refresh:
delegate?.client(self, didReceiveRefresh: message)
default:
print(message)
assertionFailure("Error: Invalid method type")
}
}

//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)
messages.forEach { message in
print(message)
}

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)
}
}
}
Expand Down
26 changes: 13 additions & 13 deletions CentrifugeiOS/Classes/Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Messages.swift
// Pods
//
// Created by Herman Saprykin on 18/04/16.
// Created by German Saprykin on 18/04/16.
//
//

Expand Down Expand Up @@ -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<T> {
Expand Down
4 changes: 2 additions & 2 deletions CentrifugeiOS/Classes/Protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// Protocols.swift
// Pods
//
// Created by Herman Saprykin on 26/04/16.
// Created by German Saprykin on 26/04/16.
//
//

public protocol CentrifugeClientDelegate {
public protocol CentrifugeClientDelegate: class {
func client(_ client: CentrifugeClient, didReceiveError error:NSError)
func client(_ client: CentrifugeClient, didReceiveRefresh: CentrifugeServerMessage)
func client(_ client: CentrifugeClient, didDisconnect: CentrifugeServerMessage)
Expand Down
Loading

0 comments on commit 45659c2

Please sign in to comment.