|
1 | | -using System; |
| 1 | +using NativeWebSocket; |
| 2 | +using System; |
2 | 3 | using System.Net.WebSockets; |
3 | 4 | using System.Threading; |
4 | 5 | using System.Threading.Tasks; |
| 6 | +using WebSocket = NativeWebSocket.WebSocket; |
| 7 | +using WebSocketState = System.Net.WebSockets.WebSocketState; |
5 | 8 |
|
6 | 9 | namespace Solana.Unity.Rpc.Core.Sockets |
7 | 10 | { |
8 | 11 | internal class WebSocketWrapper : IWebSocket |
9 | 12 | { |
10 | | - private readonly ClientWebSocket webSocket; |
| 13 | + private NativeWebSocket.IWebSocket webSocket; |
11 | 14 |
|
12 | | - internal WebSocketWrapper(ClientWebSocket webSocket) |
13 | | - { |
14 | | - this.webSocket = webSocket; |
15 | | - } |
16 | | - |
17 | | - public WebSocketCloseStatus? CloseStatus => webSocket.CloseStatus; |
| 15 | + public WebSocketCloseStatus? CloseStatus => WebSocketCloseStatus.NormalClosure; |
18 | 16 |
|
19 | | - public string CloseStatusDescription => webSocket.CloseStatusDescription; |
| 17 | + public string CloseStatusDescription => "Not implemented"; |
20 | 18 |
|
21 | | - public WebSocketState State => webSocket.State; |
| 19 | + public WebSocketState State |
| 20 | + { |
| 21 | + get |
| 22 | + { |
| 23 | + if(webSocket == null) |
| 24 | + return WebSocketState.None; |
| 25 | + return webSocket.State switch |
| 26 | + { |
| 27 | + NativeWebSocket.WebSocketState.Open => WebSocketState.Open, |
| 28 | + NativeWebSocket.WebSocketState.Closed => WebSocketState.Closed, |
| 29 | + NativeWebSocket.WebSocketState.Connecting => WebSocketState.Connecting, |
| 30 | + NativeWebSocket.WebSocketState.Closing => WebSocketState.CloseReceived, |
| 31 | + _ => WebSocketState.None |
| 32 | + }; |
| 33 | + } |
| 34 | + } |
22 | 35 |
|
23 | 36 | public Task CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken) |
24 | | - => webSocket.CloseAsync(closeStatus, statusDescription, cancellationToken); |
| 37 | + => webSocket.Close(); |
25 | 38 |
|
26 | 39 | public Task ConnectAsync(Uri uri, CancellationToken cancellationToken) |
27 | | - => webSocket.ConnectAsync(uri, cancellationToken); |
| 40 | + { |
| 41 | + webSocket = WebSocket.Create(uri.AbsoluteUri); |
| 42 | + return webSocket.Connect(); |
| 43 | + } |
28 | 44 |
|
29 | 45 | public Task CloseAsync(CancellationToken cancellationToken) |
30 | | - => webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, cancellationToken); |
| 46 | + => webSocket.Close(); |
31 | 47 |
|
32 | 48 | public Task<WebSocketReceiveResult> ReceiveAsync(Memory<byte> buffer, CancellationToken cancellationToken) |
33 | | - => webSocket.ReceiveAsync(new ArraySegment<byte>(buffer.ToArray()), cancellationToken); |
| 49 | + { |
| 50 | + TaskCompletionSource<WebSocketReceiveResult> receiveMessageTask = new(); |
| 51 | + |
| 52 | + void WebSocketOnOnMessage(byte[] bytes) |
| 53 | + { |
| 54 | + bytes.CopyTo(buffer); |
| 55 | + WebSocketReceiveResult webSocketReceiveResult = new(bytes.Length, WebSocketMessageType.Text, true); |
| 56 | + MainThreadUtil.Run(() => receiveMessageTask.SetResult(webSocketReceiveResult)); |
| 57 | + webSocket.OnMessage -= WebSocketOnOnMessage; |
| 58 | + Console.WriteLine("Message received"); |
| 59 | + } |
| 60 | + webSocket.OnMessage += WebSocketOnOnMessage; |
| 61 | + return receiveMessageTask.Task; |
| 62 | + } |
34 | 63 |
|
35 | | - public Task SendAsync(ReadOnlyMemory<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken) |
36 | | - => webSocket.SendAsync(new ArraySegment<byte>(buffer.ToArray()), messageType, endOfMessage, cancellationToken); |
| 64 | + public Task SendAsync(ReadOnlyMemory<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, |
| 65 | + CancellationToken cancellationToken) |
| 66 | + { |
| 67 | + return webSocket.Send(buffer.ToArray()); |
| 68 | + } |
37 | 69 |
|
38 | 70 | #region IDisposable Support |
39 | | - private bool disposedValue = false; // To detect redundant calls |
| 71 | + private bool disposedValue; // To detect redundant calls |
40 | 72 |
|
41 | 73 | private void Dispose(bool disposing) |
42 | 74 | { |
43 | 75 | if (!disposedValue) |
44 | 76 | { |
45 | 77 | if (disposing) |
46 | 78 | { |
47 | | - webSocket.Dispose(); |
| 79 | + webSocket.Close(); |
48 | 80 | } |
49 | 81 |
|
50 | 82 | disposedValue = true; |
|
0 commit comments