From 308d9c45a9add4ec7d393687b13f05f991241415 Mon Sep 17 00:00:00 2001 From: Muhammad Rifqi Fatchurrahman Date: Fri, 7 Dec 2018 11:57:13 +0700 Subject: [PATCH] function to remove emitter event & can access it from clusterWS instance --- Sources/CWSEmitter.swift | 16 +++++---- Sources/ClusterWS.swift | 74 ++++++++++++++++++++++------------------ 2 files changed, 51 insertions(+), 39 deletions(-) diff --git a/Sources/CWSEmitter.swift b/Sources/CWSEmitter.swift index 3215458..4628035 100644 --- a/Sources/CWSEmitter.swift +++ b/Sources/CWSEmitter.swift @@ -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() } diff --git a/Sources/ClusterWS.swift b/Sources/ClusterWS.swift index 835ce16..2ac1441 100644 --- a/Sources/ClusterWS.swift +++ b/Sources/ClusterWS.swift @@ -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() @@ -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) @@ -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] { @@ -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 { @@ -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) } @@ -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)