Signal-r android 在 iis express 上工作,但不在 windows server 2019 上的 iis 10 中工作
Signal-r android working on iis express but not in iis 10 on windows server 2019
我在 android 上使用了带有 asp.net 核心 2.2 的 signalR。当我在 android 客户端上调用集线器 hubConnection.send("Send", message);
时,它在 iis express (Visual studio 2019) 上运行良好。但是当我在 iis 10 (windows server 2019) 上部署 asp.net 核心时,出现以下错误消息:
Cannot send data if the connection is not in the 'Connected'.
连接状态为 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:signalr:1.0.0'。
Asp.net 核心 (2.2) SignalR 版本:1.1.0
谢谢
呼叫 hubConnection.start()
并不意味着您已立即接通。 hubConnection.start()
returns 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 枚举来检查连接状态,而不是使用此结构,因为它可能会在任何进一步更新时中断:
hubConnection.getConnectionState().toString().trim().equals("CONNECTED")
可以替换为:
hubConnection == HubConnectionState.CONNECTED
当您需要检查是否掉线时:
hubConnection == HubConnectionState.DISCONNECTED
正如@Jenea 所建议的,问题出在服务器上。所以我在 MS SignalR web site 上发现其中有一些已知的限制 仅支持 WebSockets 传输 。所以我去了服务器并安装了 IIS 的 Websocket 协议 功能,它工作了。
安装 Websocket 协议:
在 Windows 服务器和 Web 服务器上转到 添加角色和功能
(IIS) 转到 Web 服务器 -> 应用程序开发 并勾选 Websocket 协议。
我在 android 上使用了带有 asp.net 核心 2.2 的 signalR。当我在 android 客户端上调用集线器 hubConnection.send("Send", message);
时,它在 iis express (Visual studio 2019) 上运行良好。但是当我在 iis 10 (windows server 2019) 上部署 asp.net 核心时,出现以下错误消息:
Cannot send data if the connection is not in the 'Connected'.
连接状态为 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:signalr:1.0.0'。 Asp.net 核心 (2.2) SignalR 版本:1.1.0
谢谢
呼叫 hubConnection.start()
并不意味着您已立即接通。 hubConnection.start()
returns 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 枚举来检查连接状态,而不是使用此结构,因为它可能会在任何进一步更新时中断:
hubConnection.getConnectionState().toString().trim().equals("CONNECTED")
可以替换为:
hubConnection == HubConnectionState.CONNECTED
当您需要检查是否掉线时:
hubConnection == HubConnectionState.DISCONNECTED
正如@Jenea 所建议的,问题出在服务器上。所以我在 MS SignalR web site 上发现其中有一些已知的限制 仅支持 WebSockets 传输 。所以我去了服务器并安装了 IIS 的 Websocket 协议 功能,它工作了。
安装 Websocket 协议:
在 Windows 服务器和 Web 服务器上转到 添加角色和功能 (IIS) 转到 Web 服务器 -> 应用程序开发 并勾选 Websocket 协议。