| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using HybridWebSocket; |
| | 3 | | using rpc_csharp.transport; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Text; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace RPC.Transports |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// This WebSocketClientTransport contains authentication with signed headers out of the box. The process is the fol |
| | 14 | | /// 1. Request the signed headers using the IRPCSignRequest provided. |
| | 15 | | /// 2. Connect to the WebSocket server. |
| | 16 | | /// 3. Send the signed headers to the server. |
| | 17 | | /// </summary> |
| | 18 | | public class AuthedWebSocketClientTransport : ITransport |
| | 19 | | { |
| | 20 | | private bool VERBOSE = false; |
| | 21 | |
|
| | 22 | | public event Action OnCloseEvent; |
| | 23 | | public event Action<string> OnErrorEvent; |
| | 24 | | public event Action<byte[]> OnMessageEvent; |
| | 25 | | public event Action OnConnectEvent; |
| | 26 | |
|
| | 27 | | private readonly IRPCSignRequest signRequest; |
| | 28 | | private readonly WebSocket webSocket; |
| | 29 | |
|
| 0 | 30 | | public AuthedWebSocketClientTransport(IRPCSignRequest signRequest, string url) |
| | 31 | | { |
| 0 | 32 | | webSocket = WebSocketFactory.CreateInstance(url); |
| 0 | 33 | | this.signRequest = signRequest; |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | public async UniTask Connect(string requestUrl, CancellationToken ct = default) |
| | 37 | | { |
| 0 | 38 | | webSocket.OnMessage += this.HandleMessage; |
| 0 | 39 | | webSocket.OnError += this.HandleError; |
| 0 | 40 | | webSocket.OnClose += this.HandleClose; |
| 0 | 41 | | webSocket.OnOpen += this.HandleOpen; |
| | 42 | |
|
| 0 | 43 | | if (VERBOSE) |
| 0 | 44 | | Debug.Log($"[{nameof(GetType)}]: Requesting signed headers..."); |
| | 45 | |
|
| 0 | 46 | | string signResponse = await signRequest.RequestSignedHeaders(requestUrl, new Dictionary<string, string>(), c |
| | 47 | |
|
| 0 | 48 | | if (VERBOSE) |
| 0 | 49 | | Debug.Log($"[{nameof(GetType)}]: Signed Headers received:\n{signResponse}"); |
| | 50 | |
|
| | 51 | | // We have to wait for connection to be done to send the signed headers for authentication |
| 0 | 52 | | var connected = false; |
| | 53 | | void OnReady() |
| | 54 | | { |
| 0 | 55 | | connected = true; |
| 0 | 56 | | } |
| 0 | 57 | | webSocket.OnOpen += OnReady; |
| | 58 | |
|
| 0 | 59 | | if (VERBOSE) |
| 0 | 60 | | Debug.Log($"[{nameof(GetType)}]: Waiting for connection..."); |
| 0 | 61 | | webSocket.Connect(); |
| 0 | 62 | | await UniTask.WaitUntil(() => connected, cancellationToken: ct); |
| 0 | 63 | | if (VERBOSE) |
| 0 | 64 | | Debug.Log($"[{nameof(GetType)}]: Connected"); |
| 0 | 65 | | webSocket.OnOpen -= OnReady; |
| | 66 | |
|
| 0 | 67 | | if (VERBOSE) |
| 0 | 68 | | Debug.Log($"[{nameof(GetType)}]: Sending the signed headers"); |
| 0 | 69 | | webSocket.Send(signResponse); |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | private void HandleMessage(byte[] data) |
| | 73 | | { |
| 0 | 74 | | OnMessageEvent?.Invoke(data); |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | private void HandleError(string errorMsg) |
| | 78 | | { |
| 0 | 79 | | if(VERBOSE) |
| 0 | 80 | | Debug.Log($"[{nameof(GetType)}]: Error\n{errorMsg}"); |
| 0 | 81 | | OnErrorEvent?.Invoke(errorMsg); |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | private void HandleClose(WebSocketCloseCode closeCode) |
| | 85 | | { |
| 0 | 86 | | if (VERBOSE) |
| 0 | 87 | | Debug.Log($"[{nameof(GetType)}]: Closed WebSocket: {closeCode}"); |
| 0 | 88 | | OnCloseEvent?.Invoke(); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | private void HandleOpen() |
| | 92 | | { |
| 0 | 93 | | if (VERBOSE) |
| 0 | 94 | | Debug.Log($"[{nameof(GetType)}]: Open WebSocket:"); |
| 0 | 95 | | OnConnectEvent?.Invoke(); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | public void SendMessage(byte[] data) |
| | 99 | | { |
| 0 | 100 | | if (VERBOSE) |
| 0 | 101 | | Debug.Log($"[{nameof(GetType)}]: Sending bytes UTF8 decoded:\n{Encoding.UTF8.GetString(data)}"); |
| 0 | 102 | | webSocket.Send(data); |
| 0 | 103 | | } |
| | 104 | |
|
| | 105 | | public void SendMessage(string data) |
| | 106 | | { |
| 0 | 107 | | if (VERBOSE) |
| 0 | 108 | | Debug.Log($"[{nameof(GetType)}]: Sending data:\n{data}"); |
| 0 | 109 | | webSocket.Send(data); |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | public void Close() |
| | 113 | | { |
| 0 | 114 | | webSocket.Close(); |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | public void Dispose() |
| | 118 | | { |
| 0 | 119 | | OnCloseEvent = null; |
| 0 | 120 | | OnErrorEvent = null; |
| 0 | 121 | | OnMessageEvent = null; |
| 0 | 122 | | OnConnectEvent = null; |
| | 123 | |
|
| 0 | 124 | | webSocket.OnMessage -= this.HandleMessage; |
| 0 | 125 | | webSocket.OnError -= this.HandleError; |
| 0 | 126 | | webSocket.OnClose -= this.HandleClose; |
| 0 | 127 | | webSocket.OnOpen -= this.HandleOpen; |
| 0 | 128 | | } |
| | 129 | | } |
| | 130 | | } |