88import Foundation
99
1010open class ClusterWS : NSObject {
11-
11+
1212 // MARK: - Properties
13-
13+
1414 public var delegate : CWSDelegate ?
15-
15+
1616 // MARK: - Internal
17-
17+
1818 private var mChannels : [ CWSChannel ] = [ ]
1919 private var mWebSocket : WebSocket ?
20-
20+
2121 // MARK: - Dependencies
22-
22+
2323 private let mEmitter : CWSEmitter
2424 private let mPingHandler : CWSPing
2525 private lazy var mReconnection = CWSReconnection ( socket: self )
2626 private lazy var mParser = CWSParser ( socket: self )
27-
27+
2828 // MARK: - Settings
29-
29+
3030 private var mPingInterval : TimeInterval ?
3131 private var mUseBinary : Bool = false
3232 private let mUrl : String
3333 private var mPingBinary : Data !
34-
34+
3535 // MARK: - Static
36-
36+
3737 static let mPingMessage : String = " A "
38-
38+
3939 public init ( url: String ) {
4040 self . mUrl = url
4141 self . mEmitter = CWSEmitter ( )
@@ -46,30 +46,30 @@ open class ClusterWS: NSObject {
4646// MARK: - Public methods
4747
4848extension ClusterWS {
49-
49+
5050 public func connect( ) {
5151 guard let url = URL ( string: self . mUrl) else {
5252 self . delegate? . onError ( error: CWSError . invalidURL ( self . mUrl) )
5353 return
5454 }
55-
55+
5656 guard let binary = self . mParser. encode ( message: ClusterWS . mPingMessage) else {
5757 self . delegate? . onError ( error: CWSError . binaryEncodeError ( ClusterWS . mPingMessage) )
5858 return
5959 }
60-
60+
6161 self . mPingBinary = binary
62-
62+
6363 self . mWebSocket = WebSocket ( url: url)
64-
64+
6565 if self . mUrl. range ( of: " wss:// " ) != nil {
6666 self . mWebSocket? . allowSelfSignedSSL = true
6767 }
68-
68+
6969 self . mWebSocket? . event. open = {
7070 self . mReconnection. onConnected ( )
7171 }
72-
72+
7373 self . mWebSocket? . event. close = { code, reason, clean in
7474 self . mPingHandler. stop ( )
7575 self . delegate? . onDisconnect ( code: code, reason: reason)
@@ -80,11 +80,11 @@ extension ClusterWS {
8080 self . mReconnection. reconnect ( )
8181 }
8282 }
83-
83+
8484 self . mWebSocket? . event. error = { error in
8585 self . delegate? . onError ( error: error)
8686 }
87-
87+
8888 self . mWebSocket? . event. message = { message in
8989 var string : String = " "
9090 if let binary = message as? [ UInt8 ] {
@@ -110,11 +110,11 @@ extension ClusterWS {
110110 self . mParser. handleMessage ( with: string)
111111 }
112112 }
113-
113+
114114 public func on( event: String , completion: @escaping CompletionHandler ) {
115115 self . mEmitter. on ( event: event, completion: completion)
116116 }
117-
117+
118118 public func subscribe( _ channelName: String ) -> CWSChannel {
119119 var channel = self . mChannels. filter { $0. mChannelName == channelName } . map { $0 } . first
120120 if channel == nil {
@@ -123,30 +123,30 @@ extension ClusterWS {
123123 }
124124 return channel!
125125 }
126-
126+
127127 public func getChannels( ) -> [ CWSChannel ] {
128128 return self . mChannels
129129 }
130-
130+
131131 public func getChannel( by name: String ) -> CWSChannel ? {
132132 return self . mChannels. filter { $0. mChannelName == name } . first
133133 }
134-
134+
135135 public func disconnect( closeCode: Int ? = nil , reason: String ? = nil ) {
136136 self . mWebSocket? . close ( closeCode == nil ? 1000 : closeCode!, reason: reason == nil ? " " : reason!)
137137 }
138-
138+
139139 public func setReconnection( autoReconnect: Bool , reconnectionIntervalMin: Double ? = nil , reconnectionIntervalMax: Double ? = nil , reconnectionAttempts: Int ? = nil ) {
140140 self . mReconnection. setReconnection ( autoReconnect: autoReconnect, reconnectionIntervalMin: reconnectionIntervalMin, reconnectionIntervalMax: reconnectionIntervalMax, reconnectionAttempts: reconnectionAttempts)
141141 }
142-
142+
143143 public func getState( ) -> WebSocketReadyState {
144144 guard let state = self . mWebSocket? . readyState else {
145145 return . closed
146146 }
147147 return state
148148 }
149-
149+
150150 public func send( event: String , data: Any ? = nil ) {
151151 self . send ( event: event, data: data, type: . emit)
152152 }
@@ -173,23 +173,31 @@ extension ClusterWS {
173173 self . mWebSocket? . send ( anyData)
174174 }
175175 }
176-
176+
177177 open func setBinary( to binary: Bool ) {
178178 self . mUseBinary = binary
179179 }
180-
180+
181181 open func emit( event: String , data: Any ) {
182182 self . mEmitter. emit ( event: event, data: data)
183183 }
184-
184+
185+ open func removeEvent( _ event: String ) {
186+ self . mEmitter. remove ( event: event)
187+ }
188+
189+ open func removeAllEvents( ) {
190+ self . mEmitter. removeAllEvents ( )
191+ }
192+
185193 open func removeChannel( _ channel: CWSChannel ) {
186194 self . mChannels = self . mChannels. filter { $0 != channel }
187195 }
188-
196+
189197 open func resetPing( with interval: TimeInterval ) {
190198 self . mPingHandler. restart ( with: interval, socket: self )
191199 }
192-
200+
193201 open func setPingInterval( _ interval: TimeInterval ) {
194202 self . mPingInterval = interval
195203 self . resetPing ( with: interval)
0 commit comments