WebSocket错误:HTTP响应不正确。状态代码400,错误请求

时间:2018-05-28 14:27:13

标签: c# asp.net-core integration aspnetboilerplate asp.net-core-signalr

我尝试为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>
}

2 个答案:

答案 0 :(得分:0)

您需要使用兼容版本:

答案 1 :(得分:0)

从4.2版开始,它可以直接使用。

您只需要download,然后创建chathup,注册路线并在视图中调用它即可。大。

例如,请参阅Chathup,路线和视图: https://aspnetboilerplate.com/Pages/Documents/SignalR-AspNetCore-Integration