balderdash
Because our backend is.net, we need to use SignalR for push.Because there is not much information on the Internet, we also took a lot of detours.Now take a note and hope you can help more people.
First, confirm that the background is asp.net still asp.net core, the two ignalRs are different, and we use different libraries for Android, which I don't think is compatible.
If you are using asp.net Here is a library to use. java-client But it's no longer maintained. You can also try this But it won't be maintained.
If you are using the asp.net core, let's go on.
practice
Background here is what we're talking about asp.net core, what do we do?Take a look first Official Web Some information.In addition, there are Address on github
Steps for use are given on the official website:
- Add Dependency
implementation 'com.microsoft.signalr:signalr:1.0.0' //Here's the log output, which is used in this library above implementation group: 'org.slf4j', name: 'slf4j-android', version: '1.7.7'
- Code
HubConnection hubConnection; public void signalr() { //Create HubConnection hubConnection = HubConnectionBuilder.create(url) .build(); //Clients need to register the methods they need to call before the hubConnection object can execute the start() method //Here message is the message sent to us by the server 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]; //Start the connection and wait for it to succeed hubConnection.start().blockingAwait(); Logger.d("gxh",hubConnection.getConnectionState().toString()); return null; } } //Clients invoke methods on the server side, where method names and parameters are written with reference to the service side and are consistent. hubConnection.send("Send", "hhhhhhhhh");
Follow the steps above and look at the log. If the connection is successful, okay.There's no need to follow up.
But I still failed (I could connect to the test address of the library, but not to the address of our server).
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)
As recommended by the library maintainer: upgrade to the 3.0.0-preview3-19153-02 version.
implementation 'com.microsoft.signalr:signalr:3.0.0-preview3-19153-02'
Because this version can set the transport to LongPolling
hubConnection = HubConnectionBuilder.create(url) .withTransport(TransportEnum.LONG_POLLING) .build();
Other unchanged, so far, the connection is successful.
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
Finally, there is one more thing to say, written on the official website:
The Java client is available in ASP.NET Core 2.2 and later.
This library is located in asp.net core 2.2 won't be available until later, but the library's maintainers say 2.1 should be available as well.So, try it.
Articles are published synchronously at Blog