在Android中设置信号R:崩溃/挂起问题

在Android中设置信号R:崩溃/挂起问题,android,signalr,azure-mobile-services,signalr-hub,signalr.client,Android,Signalr,Azure Mobile Services,Signalr Hub,Signalr.client,我随后为我的Android应用程序设置了一个.NET后端来实现Signal R。我设置了一个Signal R自托管后端 这是我在控制台项目中的后端代码: namespace SignalRSelfHost { class Program { static void Main(string[] args) { // This will *ONLY* bind to localhost, if you want to bind

我随后为我的Android应用程序设置了一个.NET后端来实现Signal R。我设置了一个Signal R自托管后端

这是我在控制台项目中的后端代码:

namespace SignalRSelfHost
{
    class Program
    {
        static void Main(string[] args)
        {
            // This will *ONLY* bind to localhost, if you want to bind to all addresses
            // use http://*:8080 to bind to all addresses. 
            // See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx 
            // for more information.
            string url = "http://localhost:8080";
            using (WebApp.Start(url))
            {
                Console.WriteLine("Server running on {0}", url);
                Console.ReadLine();
            }
        }
    }
    class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCors(CorsOptions.AllowAll);
            app.MapSignalR();
        }
    }
    public class MessageHub : Hub
    {
        public static event Action<string, string> MessageReceived = delegate { };

        public void SendMessage(string name, string message)
        {
            MessageReceived(name, message);
        }

    }

    public class CustomType
    {
        public string Name;
        public int Id;
    }
}
    Handler handler;
    TextView statusField;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        handler = new Handler();
        statusField = (TextView) findViewById(R.id.statusField);

        Platform.loadPlatformComponent(new AndroidPlatformComponent());
        // Change to the IP address and matching port of your SignalR server.
        String host = "http://192.168.1.5:8080/";
        HubConnection connection = new HubConnection( host );
        HubProxy hub = connection.createHubProxy( "MessageHub" );
        ClientTransport transport = new ServerSentEventsTransport(connection.getLogger());

        SignalRFuture<Void> awaitConnection = connection.start(transport);
        try {
            awaitConnection.get();
        }
        catch (InterruptedException e) {
            Log.d("CHECK", e.toString());
            e.printStackTrace();
        } catch (ExecutionException e) {
            Log.d("CHECK", e.toString());
            e.printStackTrace();
        }

        hub.subscribe(this);

        try {
            hub.invoke( "SendMessage", "Client", "Hello world!" ).get();
            hub.invoke( "SendCustomType",
                    new CustomType() {{ Name = "Universe"; Id = 42; }} ).get();
        } catch (InterruptedException e) {
            // Handle ...
        } catch (ExecutionException e) {
            // Handle ...
        }

    }

    public void UpdateStatus(String status) {
        final String fStatus = status;
        handler.post(new Runnable() {
            @Override
            public void run() {
                statusField.setText(fStatus);
            }
        });
    }

    public class CustomType
    {
        public String Name;
        public int Id;
    }

几天前我也遇到过类似的问题,这个Github问题有助于:

基本上,我所做的是在signalr客户端sdk项目中修改WebsocketTransport.java。替换:

uri = new URI(url);

在源代码第86行附近

我现在可以连接并发送消息和接收原始数据,但无法订阅事件


希望这能帮助您解决问题。

显示完整的日志。请稍候@MurtazaKhursheedHussain@MurtazaKhursheedHussain编辑。抱歉,我无法正确格式化代码。您的Android设备是否与服务器连接到同一网络?192.168.1.5是一个无法通过Internet访问的本地网络地址。我正在从笔记本电脑托管服务器,并连接到手机连接的同一wifi。我是不是犯了个愚蠢的错误D@RicardoLageI明天肯定会试试这个。等不及了!非常感谢。我明天再打给你。真遗憾,那里没有什么好的教程。很难正确设置。你能把JAR文件寄给我吗?我想我的已经很老了。你能把它们寄给我吗?从Github上的SignalR项目打包JAR文件有这么大的困难。对不起,没有互联网。你还需要罐子吗?给你。请注意,我包括我在谷歌找到的罐子(2014年制造)和我制造的罐子。还请注意,signalr client sdk android-*是项目要引用的.aar文件(android二进制库)。
uri = new URI(url);
uri = new URI(url.replace("http://", "ws://"));