WebSocket Error: Incorrect HTTP response. Status code 400, Bad Request

WebSocket Error: Incorrect HTTP response. Status code 400, Bad Request

我尝试为 ASP.NET Core 实现 SignalR,如此处记录:https://aspnetboilerplate.com/Pages/Documents/SignalR-AspNetCore-Integration

但我总是出错。浏览器控制台输出:

DEBUG: 
Starting connection using WebSockets transport
Information: Normalizing '/signalr-myChatHub' to 'http://localhost:62114/signalr-myChatHub'
SCRIPT12008: WebSocket Error: Incorrect HTTP response. Status code 400, Bad Request

我使用的是所有包的最新版本 3.6.2 和 Abp.AspNetCore.SignalR 3.6.2-preview6.

Web.Host/Startup.cs:

#elif FEATURE_SIGNALR_ASPNETCORE
    app.UseSignalR(routes =>
    {
        routes.MapHub<AbpCommonHub>("/signalr");
        routes.MapHub<MyChatHub>("/signalr-myChatHub"); // Prefix with '/signalr'
    });
#endif

复制粘贴MyChatHub.cs:

public class MyChatHub : Hub, ITransientDependency
{
    public IAbpSession AbpSession { get; set; }

    public ILogger Logger { get; set; }

    public MyChatHub()
    {
        AbpSession = NullAbpSession.Instance;
        Logger = NullLogger.Instance;
    }

    public async Task SendMessage(string message)
    {
        await Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, message));
    }

    public override async Task OnConnectedAsync()
    {
        await base.OnConnectedAsync();
        Logger.Debug("A client connected to MyChatHub: " + Context.ConnectionId);
    }

    public override async Task OnDisconnectedAsync(Exception exception)
    {
        await base.OnDisconnectedAsync(exception);
        Logger.Debug("A client disconnected from MyChatHub: " + Context.ConnectionId);
    }
}

或多或少的复制粘贴视图:

@using EMetall.Web.Startup

@section scripts
{
    <environment names="Development">
        <script src="~/lib/signalr-client/signalr.min.js"></script>
        <script src="~/lib/abp-web-resources/Abp/Framework/scripts/libs/abp.signalr-client.js"></script>
    </environment>

    <script type="text/javascript">
        $(function () {
            var chatHub = null;

            abp.signalr.startConnection('/signalr-myChatHub', function (connection) {
                chatHub = connection; // Save a reference to the hub

                connection.on('getMessage', function (message) { // Register for incoming messages
                    console.log('received message: ' + message);
                });
            }).then(function (connection) {
                console.log('Connected to myChatHub server!');
                abp.event.trigger('myChatHub.connected');
            });

            abp.event.on('myChatHub.connected', function() { // Register for connect event
                chatHub.invoke('sendMessage', "Hi everybody, I'm connected to the chat!"); // Send a message to the server
            });
        });
    </script>
}

您需要使用兼容版本:

从 4.2 版开始,它开箱即用。

您只需 download 它,然后创建 chathup,注册路由并在视图中调用它,它就可以工作了。伟大的。

聊天、路线和视图的示例见: https://aspnetboilerplate.com/Pages/Documents/SignalR-AspNetCore-Integration