@@ -55,7 +55,6 @@ class Message extends Base {
5555 transactionId = 1
5656
5757 /**
58- *
5958 * @param {Object } msg
6059 * @returns {Promise<any> }
6160 */
@@ -65,6 +64,32 @@ class Message extends Base {
6564 return this [ `onMessage${ Neo . capitalize ( api . type ) } ` ] ( msg , api ) ;
6665 }
6766
67+ /**
68+ * @param {Object } msg
69+ * @param {Function } callback
70+ */
71+ onMessageStream ( msg , callback ) {
72+ let api = Neo . manager . rpc . Api . get ( `${ msg . service } .${ msg . method } ` ) ;
73+
74+ if ( api . type !== 'websocket' ) {
75+ console . error ( 'onMessageStream is only supported for websocket connections' , msg ) ;
76+ return
77+ }
78+
79+ this . onMessageStreamWebsocket ( msg , api , callback )
80+ }
81+
82+ /**
83+ * @param {Object } msg
84+ */
85+ onMessageStreamUnsubscribe ( msg ) {
86+ let api = Neo . manager . rpc . Api . get ( `${ msg . service } .${ msg . method } ` ) ;
87+
88+ if ( api ?. type === 'websocket' ) {
89+ this . onMessageStreamUnsubscribeWebsocket ( msg , api )
90+ }
91+ }
92+
6893 /**
6994 *
7095 * @param {Object } msg
@@ -101,7 +126,6 @@ class Message extends Base {
101126 }
102127
103128 /**
104- *
105129 * @param {Object } msg
106130 * @param {Object } api
107131 * @protected
@@ -121,6 +145,39 @@ class Message extends Base {
121145 return await connection . promiseMessage ( msg )
122146 }
123147
148+ /**
149+ * @param {Object } msg
150+ * @param {Object } api
151+ * @param {Function } callback
152+ * @protected
153+ * @returns {Promise<any> }
154+ */
155+ async onMessageStreamWebsocket ( msg , api , callback ) {
156+ let me = this ,
157+ { url} = api ,
158+ connection = me . socketConnections [ url ] ;
159+
160+ if ( ! connection ) {
161+ let module = await import ( '../../data/connection/WebSocket.mjs' ) ;
162+
163+ me . socketConnections [ url ] = connection = Neo . create ( module . default , { serverAddress : url } )
164+ }
165+
166+ connection . registerStream ( msg , callback )
167+ }
168+
169+ /**
170+ * @param {Object } msg
171+ * @param {Object } api
172+ * @protected
173+ */
174+ onMessageStreamUnsubscribeWebsocket ( msg , api ) {
175+ let connection = this . socketConnections [ api . url ] ;
176+ if ( connection ) {
177+ connection . unregisterStream ( msg )
178+ }
179+ }
180+
124181 /**
125182 * @param {String } url
126183 */
0 commit comments