在Android中设置Signal R:崩溃/挂断 [英] Setting up Signal R in Android: Crash/Hung Issue

查看:350
本文介绍了在Android中设置Signal R:崩溃/挂断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了此教程为我的Android应用设置一个.NET后端以实现SignalR.我设置了一个SignalR自托管后端.

I followed THIS tutorial to set up a .NET Backend for my Android app to implement Signal R. I set up a SignalR Self-Hosted backend.

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

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;
    }
}

我的Android代码:

    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;
    }

但是,我最终看到一个挂起的屏幕,它什么也不显示,并且该应用程序停止响应. android记录器显示以下错误:

However, I end up with a screen that hangs and it displays nothing and the app stops responding. The android logger displays the following error:

java.util.concurrent.ExecutionException:java.net.ConnectException: 15000ms后无法连接到localhost/127.0.0.1(端口80): isConnected失败:ECONNREFUSED(连接被拒绝)

java.util.concurrent.ExecutionException: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80) after 15000ms: isConnected failed: ECONNREFUSED (Connection refused)

我的代码是否正确,我是否已正确遵循指南?这是一个非常简单的指南,但是出现了这个问题.有人可以帮忙吗?

Is my code correct and have I followed the guide properly? It's a very straight forward guide but this issue comes up. Can someone help out?

编辑:

是一个非常相似的问题,实际上它描述了相同的问题.但是,这对我没有任何好处.如果您注意到我的android代码,我已经进行了该问题中提出的那些更改.仍然没有解决该问题.

This is a very similar Question, in fact it describes the same issue. However it didn't do me any good. If you notice my android code, I've already made those changes proposed in that question. Still didn't rectify the issue.

Stacktrace

 java.util.concurrent.ExecutionException: java.net.SocketTimeoutException: failed to connect to / 192.168.1.5(port 8080) after 15000ms
 at microsoft.aspnet.signalr.client.SignalRFuture.get(SignalRFuture.java: 112)
 at microsoft.aspnet.signalr.client.SignalRFuture.get(SignalRFuture.java: 102)
 at com.example.dinuka.signalrtest.Main2Activity.onCreate(Main2Activity.java: 40)
 at android.app.Activity.performCreate(Activity.java: 6289)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1119)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2655)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2767)
 at android.app.ActivityThread.access$900(ActivityThread.java: 177)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1449)
 at android.os.Handler.dispatchMessage(Handler.java: 102)
 at android.os.Looper.loop(Looper.java: 145)
 at android.app.ActivityThread.main(ActivityThread.java: 5951)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java: 372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1400)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 1195)
 Caused by: java.net.SocketTimeoutException: failed to connect to / 192.168.1.5(port 8080) after 15000ms
 at libcore.io.IoBridge.connectErrno(IoBridge.java: 169)
 at libcore.io.IoBridge.connect(IoBridge.java: 122)
 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java: 183)
 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java: 456)
 at java.net.Socket.connect(Socket.java: 882)
 at com.android.okhttp.internal.Platform.connectSocket(Platform.java: 139)
 at com.android.okhttp.Connection.connect(Connection.java: 1194)
 at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java: 392)
 at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java: 295)
 at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java: 373)
 at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java: 323)
 at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java: 491)
 at microsoft.aspnet.signalr.client.http.java.NetworkRunnable.run(NetworkRunnable.java: 72)
 at java.lang.Thread.run(Thread.java: 818)

推荐答案

几天前,我遇到了类似的问题,这个Github问题帮助了: https://github.com/SignalR/java-client/issues/63

I had similar issue a few days ago and this Github issue helped: https://github.com/SignalR/java-client/issues/63

基本上,我所做的是修改signalr-client-sdk项目中的WebsocketTransport.java.替换:

Basically what I did was modify the WebsocketTransport.java in the signalr-client-sdk project. Replace:

uri = new URI(url);

使用

uri = new URI(url.replace("http://", "ws://"));

在源代码的第86行附近.

around line 86 in the source code.

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

I can now connect and send message and recieve raw data but cannot subscribe to events...

希望这可以帮助您解决问题.

Hope this helps you get passed your problem.

这篇关于在Android中设置Signal R:崩溃/挂断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