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

Commit

Permalink
Merge branch 'release/3.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
mogol committed Nov 16, 2016
2 parents 8ff91fe + cc6af46 commit f9ee74e
Show file tree
Hide file tree
Showing 17 changed files with 229 additions and 258 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3
3.0
9 changes: 3 additions & 6 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 = "2.0.0"
s.version = "3.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,9 +16,6 @@ Pod::Spec.new do |s|

s.source_files = 'CentrifugeiOS/Classes/**/*'

s.dependency 'SwiftWebSocket', '2.6.4'
s.dependency 'IDZSwiftCommonCrypto', '0.8.3'
s.xcconfig = {
'SWIFT_VERSION' => '2.3'
}
s.dependency 'SwiftWebSocket', '~>2.6.0'
s.dependency 'IDZSwiftCommonCrypto', '~>0.9.0'
end
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protocol CentrifugeClientMessageBuilder {
func buildPresenceMessage(channel: String) -> CentrifugeClientMessage
func buildHistoryMessage(channel: String) -> CentrifugeClientMessage
func buildPingMessage() -> CentrifugeClientMessage
func buildPublishMessageTo(channel: String, data: [String: AnyObject]) -> CentrifugeClientMessage
func buildPublishMessageTo(channel: String, data: [String: Any]) -> CentrifugeClientMessage
}

class CentrifugeClientMessageBuilderImpl: CentrifugeClientMessageBuilder {
Expand All @@ -28,87 +28,69 @@ class CentrifugeClientMessageBuilderImpl: CentrifugeClientMessageBuilder {

var params = ["user" : user,
"timestamp" : timestamp,
"token" : createToken("\(user)\(timestamp)", key: secret)]
"token" : createToken(string: "\(user)\(timestamp)", key: secret)]

if let info = credentials.info {
params["info"] = info
}

return buildMessage(.Connect, params: params)
return buildMessage(method: .Connect, params: params)
}

func buildDisconnectMessage() -> CentrifugeClientMessage {
return buildMessage(.Disconnect, params: [:])
return buildMessage(method: .Disconnect, params: [:])
}

func buildSubscribeMessageTo(channel: String) -> CentrifugeClientMessage {
let params = ["channel" : channel]
return buildMessage(.Subscribe, params: params)
return buildMessage(method: .Subscribe, params: params)
}

