< Summary

Class:RPC.Transports.AuthedWebSocketClientTransport
Assembly:RPCHelpers
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/RPC/AuthedWebSocketClientTransport.cs
Covered lines:0
Uncovered lines:61
Coverable lines:61
Total lines:130
Line coverage:0% (0 of 61)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:10
Method coverage:0% (0 of 10)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AuthedWebSocketClientTransport(...)0%2100%
Connect()0%1101000%
HandleMessage(...)0%6200%
HandleError(...)0%12300%
HandleClose(...)0%12300%
HandleOpen()0%12300%
SendMessage(...)0%6200%
SendMessage(...)0%6200%
Close()0%2100%
Dispose()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/RPC/AuthedWebSocketClientTransport.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using HybridWebSocket;
 3using rpc_csharp.transport;
 4using System;
 5using System.Collections.Generic;
 6using System.Text;
 7using System.Threading;
 8using UnityEngine;
 9
 10namespace 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
 030        public AuthedWebSocketClientTransport(IRPCSignRequest signRequest, string url)
 31        {
 032            webSocket = WebSocketFactory.CreateInstance(url);
 033            this.signRequest = signRequest;
 034        }
 35
 36        public async UniTask Connect(string requestUrl, CancellationToken ct = default)
 37        {
 038            webSocket.OnMessage += this.HandleMessage;
 039            webSocket.OnError += this.HandleError;
 040            webSocket.OnClose += this.HandleClose;
 041            webSocket.OnOpen += this.HandleOpen;
 42
 043            if (VERBOSE)
 044                Debug.Log($"[{nameof(GetType)}]: Requesting signed headers...");
 45
 046            string signResponse = await signRequest.RequestSignedHeaders(requestUrl, new Dictionary<string, string>(), c
 47
 048            if (VERBOSE)
 049                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
 052            var connected = false;
 53            void OnReady()
 54            {
 055                connected = true;
 056            }
 057            webSocket.OnOpen += OnReady;
 58
 059            if (VERBOSE)
 060                Debug.Log($"[{nameof(GetType)}]: Waiting for connection...");
 061            webSocket.Connect();
 062            await UniTask.WaitUntil(() => connected, cancellationToken: ct);
 063            if (VERBOSE)
 064                Debug.Log($"[{nameof(GetType)}]: Connected");
 065            webSocket.OnOpen -= OnReady;
 66
 067            if (VERBOSE)
 068                Debug.Log($"[{nameof(GetType)}]: Sending the signed headers");
 069            webSocket.Send(signResponse);
 070        }
 71
 72        private void HandleMessage(byte[] data)
 73        {
 074            OnMessageEvent?.Invoke(data);
 075        }
 76
 77        private void HandleError(string errorMsg)
 78        {
 079            if(VERBOSE)
 080                Debug.Log($"[{nameof(GetType)}]: Error\n{errorMsg}");
 081            OnErrorEvent?.Invoke(errorMsg);
 082        }
 83
 84        private void HandleClose(WebSocketCloseCode closeCode)
 85        {
 086            if (VERBOSE)
 087                Debug.Log($"[{nameof(GetType)}]: Closed WebSocket: {closeCode}");
 088            OnCloseEvent?.Invoke();
 089        }
 90
 91        private void HandleOpen()
 92        {
 093            if (VERBOSE)
 094                Debug.Log($"[{nameof(GetType)}]: Open WebSocket:");
 095            OnConnectEvent?.Invoke();
 096        }
 97
 98        public void SendMessage(byte[] data)
 99        {
 0100            if (VERBOSE)
 0101                Debug.Log($"[{nameof(GetType)}]: Sending bytes UTF8 decoded:\n{Encoding.UTF8.GetString(data)}");
 0102            webSocket.Send(data);
 0103        }
 104
 105        public void SendMessage(string data)
 106        {
 0107            if (VERBOSE)
 0108                Debug.Log($"[{nameof(GetType)}]: Sending data:\n{data}");
 0109            webSocket.Send(data);
 0110        }
 111
 112        public void Close()
 113        {
 0114            webSocket.Close();
 0115        }
 116
 117        public void Dispose()
 118        {
 0119            OnCloseEvent = null;
 0120            OnErrorEvent = null;
 0121            OnMessageEvent = null;
 0122            OnConnectEvent = null;
 123
 0124            webSocket.OnMessage -= this.HandleMessage;
 0125            webSocket.OnError -= this.HandleError;
 0126            webSocket.OnClose -= this.HandleClose;
 0127            webSocket.OnOpen -= this.HandleOpen;
 0128        }
 129    }
 130}