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

Commit

Permalink
Merge pull request #6 from muhrifqii/master
Browse files Browse the repository at this point in the history
Function to remove emitter event & can access it from clusterWS instance
  • Loading branch information
romanbaitaliuk authored Dec 12, 2018
2 parents eefd92b + 308d9c4 commit 52633bb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 39 deletions.
16 changes: 10 additions & 6 deletions Sources/CWSEmitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ public typealias CompletionHandler = (Any) -> Void

open class CWSEmitter {
private var mEvents: [(string: String, completion: CompletionHandler)] = []

public func on(event: String, completion: @escaping CompletionHandler) {
if let index = self.mEvents.index(where: { $0.0 == event }) {
self.mEvents.remove(at: index)
}
self.remove(event: event)
mEvents.append((string: event, completion: completion))
}

public func emit(event: String, data: Any) {
if let index = self.mEvents.index(where: { $0.0 == event }) {
self.mEvents[index].completion(data)
}
}


public func remove(event: String) {
if let index = self.mEvents.index(where: { $0.0 == event }) {
self.mEvents.remove(at: index)
}
}

public func removeAllEvents() {
self.mEvents.removeAll()
}
Expand Down
74 changes: 41 additions & 33 deletions Sources/ClusterWS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,34 @@
import Foundation

open class ClusterWS: NSObject {

// MARK: - Properties

public var delegate: CWSDelegate?

// MARK: - Internal

private var mChannels: [CWSChannel] = []
private var mWebSocket: WebSocket?

// MARK: - Dependencies

private let mEmitter: CWSEmitter
private let mPingHandler: CWSPing
private lazy var mReconnection = CWSReconnection(socket: self)
private lazy var mParser = CWSParser(socket: self)

// MARK: - Settings

private var mPingInterval: TimeInterval?
private var mUseBinary: Bool = false
private let mUrl: String
private var mPingBinary: Data!

// MARK: - Static

static let mPingMessage: String = "A"

public init(url: String) {
self.mUrl = url
self.mEmitter = CWSEmitter()
Expand All @@ -46,30 +46,30 @@ open class ClusterWS: NSObject {
// MARK: - Public methods

extension ClusterWS {

public func connect() {
guard let url = URL(string: self.mUrl) else {
self.delegate?.onError(error: CWSError.invalidURL(self.mUrl))
return
}

guard let binary = self.mParser.encode(message: ClusterWS.mPingMessage) else {
self.delegate?.onError(error: CWSError.binaryEncodeError(ClusterWS.mPingMessage))
return
}

self.mPingBinary = binary

self.mWebSocket = WebSocket(url: url)

if self.mUrl.range(of: "wss://") != nil {
self.mWebSocket?.allowSelfSignedSSL = true
}

self.mWebSocket?.event.open = {
self.mReconnection.onConnected()
}

self.mWebSocket?.event.close = { code, reason, clean in
self.mPingHandler.stop()
self.delegate?.onDisconnect(code: code, reason: reason)
Expand All @@ -80,11 +80,11 @@ extension ClusterWS {
self.mReconnection.reconnect()
}
}

self.mWebSocket?.event.error = { error in
self.delegate?.onError(error: error)
}

self.mWebSocket?.event.message = { message in
var string: String = ""
if let binary = message as? [UInt8] {
Expand All @@ -110,11 +110,11 @@ extension ClusterWS {
self.mParser.handleMessage(with: string)
}
}

public func on(event: String, completion: @escaping CompletionHandler) {
self.mEmitter.on(event: event, completion: completion)
}

public func subscribe(_ channelName: String) -> CWSChannel {
var channel = self.mChannels.filter { $0.mChannelName == channelName }.map { $0 }.first
if channel == nil {
Expand All @@ -123,30 +123,30 @@ extension ClusterWS {
}
return channel!
}

public func getChannels() -> [CWSChannel] {
return self.mChannels
}

public func getChannel(by name: String) -> CWSChannel? {
return self.mChannels.filter { $0.mChannelName == name }.first
}

public func disconnect(closeCode: Int? = nil, reason: String? = nil) {
self.mWebSocket?.close(closeCode == nil ? 1000 : closeCode!, reason: reason == nil ? "" : reason!)
}

public func setReconnection(autoReconnect: Bool, reconnectionIntervalMin: Double? = nil, reconnectionIntervalMax: Double? = nil, reconnectionAttempts: Int? = nil) {
self.mReconnection.setReconnection(autoReconnect: autoReconnect, reconnectionIntervalMin: reconnectionIntervalMin, reconnectionIntervalMax: reconnectionIntervalMax, reconnectionAttempts: reconnectionAttempts)
}

public func getState() -> WebSocketReadyState {
guard let state = self.mWebSocket?.readyState else {
return .closed
}
return state
}

public func send(event: String, data: Any? = nil) {
self.send(event: event, data: data, type: .emit)
}
Expand All @@ -173,23 +173,31 @@ extension ClusterWS {
self.mWebSocket?.send(anyData)
}
}

open func setBinary(to binary: Bool) {
self.mUseBinary = binary
}

open func emit(event: String, data: Any) {
self.mEmitter.emit(event: event, data: data)
}


open func removeEvent(_ event: String) {
self.mEmitter.remove(event: event)
}

open func removeAllEvents() {
self.mEmitter.removeAllEvents()
}

open func removeChannel(_ channel: CWSChannel) {
self.mChannels = self.mChannels.filter { $0 != channel }
}

open func resetPing(with interval: TimeInterval) {
self.mPingHandler.restart(with: interval, socket: self)
}

open func setPingInterval(_ interval: TimeInterval) {
self.mPingInterval = interval
self.resetPing(with: interval)
Expand Down

0 comments on commit 52633bb

Please sign in to comment.