首页 > 解决方案 > 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.

标签: asp.net-coreasp.net-core-signalr

解决方案


我知道这是迟到的回应,但这对我有用:
在连接开始后调用。

connection.start()
        .then(function (data) {
            console.log(data);

        }).catch(function (err) {
            console.log(err);
        }); 
    

将此属性添加到 webSocket 对象:

Object.defineProperty(window.WebSocket, 'OPEN', { value: 1, });

已知问题


推荐阅读