void Start()
    {
        uiText = GameObject.Find("Text").GetComponent <Text>();
        DisplayMessage(statusText);

        var handlers = new List <string>()
        {
            HANDLER_A, HANDLER_B
        };
        var srLib = new SignalRLib(signalRHubURL, handlers, true);

        srLib.ConnectionStarted += (object sender, ConnectionEventArgs e) =>
        {
            Debug.Log(e.ConnectionId);
            DisplayMessage(connectedText);

            var json1 = new JsonPayload
            {
                message = messageToSendA
            };
            srLib.SendToHub(hubMethodA, JsonUtility.ToJson(json1));

            var json2 = new JsonPayload
            {
                message = messageToSendB
            };
            srLib.SendToHub(hubMethodB, JsonUtility.ToJson(json2));
        };

        srLib.HandlerInvoked += (object sender, HandlerEventArgs e) =>
        {
            var json = JsonUtility.FromJson <JsonPayload>(e.Payload);

            switch (e.HandlerName)
            {
            case HANDLER_A:
                DisplayMessage($"{HANDLER_A}: {json.message}");
                break;

            case HANDLER_B:
                DisplayMessage($"{HANDLER_B}: {json.message}");
                break;

            default:
                Debug.Log($"Handler: '{e.HandlerName}' not defined");
                break;
            }
        };
    }
    void Start()
    {
        uiText = GameObject.Find("Text").GetComponent <Text>();
        DisplayMessage(StatusText);

        srLib = new SignalRLib();
        srLib.Init(SignalRHubURL, HubListenerName);

        srLib.ConnectionStarted += (object sender, MessageEventArgs e) =>
        {
            DisplayMessage(e.Message);
            srLib.SendMessage(HubMethodName, MessageToSend);
        };

        srLib.MessageReceived += (object sender, MessageEventArgs e) =>
        {
            DisplayMessage(e.Message);
        };
    }
        /// <summary>
        /// Try to start the SignalR connection with the server.
        /// </summary>
        private void StartSignalR()
        {
            if (string.IsNullOrEmpty(SettingsManager.Instance.Settings.Api.EventHubEndpoint))
            {
                Debug.LogWarning("SignalR is not enabled, no endpoint is set. Real-time data is unavailable.");

                return;
            }

            _srLib = new SignalRLib();
            _srLib.Init(_hubEndpoint, HubListenerName);

            _srLib.Error += (sender, e) =>
            {
                _alertText.text =
                    $"Error retrieving live data from server! Server seems to be down.";
                AlertCanvas.SetActive(true);
#if UNITY_EDITOR
                Debug.LogError($"SignalR gives an error: {e.Message}");
#endif
            };
            _srLib.ConnectionStarted += (sender, e) => { Debug.Log("Connection with server established."); };


            _srLib.MessageReceived += (sender, e) =>
            {
                // Disable alerting text if server went back up
                if (_alertText.gameObject.activeSelf)
                {
                    _alertText.gameObject.SetActive(false);
                }

                UpdateEventModel updateEventModel = JsonParser.ParseUpdateEvent(e.Message);
                if (Application.isEditor && updateEventModel.UpdatedLayerValues == null)
                {
                    Debug.Log(e.Message);
                }
                ApiUpdateEvent?.Invoke(updateEventModel);
            };
        }
    void Start()
    {
        player   = GameObject.Find("Postmanaut").GetComponent <PlayerControl>();
        endpoint = GameObject.Find("EndpointBar").GetComponent <EndpointBar>();
        actions  = GameObject.Find("ActionsLog").GetComponent <ActionsLog>();

        srLib = new SignalRLib();
        srLib.Init(signalRHubUrl, hubListenerName);

        srLib.ConnectionStarted += (object sender, ConnectionEventArgs e) =>
        {
            string hubName     = signalRHubUrl.Split('/').Last();
            string endpointUrl = signalRHubUrl.Replace(hubName, e.ConnectionId);

            Debug.Log(endpointUrl);
            endpoint.UpdateURL(endpointUrl);
        };

        srLib.MessageReceived += (object sender, MessageEventArgs e) =>
        {
            ActionCommand action = new ActionCommand();
            action = JsonUtility.FromJson <ActionCommand>(e.Message);

            if (!string.IsNullOrEmpty(action.direction))
            {
                player.SetDirection(action.direction);
            }

            if (!string.IsNullOrEmpty(action.perform))
            {
                player.PerformAction(action.perform);
            }

            actions.AddEntry(action);
        };
    }
 public SignalRLib()
 {
     instance = this;
 }
    /// <summary>
    /// Set up the connection and callbacks.
    /// </summary>
    void Awake()
    {
#if DEBUG
        signalRHubURL = "http://localhost:50403/signalr";

        //signalRHubURL="https://api.test.scrawlbrawl.tv/signalr";
#else
        signalRHubURL = "https://api.scrawlbrawl.tv/signalr";
#endif
#if UNITY_WEBGL
        if (Application.absoluteURL.Contains("localhost") || Application.absoluteURL == "")
        {
            signalRHubURL = "http://localhost:50403/signalr";
        }
        else if (Application.absoluteURL.Contains("test."))
        {
            signalRHubURL = "https://api.test.scrawlbrawl.tv/signalr";
        }
        else
        {
            signalRHubURL = "https://api.scrawlbrawl.tv/signalr";
        }
#endif

        Debug.Log("URL:" + Application.absoluteURL);
        srLib = new SignalRLib(signalRHubURL, handlers, true);

        Application.runInBackground = true;
        QualitySettings.vSyncCount  = 0; // VSync must be disabled

#if UNITY_WEBGL
        Application.targetFrameRate = -1;  // https://docs.unity3d.com/ScriptReference/Application-targetFrameRate.html
#else
        Application.targetFrameRate = 60;
#endif

        srLib.ConnectionStarted += (object sender, ConnectionEventArgs e) =>
        {
            Debug.Log(e.ConnectionId);
            Connected = true;  // just a flag we are using to know we connected, does not ensure we have not been disconnected
            if (!string.IsNullOrEmpty(this.LobbyId))
            {
                ConnectToLobby(this.LobbyId);
            }

/* no longer needed with embedded viewer
 *          Uri uri = new Uri(Application.absoluteURL);
 *          string[] lobby = uri.Query.Split(new string[]{"lobby="},StringSplitOptions.None);
 *          if (lobby.Length==2 && lobby[1].Length > 0)
 *          {
 *              ConnectToLobby(lobby[1].Split('&')[0].Truncate(10));
 *          }
 */
        };

        srLib.HandlerInvoked += (object sender, HandlerEventArgs e) =>
        {
            Debug.Log("handler invoked");

            switch (e.HandlerName)
            {
            case "ConfigureMetadata":
                ConfigurationMeta = JsonConvert.DeserializeObject <ConfigurationMetadata>(e.Payload);
                ConfigDirty       = true;
                break;

            case "UpdateState":
                try {
                    CurrentView = ParseJObjects(JsonConvert.DeserializeObject <UnityView>(e.Payload));
                }
                catch (Exception err)
                {
                    Debug.Log(err.Message);
                }
                Dirty = true;
                break;

            case "LobbyClose":
                LobbyClosed = true;
                Dirty       = true;
                break;

            default:
                Debug.Log($"Handler: '{e.HandlerName}' not defined");
                break;
            }
        };

        // plr ConnectToHub();
    }