< 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:42
Coverable lines:42
Total lines:119
Line coverage:0% (0 of 42)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DCLWebSocketService()0%2100%
OnApplicationWantsToQuit()0%2100%
SendMessageToWeb(...)0%12300%
SendBinary(...)0%2100%
ToString()0%2100%
OnMessage(...)0%30500%
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 bool wantsToQuit;
 25
 026    public DCLWebSocketService()
 27    {
 028        Application.wantsToQuit += OnApplicationWantsToQuit;
 029    }
 30
 31    private bool OnApplicationWantsToQuit()
 32    {
 033        wantsToQuit = true;
 034        return true;
 35    }
 36
 37    private void SendMessageToWeb(string type, string message)
 38    {
 39#if (UNITY_EDITOR || UNITY_STANDALONE)
 040        var x = new Message()
 41        {
 42            type = type,
 43            payload = message
 44        };
 45
 046        if (ConnectionState == WebSocketState.Open)
 47        {
 048            var serializeObject = JsonConvert.SerializeObject(x);
 49
 050            Send(serializeObject);
 51
 052            if (VERBOSE)
 53            {
 054                Debug.Log("SendMessageToWeb: " + type);
 55            }
 56        }
 57#endif
 058    }
 59
 60    public void SendBinary(byte[] data)
 61    {
 062        Send(data);
 063    }
 64
 65    public class Message
 66    {
 67        public string type;
 68        public string payload;
 69
 070        public override string ToString() { return string.Format("type = {0}... payload = {1}...", type, payload); }
 71    }
 72
 73    protected override void OnMessage(MessageEventArgs e)
 74    {
 075        base.OnMessage(e);
 76
 077        if (e.IsBinary)
 78        {
 079            OnMessageEvent?.Invoke(e.RawData);
 080            return;
 81        }
 82
 083        lock (WebSocketCommunication.queuedMessages)
 84        {
 085            if (wantsToQuit)
 086                return;
 87
 088            Message finalMessage = JsonUtility.FromJson<Message>(e.Data);
 89
 090            WebSocketCommunication.queuedMessages.Enqueue(finalMessage);
 091            WebSocketCommunication.queuedMessagesDirty = true;
 092        }
 093    }
 94
 95    protected override void OnError(ErrorEventArgs e)
 96    {
 097        Debug.LogError(e.Message);
 098        base.OnError(e);
 099        OnErrorEvent?.Invoke(e.Message);
 0100    }
 101
 102    protected override void OnClose(CloseEventArgs e)
 103    {
 0104        base.OnClose(e);
 0105        WebInterface.OnMessageFromEngine -= SendMessageToWeb;
 0106        DataStore.i.wsCommunication.communicationEstablished.Set(false);
 0107        OnCloseEvent?.Invoke();
 0108    }
 109
 110    protected override void OnOpen()
 111    {
 0112        Debug.Log("WebSocket Communication Established");
 0113        base.OnOpen();
 114
 0115        WebInterface.OnMessageFromEngine += SendMessageToWeb;
 0116        DataStore.i.wsCommunication.communicationEstablished.Set(true);
 0117        OnConnectEvent?.Invoke();
 0118    }
 119}