asp.net-core - Hub handshake failed with error 'WebSocket is not in the OPEN state' during start(). Stopping HubConnection
问题描述
When trying to do a user count I get the error web socket is not in open state, I have Saaskit as well for mutitenancy could that be the cause?
I have tried specifieng UseWebsockets in the startup.cs with no luck
{
private static int UserCount;
public override Task OnConnectedAsync()
{
UserCount++;
base.OnConnectedAsync();
this.Clients.All.SendAsync("updateUserCount", UserCount);
return Task.CompletedTask;
}
public override Task OnDisconnectedAsync(Exception exception)
{
UserCount--;
base.OnDisconnectedAsync(exception);
this.Clients.All.SendAsync("updateUserCount", UserCount);
return Task.CompletedTask;
}
}```
```<script>
document.addEventListener('DOMContentLoaded', function () {
}
function onConnectionError(error) {
if (error && error.message) {
console.error(error.message);
}
}
var connection = new
signalR.HubConnectionBuilder().withUrl('/adminHub')
.configureLogging(signalR.LogLevel.Debug).build();
connection.start()
.then(function () {
onConnected(connection);
})
.catch(function (error) {
console.error(error.message);
});
});
</script>```
Debug: Sending handsha`enter code here`ke request.
Debug: Hub handshake failed with error 'WebSocket is not in the OPEN state' during start(). Stopping HubConnection.
HttpConnection.stopConnection(undefined) called while in state Disconnecting.
Connection disconnected with error 'WebSocket is not in the OPEN state'.
HubConnection.connectionClosed(WebSocket is not in the OPEN state) called while in state Connecting.
解决方案
我知道这是迟到的回应,但这对我有用:
在连接开始后调用。
connection.start()
.then(function (data) {
console.log(data);
}).catch(function (err) {
console.log(err);
});
将此属性添加到 webSocket 对象:
Object.defineProperty(window.WebSocket, 'OPEN', { value: 1, });
推荐阅读
- laravel - Laravel (Echo, Sanctum, websockets) + Pusher + Nuxtjs SPA
- javascript - 如何制作一个使用javascript搜索网站中特定字段的搜索栏?
- r - 如何将函数中的局部变量默认传递给 R 中的嵌套变量?
- python - Python RSA消息加密“明文太长”?
- flutter - 在颤动中从父颜色生成自定义颜色阴影
- css - 保存为 PDF 时 CSS 设置强制 300PPI
- function - 一次在所有按钮上调用 Flutter 函数
- python - Scikit-learn:在 X0 的系数和截距之间混淆
- javascript - 路由器没有选择正确的功能
- flutter - 如何在颤动中改变bottomappbar缺口的位置?