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

Swift4 #14

Merged
merged 4 commits into from
Jan 14, 2018
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: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
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
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
66 changes: 37 additions & 29 deletions CentrifugeiOS/Classes/CentrifugeClientImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!
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 @@ -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,43 +215,47 @@ 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")
}
}

//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)
}
}
}
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
2 changes: 1 addition & 1 deletion CentrifugeiOS/Classes/Protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Protocols.swift
// Pods
//
// Created by Herman Saprykin on 26/04/16.
// Created by German Saprykin on 26/04/16.
//
//

Expand Down
21 changes: 0 additions & 21 deletions CentrifugeiOS/Classes/WebSocket.swift

This file was deleted.

Loading