< Summary

Class:DCLWebSocketService
Assembly:WebSocketCommunication
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/KernelCommunication/WebSocketCommunication/DCLWebSocketService.cs
Covered lines:0
Uncovered lines:35
Coverable lines:35
Total lines:103
Line coverage:0% (0 of 35)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SendMessageToWeb(...)0%12300%
SendBinary(...)0%2100%
ToString()0%2100%
OnMessage(...)0%20400%
OnError(...)0%6200%
OnClose(...)0%6200%
OnOpen()0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/KernelCommunication/WebSocketCommunication/DCLWebSocketService.cs

#LineLine coverage
 1using System.IO;
 2using System.Text;
 3using DCL;
 4using DCL.Interface;
 5using Newtonsoft.Json;
 6using UnityEngine;
 7using WebSocketSharp;
 8using WebSocketSharp.Server;
 9using System;
 10using ErrorEventArgs = WebSocketSharp.ErrorEventArgs;
 11
 12public class DCLWebSocketService : WebSocketBehavior
 13{
 14    public static bool VERBOSE = false;
 15
 16    public event Action OnCloseEvent;
 17
 18    public event Action<string> OnErrorEvent;
 19
 20    public event Action<byte[]> OnMessageEvent;
 21
 22    public event Action OnConnectEvent;
 23
 24    private void SendMessageToWeb(string type, string message)
 25    {
 26#if (UNITY_EDITOR || UNITY_STANDALONE)
 027        var x = new Message()
 28        {
 29            type = type,
 30            payload = message
 31        };
 32
 033        if (ConnectionState == WebSocketState.Open)
 34        {
 035            var serializeObject = JsonConvert.SerializeObject(x);
 36
 037            Send(serializeObject);
 38
 039            if (VERBOSE)
 40            {
 041                Debug.Log("SendMessageToWeb: " + type);
 42            }
 43        }
 44#endif
 045    }
 46
 47    public void SendBinary(byte[] data)
 48    {
 049        Send(data);
 050    }
 51
 52    public class Message
 53    {
 54        public string type;
 55        public string payload;
 56
 057        public override string ToString() { return string.Format("type = {0}... payload = {1}...", type, payload); }
 58    }
 59
 60    protected override void OnMessage(MessageEventArgs e)
 61    {
 062        base.OnMessage(e);
 63
 064        if (e.IsBinary)
 65        {
 066            OnMessageEvent?.Invoke(e.RawData);
 067            return;
 68        }
 69
 070        lock (WebSocketCommunication.queuedMessages)
 71        {
 072            Message finalMessage = JsonUtility.FromJson<Message>(e.Data);
 73
 074            WebSocketCommunication.queuedMessages.Enqueue(finalMessage);
 075            WebSocketCommunication.queuedMessagesDirty = true;
 076        }
 077    }
 78
 79    protected override void OnError(ErrorEventArgs e)
 80    {
 081        Debug.LogError(e.Message);
 082        base.OnError(e);
 083        OnErrorEvent?.Invoke(e.Message);
 084    }
 85
 86    protected override void OnClose(CloseEventArgs e)
 87    {
 088        base.OnClose(e);
 089        WebInterface.OnMessageFromEngine -= SendMessageToWeb;
 090        DataStore.i.wsCommunication.communicationEstablished.Set(false);
 091        OnCloseEvent?.Invoke();
 092    }
 93
 94    protected override void OnOpen()
 95    {
 096        Debug.Log("WebSocket Communication Established");
 097        base.OnOpen();
 98
 099        WebInterface.OnMessageFromEngine += SendMessageToWeb;
 0100        DataStore.i.wsCommunication.communicationEstablished.Set(true);
 0101        OnConnectEvent?.Invoke();
 0102    }
 103}