@@ -77,7 +77,7 @@ class WebSocketTransport implements DataConnectTransport {
7777 requestId: _generateRequestId ('auth' ),
7878 headers: _buildHeaders (token, null ),
7979 );
80- _channel ? .sink. add ( jsonEncode ( request.toJson () ));
80+ _send ( request.toJson ());
8181 } catch (_) {
8282 // Ignored
8383 }
@@ -146,6 +146,15 @@ class WebSocketTransport implements DataConnectTransport {
146146 return '${operationName }_$randStr ' ;
147147 }
148148
149+ void _send (Map <String , dynamic > json) {
150+ if (_channel == null ) return ;
151+ final encoded = jsonEncode (json);
152+ if (encoded.isNotEmpty) {
153+ developer.log ("Sending stream message \n $encoded " );
154+ _channel! .sink.add (encoded);
155+ }
156+ }
157+
149158 bool get isConnected => _channel != null ;
150159
151160 Map <String , String > _buildHeaders (String ? authToken, String ? appCheckToken) {
@@ -208,7 +217,7 @@ class WebSocketTransport implements DataConnectTransport {
208217 'projects/${options .projectId }/locations/${options .location }/services/${options .serviceId }/connectors/${options .connector }' ,
209218 headers: headers,
210219 );
211- _channel ? .sink. add ( jsonEncode ( initRequest.toJson () ));
220+ _send ( initRequest.toJson ());
212221 }
213222
214223 // called when a message is received from the stream
@@ -356,7 +365,7 @@ class WebSocketTransport implements DataConnectTransport {
356365 subscribe: ExecuteRequest (sub.queryName, sub.variables),
357366 headers: headers,
358367 );
359- _channel ? .sink. add ( jsonEncode ( request.toJson () ));
368+ _send ( request.toJson ());
360369 }
361370 }
362371
@@ -379,7 +388,7 @@ class WebSocketTransport implements DataConnectTransport {
379388 execute: ExecuteRequest (p.operationName, p.variables),
380389 headers: headers,
381390 );
382- _channel ? .sink. add ( jsonEncode ( request.toJson () ));
391+ _send ( request.toJson ());
383392 }
384393 }
385394 if (kept.isNotEmpty) {
@@ -506,7 +515,7 @@ class WebSocketTransport implements DataConnectTransport {
506515 resume: ResumeRequest (),
507516 headers: headers,
508517 );
509- _channel ? .sink. add ( jsonEncode ( request.toJson () ));
518+ _send ( request.toJson ());
510519
511520 return completer.future;
512521 }
@@ -537,7 +546,7 @@ class WebSocketTransport implements DataConnectTransport {
537546 headers: headers,
538547 );
539548
540- _channel ? .sink. add ( jsonEncode ( request.toJson () ));
549+ _send ( request.toJson ());
541550
542551 return completer.future;
543552 }
@@ -606,11 +615,7 @@ class WebSocketTransport implements DataConnectTransport {
606615 headers: headers,
607616 );
608617
609- if (_channel != null ) {
610- final encodedMessage = jsonEncode (request.toJson ());
611- developer.log ('Sending subscribe message $encodedMessage ' );
612- _channel? .sink.add (encodedMessage);
613- }
618+ _send (request.toJson ());
614619 },
615620 onCancel: () {
616621 if (! _activeSubscriptions.containsKey (operationId)) return ;
@@ -624,14 +629,12 @@ class WebSocketTransport implements DataConnectTransport {
624629 _activeSubscriptions.remove (operationId);
625630 _pendingSubscriptions.remove (requestId);
626631
627- if (_channel != null ) {
628- final cancelReq = StreamRequest (
629- requestId: requestId,
630- requestKind: RequestKind .cancel,
631- cancel: true ,
632- );
633- _channel? .sink.add (jsonEncode (cancelReq.toJson ()));
634- }
632+ final cancelReq = StreamRequest (
633+ requestId: requestId,
634+ requestKind: RequestKind .cancel,
635+ cancel: true ,
636+ );
637+ _send (cancelReq.toJson ());
635638 _checkIdleAndDisconnect ();
636639 }
637640 }
0 commit comments