@@ -12,20 +12,10 @@ import RxSwift
1212import Starscream
1313
1414
15- /**
16- * This is the abstraction over Starscream to make it reactive.
17- */
18- public class RxWebSocket : WebSocket {
15+ /// This is the abstraction over Starscream to make it reactive.
16+ open class RxWebSocket : WebSocket {
1917
20- /**
21- Every message received by the websocket is converted to an `StreamEvent`.
22-
23- - connect: The "connect" message, flagging that the websocket did connect to the server.
24- - disconnect: A disconnect message that may contain an `Error` containing the reason for the disconection.
25- - pong: The "pong" message the server may respond to a "ping".
26- - text: Any string messages received by the client.
27- - data: Any data messages received by the client, excluding strings.
28- */
18+ /// Every message received by the websocket is converted to an `StreamEvent`.
2919 public enum StreamEvent {
3020 /// The "connect" message, flagging that the websocket did connect to the server.
3121 case connect
@@ -43,17 +33,17 @@ public class RxWebSocket: WebSocket {
4333 case data( Data )
4434 }
4535
46- // MARK: Private Properties
47-
4836 fileprivate let publishStream : PublishSubject < StreamEvent >
4937
5038 /**
51- The creation of a `RxWebSocket` object. The client is automatically connected to the server uppon initialization.
52-
53- - parameter url: The server url .
54- - parameter protocols: The protocols that should be used in the comms. May be nil .
55-
39+ - parameters:
40+ - request: A URL Request to be started.
41+ - protocols: The protocols that should be used in the comms. May be nil .
42+ - stream: A stream to which the client should connect .
43+
5644 - returns: An instance of `RxWebSocket`
45+
46+ The creation of a `RxWebSocket` object. The client is automatically connected to the server uppon initialization.
5747 */
5848 override public init ( request: URLRequest , protocols: [ String ] ? = nil , stream: WSStream = FoundationStream ( ) ) {
5949 let publish = PublishSubject < StreamEvent > ( )
@@ -69,6 +59,22 @@ public class RxWebSocket: WebSocket {
6959
7060 connect ( )
7161 }
62+
63+ /**
64+ - parameters:
65+ - url: The server url.
66+ - protocols: The protocols that should be used in the comms. May be nil.
67+
68+ - returns: An instance of `RxWebSocket`
69+
70+ The creation of a `RxWebSocket` object. The client is automatically connected to the server uppon initialization.
71+ */
72+ public convenience init ( url: URL , protocols: [ String ] ? = nil ) {
73+ self . init (
74+ request: URLRequest ( url: url) ,
75+ protocols: protocols
76+ )
77+ }
7278}
7379
7480
@@ -84,12 +90,12 @@ public extension Reactive where Base: RxWebSocket {
8490 return Observable . just ( text)
8591 }
8692
87- return ControlProperty ( values: values, valueSink: AnyObserver { event in
93+ return ControlProperty ( values: values, valueSink: AnyObserver { [ weak base ] event in
8894 guard case . next( let text) = event else {
8995 return
9096 }
9197
92- self . base. write ( string: text)
98+ base? . write ( string: text)
9399 } )
94100 }
95101
@@ -103,12 +109,12 @@ public extension Reactive where Base: RxWebSocket {
103109 return Observable . just ( data)
104110 }
105111
106- return ControlProperty ( values: values, valueSink: AnyObserver { event in
112+ return ControlProperty ( values: values, valueSink: AnyObserver { [ weak base ] event in
107113 guard case . next( let data) = event else {
108114 return
109115 }
110116
111- self . base. write ( data: data)
117+ base? . write ( data: data)
112118 } )
113119 }
114120
0 commit comments