6
6
using Newtonsoft . Json . Converters ;
7
7
using Newtonsoft . Json . Serialization ;
8
8
using Solana . Unity . Rpc . Converters ;
9
+ using System . ComponentModel ;
10
+ using System . Net ;
9
11
using System . Net . Http ;
10
12
using System . Text ;
11
13
using System . Threading . Tasks ;
12
- using JsonException = Newtonsoft . Json . JsonException ;
14
+ using UnityEngine ;
15
+ using UnityEngine . Networking ;
13
16
14
17
namespace Solana . Unity . Rpc . Core . Http
15
18
{
@@ -101,14 +104,12 @@ protected async Task<RequestResult<T>> SendRequest<T>(JsonRpcRequest req)
101
104
} ;
102
105
103
106
// execute POST
104
- using ( var response = await _httpClient . SendAsync ( httpReq ) . ConfigureAwait ( false ) )
107
+ using ( var response = await SendAsyncRequest ( _httpClient , httpReq ) )
105
108
{
106
109
var result = await HandleResult < T > ( req , response ) . ConfigureAwait ( false ) ;
107
110
result . RawRpcRequest = requestJson ;
108
111
return result ;
109
112
}
110
-
111
-
112
113
}
113
114
catch ( HttpRequestException e )
114
115
{
@@ -130,10 +131,10 @@ protected async Task<RequestResult<T>> SendRequest<T>(JsonRpcRequest req)
130
131
}
131
132
return result ;
132
133
}
133
-
134
-
134
+
135
135
}
136
136
137
+
137
138
/// <summary>
138
139
/// Handles the result after sending a request.
139
140
/// </summary>
@@ -143,7 +144,7 @@ protected async Task<RequestResult<T>> SendRequest<T>(JsonRpcRequest req)
143
144
/// <returns>A task that represents the asynchronous operation that holds the request result.</returns>
144
145
private async Task < RequestResult < T > > HandleResult < T > ( JsonRpcRequest req , HttpResponseMessage response )
145
146
{
146
- RequestResult < T > result = new RequestResult < T > ( response ) ;
147
+ RequestResult < T > result = new ( response ) ;
147
148
try
148
149
{
149
150
result . RawRpcResponse = await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
@@ -154,9 +155,10 @@ private async Task<RequestResult<T>> HandleResult<T>(JsonRpcRequest req, HttpRes
154
155
}
155
156
var res = JsonConvert . DeserializeObject < JsonRpcResponse < T > > ( result . RawRpcResponse , _serializerOptions ) ;
156
157
158
+
157
159
if ( res . Result != null )
158
160
{
159
- result . Result = ( T ) res . Result ;
161
+ result . Result = res . Result ;
160
162
result . WasRequestSuccessfullyHandled = true ;
161
163
}
162
164
else
@@ -226,7 +228,7 @@ public async Task<RequestResult<JsonRpcBatchResponse>> SendBatchRequestAsync(Jso
226
228
} ;
227
229
228
230
// execute POST
229
- using ( var response = await _httpClient . SendAsync ( httpReq ) . ConfigureAwait ( false ) )
231
+ using ( var response = await SendAsyncRequest ( _httpClient , httpReq ) )
230
232
{
231
233
var result = await HandleBatchResult ( reqs , response ) . ConfigureAwait ( false ) ;
232
234
result . RawRpcRequest = requestsJson ;
@@ -315,7 +317,74 @@ private async Task<RequestResult<JsonRpcBatchResponse>> HandleBatchResult(JsonRp
315
317
316
318
return result ;
317
319
}
318
-
320
+
321
+ /// <summary>
322
+ /// Return True if running on Unity, False otherwise
323
+ /// </summary>
324
+ /// <returns>Return True if running on Unity, False otherwise</returns>
325
+ private bool IsUnityPlayer ( )
326
+ {
327
+ #if NETSTANDARD2_0 && ! DEBUG
328
+ try
329
+ {
330
+ if ( Application . platform != null )
331
+ {
332
+ return true ;
333
+ }
334
+ }
335
+ catch ( Exception )
336
+ {
337
+ return false ;
338
+ }
339
+ #endif
340
+ return false ;
341
+ }
342
+
343
+ /// <summary>
344
+ /// Send an async request using HttpClient or UnityWebRequest if running on Unity
345
+ /// </summary>
346
+ /// <param name="httpClient"></param>
347
+ /// <param name="httpReq"></param>
348
+ /// <returns></returns>
349
+ private async Task < HttpResponseMessage > SendAsyncRequest ( HttpClient httpClient , HttpRequestMessage httpReq )
350
+ {
351
+ if ( IsUnityPlayer ( ) )
352
+ {
353
+ return await SendUnityWebRequest ( httpClient . BaseAddress , httpReq ) ;
354
+ }
355
+ return await _httpClient . SendAsync ( httpReq ) . ConfigureAwait ( false ) ;
356
+ }
357
+
358
+ /// <summary>
359
+ /// Convert a httReq to a Unity Web request
360
+ /// </summary>
361
+ /// <param name="uri">RPC URI</param>
362
+ /// <param name="httpReq">The http request</param>
363
+ /// <returns>Http response</returns>
364
+ /// <exception cref="HttpRequestException"></exception>
365
+ private async Task < HttpResponseMessage > SendUnityWebRequest ( Uri uri , HttpRequestMessage httpReq )
366
+ {
367
+ Byte [ ] buffer = await httpReq . Content . ReadAsByteArrayAsync ( ) ;
368
+ Console . WriteLine ( $ "Send A") ;
369
+ using ( var request = new UnityWebRequest ( uri , httpReq . Method . ToString ( ) ) )
370
+ {
371
+ request . uploadHandler = new UploadHandlerRaw ( buffer ) ;
372
+ request . downloadHandler = new DownloadHandlerBuffer ( ) ;
373
+ request . SetRequestHeader ( "Content-Type" , "application/json" ) ;
374
+ request . SendWebRequest ( ) ;
375
+ if ( request . result == UnityWebRequest . Result . ConnectionError )
376
+ {
377
+ throw new HttpRequestException ( "Error While Sending: " + request . error ) ;
378
+ }
379
+ while ( ! request . isDone )
380
+ {
381
+ await Task . Yield ( ) ;
382
+ }
383
+ var response = new HttpResponseMessage ( HttpStatusCode . OK ) ;
384
+ response . Content = new ByteArrayContent ( Encoding . UTF8 . GetBytes ( request . downloadHandler . text ) ) ;
385
+ return response ;
386
+ }
387
+ }
319
388
}
320
389
321
390
}
0 commit comments