[英]Signal-r android working on iis express but not in iis 10 on windows server 2019
I used signalR on android with asp.net core 2.2.我在 android 上使用 signalR 和 asp.net 核心 2.2。 It's working well on iis express (Visual studio 2019) when I call the hub
hubConnection.send("Send", message);
当我调用集线器
hubConnection.send("Send", message);
时,它在 iis express (Visual Studio 2019) 上运行良好on my android client.在我的 android 客户端上。 But when I deploy the asp.net core on iis 10 (windows server 2019), I have the following error message:
但是当我在 iis 10(windows server 2019)上部署 asp.net 内核时,出现以下错误消息:
Cannot send data if the connection is not in the 'Connected'.
and the connection state is DISCONNECTED
.并且连接 state 为
DISCONNECTED
。
My android client code:我的 android 客户端代码:
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'. Android SignalR 版本'com.microsoft.signalr:Z7D354B1EF1BEFD3CC8604D47E6'426.B. Asp.net core (2.2) SignalR version: 1.1.0
Asp.net 内核(2.2) SignalR 版本:1.1.0
Thanks谢谢
Calling hubConnection.start()
does not mean you have connected right away.调用
hubConnection.start()
并不意味着您已立即连接。 hubConnection.start()
returns io.reactivex.Completable to which you should subscribe creating CompletableObserver
. hubConnection.start()
返回io.reactivex.Completable您应该订阅它创建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:另一个提示:使用HubConnectionState枚举来检查连接 state 而不是使用这种可能会在任何进一步更新时中断的构造:
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.正如@Jenea 建议的那样,问题出在服务器上。 So I found out on MS SignalR web site that there are some known limitations among them Only the WebSockets transport is supported .
所以我在MS SignalR web 站点上发现其中存在一些已知限制,仅支持 WebSockets 传输。 So I went to the server and install the Websocket Protocol features of IIS and it works.
所以我去了服务器并安装了 IIS 的Websocket 协议功能,它可以工作。
To install the Websocket Protocol:要安装 Websocket 协议:
Go to Add roles and features on Windows server and on Web Server (IIS) go to Web Server -> Application Development and tick 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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.