Unity:SignalR连接在编辑器中有效,但在设备(apk)中无效

时间:2019-04-21 22:23:59

标签: c# unity3d signalr

我是一个初学者,我已经将SignalR连接到我的应用程序,它工作正常,它发送/接收数据。但是,所有内容仅在编辑器中有效,在构建(Android)后,将显示第一个场景,仅此而已,按钮不起作用,并且不会在其连接到服务器的日志中显示。 API兼容级别.NET Standard 2.0。请帮助我在Internet上找不到解决方案。如果我删除SignalR脚本,那么一切都会按原样显示。

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 个答案:

没有答案