SignalR在Android上的实践

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GXH_APOLOGIZE/article/details/89396536

闲言

因为我们后台是 .net,所以要求使用SignalR来实现推送。因为网上资料也不多,也走了很多弯路。现在记录一下,希望可以帮到更多的人。

首先要确认后台用的是 asp.net 还是 asp.net core ,这两个的SignalR还是有区别的,我们Android用的库也不一样,我认为是不兼容的。

如果使用的是 asp.net,这里有个库可以使用。java-client 但是不再维护了。也可以试试这个,但是也不维护了。

如果后台使用的是 asp.net core ,那就接着看吧。

实践

这里我们说的后台是 asp.net core,我们该怎么办。首先看一下官网的一些信息。 另外,还有github上的地址

官网上给出了使用的步骤:

  • 添加依赖
implementation 'com.microsoft.signalr:signalr:1.0.0'
//下面这个是日志输出,上面这个库用到了这个
implementation group: 'org.slf4j', name: 'slf4j-android', version: '1.7.7'
  • 代码
 HubConnection hubConnection;
    public void signalr() {
        //创建HubConnection
        hubConnection = HubConnectionBuilder.create(url)
                .build();

        //客户端需要在hubConnection对象执行start()方法之前对服务端需要调用的方法进行注册
        //这里的message是服务端发给我们的信息
        hubConnection.on("SendAsync", (message) -> {
            Logger.d("gxh",message+"#haha");
        }, String.class);
        
        new HubConnectionTask().execute(hubConnection);
    }

    class HubConnectionTask extends AsyncTask<HubConnection, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(HubConnection... hubConnections) {
            HubConnection hubConnection = hubConnections[0];
            //开始连接并等待连接成功
            hubConnection.start().blockingAwait();
            Logger.d("gxh",hubConnection.getConnectionState().toString());
            return null;
        }
    }
    
    
//客户端调用服务端的方法 ,这里的方法名和参数要参考服务端写的,要一致。
hubConnection.send("Send", "hhhhhhhhh");

按照上面的步骤,你看下日志,如果连接成功了,那ok了。不用接着看了。
但是我还是失败了(可以连接到该库的测试地址上,但是连不上我们服务器的地址上)。

04-18 14:09:14.827 3655-3724/cn.gxh.view E/OkHttpWebSocketWrapper: WebSocket closed from an error: Expected 'Connection' header value 'Upgrade' but was 'null'.
04-18 14:09:14.828 3655-3724/cn.gxh.view I/WebSocketTransport: WebSocket connection stopping with code null and reason 'Expected 'Connection' header value 'Upgrade' but was 'null''.
04-18 14:09:14.828 3655-3724/cn.gxh.view E/c*.m*.s*.HubConnection: HubConnection disconnected with an error Expected 'Connection' header value 'Upgrade' but was 'null'.

--------- beginning of crash
04-18 14:09:14.831 3655-3724/cn.gxh.view E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: cn.gxh.view, PID: 3655
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.microsoft.signalr.HubConnection$ConnectionState.cancelOutstandingInvocations(java.lang.Exception)' on a null object reference
at com.microsoft.signalr.HubConnection.stopConnection(HubConnection.java:431)
at com.microsoft.signalr.HubConnection.lambda$start$6$HubConnection(HubConnection.java:301)
at com.microsoft.signalr.HubConnection$$Lambda$19.invoke(Unknown Source)
at com.microsoft.signalr.WebSocketTransport.onClose(WebSocketTransport.java:93)
at com.microsoft.signalr.WebSocketTransport.lambda$start$1$WebSocketTransport(WebSocketTransport.java:56)
at com.microsoft.signalr.WebSocketTransport$$Lambda$1.invoke(Unknown Source)
at com.microsoft.signalr.OkHttpWebSocketWrapper$SignalRWebSocketListener.onFailure(OkHttpWebSocketWrapper.java:98)
at okhttp3.internal.ws.RealWebSocket.failWebSocket(RealWebSocket.java:570)
at okhttp3.internal.ws.RealWebSocket$2.onResponse(RealWebSocket.java:197)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)

按照该库维护人员的建议:upgrade to the 3.0.0-preview3-19153-02 version.

implementation 'com.microsoft.signalr:signalr:3.0.0-preview3-19153-02'

因为这个版本 can set the transport to LongPolling

hubConnection = HubConnectionBuilder.create(url)
            .withTransport(TransportEnum.LONG_POLLING)
            .build();

其它无变化,至此,连接成功。

04-19 11:15:53.986 7924-7954/cn.gxh.view I/c*.m*.s*.HubConnection: HubConnection started.
04-19 11:15:53.988 7924-7951/cn.gxh.view E/gxh: CONNECTED
04-19 11:15:54.048 7924-7960/cn.gxh.view E/gxh: User tom: hi Baby#haha

最后,还有一点要说的,按照官网上写的:

The Java client is available in ASP.NET Core 2.2 and later.

这个库是在 asp.net core 2.2以及以后才可以用的,但是吧,该库的维护人员说2.1应该也可以用。所以,尽管试试吧。

文章同步发布在博客

猜你喜欢

转载自blog.csdn.net/GXH_APOLOGIZE/article/details/89396536
今日推荐