func buildSubscribeMessageTo(channel: String, lastMessageUUID: String) -> CentrifugeClientMessage {
let params = ["channel" : channel,
let params: [String : Any] = ["channel" : channel,
"recover" : true,
"last" : lastMessageUUID]
return buildMessage(.Subscribe, params: params as! [String : AnyObject])
return buildMessage(method: .Subscribe, params: params)
}

func buildUnsubscribeMessageFrom(channel: String) -> CentrifugeClientMessage {
let params = ["channel" : channel]
return buildMessage(.Unsubscribe, params: params)
return buildMessage(method: .Unsubscribe, params: params)
}

func buildPublishMessageTo(channel: String, data: [String : AnyObject]) -> CentrifugeClientMessage {
let params:[String : AnyObject] = ["channel" : channel,
"data" : data]
return buildMessage(.Publish, 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)
}

func buildPresenceMessage(channel: String) -> CentrifugeClientMessage {
let params:[String : AnyObject] = ["channel" : channel]
return buildMessage(.Presence, params: params)
let params = ["channel" : channel]
return buildMessage(method: .Presence, params: params)
}

func buildHistoryMessage(channel: String) -> CentrifugeClientMessage {
let params:[String : AnyObject] = ["channel" : channel]
return buildMessage(.History, params: params)
let params = ["channel" : channel]
return buildMessage(method: .History, params: params)
}

func buildPingMessage() -> CentrifugeClientMessage {
return buildMessage(.Ping, params: [:])
return buildMessage(method: .Ping, params: [:])
}

private func buildMessage(method: CentrifugeMethod, params: [String: AnyObject]) -> CentrifugeClientMessage {
private func buildMessage(method: CentrifugeMethod, params: [String: Any]) -> CentrifugeClientMessage {
let uid = generateUUID()
let message = CentrifugeClientMessage(uid: uid, method: method, params: params)
return message
}

private func generateUUID() -> String {
return NSUUID().UUIDString
return NSUUID().uuidString
}

private func createToken(string: String, key: String) -> String {
let hexKey = hexadecimalStringFromData(key.dataUsingEncoding(NSUTF8StringEncoding)!)
let hexString = hexadecimalStringFromData(string.dataUsingEncoding(NSUTF8StringEncoding)!)

let keys5 = arrayFromHexString(hexKey)
let datas5 = arrayFromHexString(hexString)

let hmacs5 = HMAC(algorithm:.SHA256, key:keys5).update(datas5)?.final()
let token = hexStringFromArray(hmacs5!)
let hmacs5 = HMAC(algorithm:.sha256, key:key).update(string: string)?.final()
let token = hexString(fromArray: hmacs5!)
return token
}

private func hexadecimalStringFromData(data: NSData) -> String{
var bytes = [UInt8](count: data.length, repeatedValue: 0)
data.getBytes(&bytes, length: data.length)

let hexString = NSMutableString()
for byte in bytes {
hexString.appendFormat("%02x", UInt(byte))
}

return String(hexString)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
//

protocol CentrifugeServerMessageParser {
func parse(data: NSData) throws -> [CentrifugeServerMessage]
func parse(data: Data) throws -> [CentrifugeServerMessage]
}

class CentrifugeServerMessageParserImpl: CentrifugeServerMessageParser {
func parse(data: NSData) throws -> [CentrifugeServerMessage] {
func parse(data: Data) throws -> [CentrifugeServerMessage] {
do {
let response = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
let response = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions())
var messages = [CentrifugeServerMessage]()

if let infos = response as? [[String : AnyObject]] {
for info in infos {
if let message = messageParse(info){
if let message = messageParse(info: info){
messages.append(message)
}
}
}

if let info = response as? [String : AnyObject] {
if let message = messageParse(info){
if let message = messageParse(info: info){
messages.append(message)
}
}
Expand Down Expand Up @@ -71,4 +71,4 @@ class CentrifugeServerMessageParserImpl: CentrifugeServerMessageParser {

return CentrifugeServerMessage(uid: uid, method: method, error: error, body: body)
}
}
}
80 changes: 40 additions & 40 deletions CentrifugeiOS/Classes/CentrifugeClientImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient {

//MARK: - Public interface
//MARK: Server related method
func connect(completion: CentrifugeMessageHandler) {
func connect(withCompletion completion: @escaping CentrifugeMessageHandler) {
blockingHandler = connectionProcessHandler
connectionCompletion = completion
ws = CentrifugeWebSocket(url)
Expand All @@ -41,49 +41,49 @@ class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient {
ws.close()
}

func ping(completion: CentrifugeMessageHandler) {
func ping(withCompletion completion: @escaping CentrifugeMessageHandler) {
let message = builder.buildPingMessage()
messageCallbacks[message.uid] = completion
send(message)
send(message: message)
}

//MARK: Channel related method
func subscribe(channel: String, delegate: CentrifugeChannelDelegate, completion: CentrifugeMessageHandler) {
let message = builder.buildSubscribeMessageTo(channel)
func subscribe(toChannel channel: String, delegate: CentrifugeChannelDelegate, completion: @escaping CentrifugeMessageHandler) {
let message = builder.buildSubscribeMessageTo(channel: channel)
subscription[channel] = delegate
messageCallbacks[message.uid] = completion
send(message)
send(message: message)
}

func subscribe(channel: String, delegate: CentrifugeChannelDelegate, lastMessageUID uid: String, completion: CentrifugeMessageHandler) {
let message = builder.buildSubscribeMessageTo(channel, lastMessageUUID: uid)
func subscribe(toChannel channel: String, delegate: CentrifugeChannelDelegate, lastMessageUID uid: String, completion: @escaping CentrifugeMessageHandler) {
let message = builder.buildSubscribeMessageTo(channel: channel, lastMessageUUID: uid)
subscription[channel] = delegate
messageCallbacks[message.uid] = completion
send(message)
send(message: message)
}

func publish(channel: String, data: [String : AnyObject], completion: CentrifugeMessageHandler) {
let message = builder.buildPublishMessageTo(channel, data: data)
func publish(toChannel channel: String, data: [String : Any], completion: @escaping CentrifugeMessageHandler) {
let message = builder.buildPublishMessageTo(channel: channel, data: data)
messageCallbacks[message.uid] = completion
send(message)
send(message: message)
}

func unsubscribe(channel: String, completion: CentrifugeMessageHandler) {
let message = builder.buildUnsubscribeMessageFrom(channel)
func unsubscribe(fromChannel channel: String, completion: @escaping CentrifugeMessageHandler) {
let message = builder.buildUnsubscribeMessageFrom(channel: channel)
messageCallbacks[message.uid] = completion
send(message)
send(message: message)
}

func presence(channel: String, completion: CentrifugeMessageHandler) {
let message = builder.buildPresenceMessage(channel)
func presence(inChannel channel: String, completion: @escaping CentrifugeMessageHandler) {
let message = builder.buildPresenceMessage(channel: channel)
messageCallbacks[message.uid] = completion
send(message)
send(message: message)
}

func history(channel: String, completion: CentrifugeMessageHandler) {
let message = builder.buildHistoryMessage(channel)
func history(ofChannel channel: String, completion: @escaping CentrifugeMessageHandler) {
let message = builder.buildHistoryMessage(channel: channel)
messageCallbacks[message.uid] = completion
send(message)
send(message: message)
}

//MARK: - Helpers
Expand All @@ -92,7 +92,7 @@ class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient {
}

func send(message: CentrifugeClientMessage) {
try! ws.send(message)
try! ws.send(centrifugeMessage: message)
}

func setupConnectedState() {
Expand Down Expand Up @@ -133,7 +133,7 @@ class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient {
setupConnectedState()
handler(message, nil)
} else {
let error = NSError.errorWithMessage(message)
let error = NSError.errorWithMessage(message: message)
handler(nil, error)
}
}
Expand All @@ -153,25 +153,25 @@ class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient {
}

for message in msgs {
defaultProcessHandler(message)
defaultProcessHandler(message: message)
}
}

func defaultProcessHandler(message: CentrifugeServerMessage) {
var handled = false
if let uid = message.uid where messageCallbacks[uid] == nil {
if let uid = message.uid, messageCallbacks[uid] == nil {
assertionFailure("Error: Untracked message is received")
return
}

if let uid = message.uid, handler = messageCallbacks[uid] where message.error != nil {
let error = NSError.errorWithMessage(message)
if let uid = message.uid, let handler = messageCallbacks[uid], message.error != nil {
let error = NSError.errorWithMessage(message: message)
handler(nil, error)
messageCallbacks[uid] = nil
return
}

if let uid = message.uid, handler = messageCallbacks[uid] {
if let uid = message.uid, let handler = messageCallbacks[uid] {
handler(message, nil)
messageCallbacks[uid] = nil
handled = true
Expand All @@ -185,30 +185,30 @@ class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient {

// Channel events
case .Message:
guard let channel = message.body?["channel"] as? String, delegate = subscription[channel] else {
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:
guard let channel = message.body?["channel"] as? String, delegate = subscription[channel] else {
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:
guard let channel = message.body?["channel"] as? String, delegate = subscription[channel] else {
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:
guard let channel = message.body?["channel"] as? String, delegate = subscription[channel] else {
guard let channel = message.body?["channel"] as? String, let delegate = subscription[channel] else {
assertionFailure("Error: Invalid \(message.method) handler")
return
}
delegate.client(self, didReceiveUnsubscribeInChannel: channel, message: message)
unsubscribeFrom(channel)
unsubscribeFrom(channel: channel)

// Client events
case .Disconnect:
Expand All @@ -225,13 +225,13 @@ class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient {

//MARK: - WebSocketDelegate
func webSocketOpen() {
let message = builder.buildConnectMessage(creds)
send(message)
let message = builder.buildConnectMessage(credentials: creds)
send(message: message)
}

func webSocketMessageText(text: String) {
let data = text.dataUsingEncoding(NSUTF8StringEncoding)!
let messages = try! parser.parse(data)
func webSocketMessageText(_ text: String) {
let data = text.data(using: String.Encoding.utf8)!
let messages = try! parser.parse(data: data)
messages.forEach { message in
print(message)
}
Expand All @@ -240,15 +240,15 @@ class CentrifugeClientImpl: NSObject, WebSocketDelegate, CentrifugeClient {
}
}

func webSocketClose(code: Int, reason: String, wasClean: Bool) {
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 webSocketError(error: NSError) {
func webSocketError(_ error: NSError) {
if let handler = blockingHandler {
handler(nil, error)
}
Expand Down
Loading

0 comments on commit f9ee74e

Please sign in to comment.