[英]Signal-r android working on iis express but not in iis 10 on windows server 2019
我在 android 上使用 signalR 和 asp.net 核心 2.2。 当我调用集线器hubConnection.send("Send", message);
时,它在 iis express (Visual Studio 2019) 上运行良好在我的 android 客户端上。 但是当我在 iis 10(windows server 2019)上部署 asp.net 内核时,出现以下错误消息:
Cannot send data if the connection is not in the 'Connected'.
并且连接 state 为DISCONNECTED
。
我的 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 版本'com.microsoft.signalr:Z7D354B1EF1BEFD3CC8604D47E6'426.B. Asp.net 内核(2.2) SignalR 版本:1.1.0
谢谢
调用hubConnection.start()
并不意味着您已立即连接。 hubConnection.start()
返回io.reactivex.Completable您应该订阅它创建CompletableObserver
。 当您连接或连接失败时,此观察者将收到响应。 然后,只有在成功连接后,您才能发送消息。
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());
}
});
另一个提示:使用HubConnectionState枚举来检查连接 state 而不是使用这种可能会在任何进一步更新时中断的构造:
hubConnection.getConnectionState().toString().trim().equals("CONNECTED")
它可以替换为:
hubConnection == HubConnectionState.CONNECTED
当需要检查是否断开连接时:
hubConnection == HubConnectionState.DISCONNECTED
正如@Jenea 建议的那样,问题出在服务器上。 所以我在MS SignalR web 站点上发现其中存在一些已知限制,仅支持 WebSockets 传输。 所以我去了服务器并安装了 IIS 的Websocket 协议功能,它可以工作。
要安装 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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.