Signal-r android working on iis express but not in iis 10 on windows server 2019

Viewed 736

I used signalR on android with asp.net core 2.2. It's working well on iis express (Visual studio 2019) when I call the hub hubConnection.send("Send", message); on my android client. But when I deploy the asp.net core on iis 10 (windows server 2019), I have the following error message :

Cannot send data if the connection is not in the 'Connected'. and the connection state is DISCONNECTED.

My android client code :

HubConnection hubConnection = HubConnectionBuilder.create("server IP @").build();
try {
    hubConnection.start();
    //I added the following line to restart the hubConnection when disconnected for whatever reason but in vain ;-(
    if (!hubConnection.getConnectionState().toString().trim().equals("CONNECTED"))
        hubConnection.start();            
    hubConnection.send("Send", message);
} catch (Exception e) {
    Log.e("Erreur hubconnection ", e.getMessage());            
}

Android SignalR version 'com.microsoft.signalr:signalr:1.0.0'. Asp.net core (2.2) SignalR version : 1.1.0

Thanks

2 Answers

Calling hubConnection.start() does not mean you have connected right away. hubConnection.start() returns io.reactivex.Completable to which you should subscribe creating CompletableObserver. This observer will receive a response when you either connected or failed to connect. Then and only after a successful connection, you will be able to send a message.

HubConnection hubConnection = HubConnectionBuilder.create("127.0.0.1").build();
Completable connecting = hubConnection.start();
connecting.subscribe(new CompletableObserver() {
        @Override
        public void onSubscribe(Disposable d) {
            // Called once by the Completable to set a Disposable 
            // on this instance which then can be used to cancel 
            // the subscription at any time.
            Log.d(TAG, " onSubscribe : " + d.isDisposed());
        }

        @Override
        public void onComplete() {
            // Connected     
            hubConnection.send("Send", message);
        }

        @Override
        public void onError(Throwable e) {
            Log.d(TAG, " onError : " + e.getMessage());
        }
    });

One more hint: use HubConnectionState enum to check connection state instead of using this construction which can break on any further update:

hubConnection.getConnectionState().toString().trim().equals("CONNECTED")

It can be replaced with:

hubConnection == HubConnectionState.CONNECTED

When you need to check whether you are disconnected:

hubConnection == HubConnectionState.DISCONNECTED

As @Jenea suggested, The issue is on the server. So I found out on MS SignalR web site that there are some known limitations among them Only the WebSockets transport is supported. So I went to the server and install the Websocket Protocol features of IIS and it works.

To install the Websocket Protocol :

Go to Add roles and features on Windows server and on Web Server (IIS) go to Web Server -> Application Development and tick Websocket Protocol.

Related