I am a beginner, I have connected SignalR to my application, it works fine, it sends / receives data. But everything works only in the editor, after building(Android) the first scene appears and nothing more, the buttons do not work, it does not show in the logs that it connected to the server. API Compatibility Level .NET Standard 2.0. Please help I can not find a solution on the Internet. If I remove the SignalR script, then everything is displayed as it should.

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using Newtonsoft.Json;
using SignalR.Client._20.Hubs;
using UnityEngine;
using UnityEngine.UI;
using Users;

    public class SignalRClient : MonoBehaviour {
        private string URL = "[MY SERVER]";
        private HubConnection _hubConnection = null;
        private IHubProxy _hubProxy;
        private Subscription _subscription;
        private string _response;
        public InputField Login;
        public InputField Pass;
        public Text testTExt;
        public User user;

    IEnumerator Start () {
        UnityEngine.Debug.Log ("Start()");
        yield return new WaitForSeconds (1);
        UnityEngine.Debug.Log ("Start() 1 second.");
        StartSignalR ();
    }

    void StartSignalR () {
        UnityEngine.Debug.Log ("StartSignalR");
        if (_hubConnection == null) {
            _hubConnection = new HubConnection (URL);
            UnityEngine.Debug.Log ("URL: " + URL);
            _hubConnection.Error += hubConnection_Error;

            _hubProxy = _hubConnection.CreateProxy ("ChatHub");
            Subscription subscription = _hubProxy.Subscribe ("broadcastMessage");
            subscription.Data += Subscription_Data;

            UnityEngine.Debug.Log ("HubConnection Start ");
            _hubConnection.Start();
        } else {
            UnityEngine.Debug.Log ("SignalR already connected... ");
        }
    }       


    private void Subscription_Data (object[] obj) {
        User user = 
            JsonConvert.DeserializeObject<User>(obj[0].ToString());
        UnityEngine.Debug.Log("Object: ID->" + user.Id + " Login->" + user.login + "Password->" + user.password);
        _response = user.Id + ": " + user.login + " ";
    }

    private void hubConnection_Error (System.Exception obj) {
        UnityEngine.Debug.Log ("Hub Error - " + obj.Message +
            System.Environment.NewLine + obj.Data +
            System.Environment.NewLine + obj.StackTrace +
            System.Environment.NewLine + obj.TargetSite);
    }

    private void hubConnection_Closed () {
        UnityEngine.Debug.Log ("Hub Connection Closed ");
    }

    public void SendMessage () {
        User user = new User(){
            Id = 0,
            login = Login.text,
            password = Pass.text
        };
        _hubProxy.Invoke ("send", user);
    }

    private void OnApplicationQuit () {
        UnityEngine.Debug.Log ("Application Exit " + Time.time + " seconds ");
        _hubConnection.Error -= hubConnection_Error;
        _hubConnection.Stop();
    }


    void Update () {
        testTExt.text = _response;
    }
}

0 Answers