@@ -24,13 +24,15 @@ public static class HttpUtils
2424 /// Called when the websocket receives subscription data.
2525 /// </summary>
2626 public static event Action < string > SubscriptionDataReceived ;
27-
27+ public static Dictionary < string , Action < string > > SubscriptionDataReceivedPerChannel ;
2828 public static HttpClient httpClient ;
29+
2930 [ RuntimeInitializeOnLoadMethod ( RuntimeInitializeLoadType . BeforeSceneLoad ) ]
3031 public static void PreInit ( )
3132 {
3233 _webSocket ? . Dispose ( ) ;
3334 SubscriptionDataReceived = null ;
35+ SubscriptionDataReceivedPerChannel = new Dictionary < string , Action < string > > ( ) ;
3436
3537 // Create a New HttpClient object.
3638 InitializeHttpClient ( ) ;
@@ -67,6 +69,7 @@ public static void Dispose()
6769 public static async Task < string > PostRequest (
6870 string url ,
6971 Request request ,
72+ JsonSerializerSettings serializerSettings = null ,
7073 Dictionary < string , string > headers = null ,
7174 string authToken = null ,
7275 string authScheme = null ,
@@ -83,6 +86,7 @@ public static async Task<string> PostRequest(
8386 string payload = request . ToJson ( ) ;
8487
8588 var requestMessage = new HttpRequestMessage ( ) ;
89+ //byte[] payload = request.ToBytes(serializerSettings);
8690
8791 if ( authToken != null )
8892 {
@@ -168,12 +172,23 @@ public static async Task WebSocketConnect(
168172 _webSocket = new ClientWebSocket ( ) ;
169173 _webSocket . Options . AddSubProtocol ( protocol ) ;
170174
171- if ( authToken != null )
172- {
173- _webSocket . Options . SetRequestHeader ( "Authorization" , $ "{ authScheme } { authToken } ") ;
175+ var payload = new Dictionary < string , string > ( ) ;
176+
177+ if ( protocol == "graphql-transport-ws" ) {
178+ payload [ "content-type" ] = "application/json" ;
179+ } else {
180+ _webSocket . Options . SetRequestHeader ( "Content-Type" , "application/json" ) ;
181+ }
182+
183+ if ( authToken != null ) {
184+ if ( protocol == "graphql-transport-ws" ) {
185+ // set Authorization as payload
186+ payload [ "Authorization" ] = $ "{ authScheme } { authToken } ";
187+ } else {
188+ _webSocket . Options . SetRequestHeader ( "Authorization" , $ "{ authScheme } { authToken } ") ;
189+ }
174190 }
175191
176- _webSocket . Options . SetRequestHeader ( "Content-Type" , "application/json" ) ;
177192
178193 if ( headers != null )
179194 {
@@ -188,12 +203,23 @@ public static async Task WebSocketConnect(
188203 Debug . Log ( "Websocket is connecting" ) ;
189204 await _webSocket . ConnectAsync ( uri , CancellationToken . None ) ;
190205
206+ var json = JsonConvert . SerializeObject (
207+ new
208+ {
209+ type = "connection_init" ,
210+ payload = payload
211+ } ,
212+ Formatting . None ,
213+ new JsonSerializerSettings
214+ {
215+ NullValueHandling = NullValueHandling . Ignore
216+ }
217+ ) ;
218+
191219 Debug . Log ( "Websocket is starting" ) ;
192220 // Initialize the socket at the server side
193221 await _webSocket . SendAsync (
194- new ArraySegment < byte > (
195- Encoding . UTF8 . GetBytes ( @"{""type"":""connection_init"",""payload"": {}}" )
196- ) ,
222+ new ArraySegment < byte > ( Encoding . UTF8 . GetBytes ( json ) ) ,
197223 WebSocketMessageType . Text ,
198224 true ,
199225 CancellationToken . None
@@ -242,7 +268,7 @@ public static async Task<bool> WebSocketSubscribe(string id, Request request)
242268 new
243269 {
244270 id ,
245- type = "start" ,
271+ type = _webSocket . SubProtocol == "graphql-transport-ws" ? "subscribe" : "start" ,
246272 payload = new
247273 {
248274 query = request . Query ,
@@ -280,8 +306,10 @@ public static async Task WebSocketUnsubscribe(string id)
280306 return ;
281307 }
282308
309+ var type = _webSocket . SubProtocol == "graphql-transport-ws" ? "complete" : "stop" ;
310+
283311 await _webSocket . SendAsync (
284- new ArraySegment < byte > ( Encoding . UTF8 . GetBytes ( $@ "{{""type"":""stop "",""id"":""{ id } ""}}") ) ,
312+ new ArraySegment < byte > ( Encoding . UTF8 . GetBytes ( $@ "{{""type"":""{ type } "",""id"":""{ id } ""}}") ) ,
285313 WebSocketMessageType . Text ,
286314 true ,
287315 CancellationToken . None
@@ -324,17 +352,18 @@ private static async void WebSocketUpdate()
324352 }
325353
326354 var msgType = ( string ) jsonObj [ "type" ] ;
355+ var id = ( string ) jsonObj [ "id" ] ;
327356 switch ( msgType )
328357 {
329358 case "connection_error" :
330359 {
331360 throw new WebSocketException ( "Connection error. Error: " + jsonResult ) ;
332361 }
333362 case "connection_ack" :
334- {
335- Debug . Log ( "Websocket connection acknowledged." ) ;
336- continue ;
337- }
363+ {
364+ Debug . Log ( $ "Websocket connection acknowledged ( { id } ) .") ;
365+ continue ;
366+ }
338367 case "data" :
339368 case "next" :
340369 {
@@ -380,4 +409,4 @@ await _webSocket.SendAsync(
380409 }
381410 }
382411 }
383- }
412+ }
0 commit comments