WebSocket未处于OPEN状态

问题描述 投票:0回答:1

我在ASP.NET Core上尝试SignalR。从VisaulStudio调试器运行良好。

但是它在部署的代码中不起作用,显示错误消息“WebSocket未处于打开状态”和“握手被取消”。问题的可能原因是什么?

  • Microsoft.AspNetCore.Mvc 2.2.0
  • Microsoft.AspNetCore.SignalR 1.1.0
  • @ aspnet / signalr 1.1.2
  • Bootstrap 4
  • zGuyery vz.0.0
  • 红隼
  • 没有HTTPS SSL
  • 试过Windows 7和Ubuntu 18.4

Google Chrome上的网络控制台

WebSocket is not in the OPEN state (kms-event-exit.js:12)
Uncaught Error: Seerver returned handshake error: Handshake was canceled. (signalr.min.js:16)
 at HubConnection.processHandshakeResponse (signalr.min.js:16)
 at HubConnection.processIncomingData (signalr.min.js:16)
 at WebSocketTransport.HubConnection.connection.onreceive (signalr.min.js:16)
 at WebSocket.webSocket.onmessag (signalr.min.js:16)
[2019-04-06T01:06:41.965Z] Error: Connection disconnected with error 'Error: Server returned handshake error: Handshake was canceled.'. signalr.min.js:16
Uncaught (in promise) Server returned handshake error: Handshake was canceled. (signalr.min.js:16)

启动功能。

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddRazorOptions(options => options.AllowRecompilingViewsOnFileChange = true);

        services.AddSignalR(options => options.EnableDetailedErrors = true);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseDeveloperExceptionPage();
        if (!env.IsDevelopment())
        {
            //app.UseExceptionHandler("/Home/Main");
            app.UseHsts();
        }

        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseSignalR(routes =>
        {
            routes.MapHub<Hubs.KmsHub>("/KmsHub");
            routes.MapHub<Hubs.AllResetHub>("/AllResetHub");
        });

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "areaDefault",
                template: "{area:exists}/{controller=Home}/{action=Main}/{id?}");

            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Main}/{id?}/{exit?}");
        });
    }

日志

dbug: Microsoft.AspNetCore.Http.Connections.Internal.HttpConnectionDispatcher[4]
      Establishing new connection.
dbug: Microsoft.AspNetCore.SignalR.HubConnectionHandler[5]
      OnConnectedAsync started.
dbug: Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsTransport[1]
      Socket opened using Sub-Protocol: '(null)'.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/favicon.ico
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/favicon.ico'. Physical path: 'D:\K4\KMS\KMS\bin\Release\netcoreapp2.2\publish\wwwroot\favicon.ico'
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 6.664ms 200 image/x-icon
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/lib/Popper/popper.min.js.map
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 3.4573ms 404
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/lib/bootstrap/dist/js/bootstrap.min.js.map
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/lib/signalr/dist/browser/signalr.min.js.map
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 2.3443ms 404
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/lib/bootstrap/dist/js/bootstrap.min.js.map'. Physical path: 'D:\K4\KMS\KMS\bin\Release\netcoreapp2.2\publish\wwwroot\lib\boo
tstrap\dist\js\bootstrap.min.js.map'
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 114.858ms 200 text/plain
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/lib/bootstrap/dist/css/bootstrap.min.css.map
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/lib/bootstrap/dist/css/bootstrap.min.css.map'. Physical path: 'D:\K4\KMS\KMS\bin\Release\netcoreapp2.2\publish\wwwroot\lib\b
ootstrap\dist\css\bootstrap.min.css.map'
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 18.0356ms 200 text/plain
dbug: Microsoft.AspNetCore.SignalR.HubConnectionContext[2]
      Handshake was canceled.
dbug: Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsTransport[7]
      Waiting for the client to close the socket.
dbug: Microsoft.AspNetCore.Http.Connections.Internal.Transports.WebSocketsTransport[2]
      Socket closed.
dbug: Microsoft.AspNetCore.Http.Connections.Internal.HttpConnectionManager[2]
      Removing connection 8K2CDgDs6jWXM7DPMWk_Dg from the list of connections.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 15047.5227ms 101

捕获异常的JavaScript代码。

function kmsEventExit(url) {
    var exitButton = document.getElementById("exitButton");
    var connection = new signalR.HubConnectionBuilder().withUrl(url + "/KmsHub").build();

    //Disable send button until connection is established
    exitButton.disabled = true;

    //Wait until connection finishes.
    connection.start().then(function () {
        exitButton.disabled = false;
    }).catch(function (err) {
        return console.error(err.toString());   //WebSocket is not in the OPEN state
    });

    //Call ExitKms on clicking the button.
    exitButton.addEventListener("click", function (event) {
        connection.invoke("ExitKms").catch(function (err) {
            return console.error(err.toString());
        });
        event.preventDefault();
    });

    //Catch the result.
    connection.on("ExitKmsResult", function (isAlert, options) {
        if (isAlert) {
            swal(JSON.parse(options));
        }
    });
}
asp.net-core signalr
1个回答
2
投票

我已经确定了问题!

它没有用,因为pace.js与signalr.js不兼容。 WebSocket变量在这两个插件中重复。删除pace.js后,SignalR工作正常。

VisualStudio上的SignalR与pace.js配合使用,因为它使用SSE和IIS Express而不是WebSocket和Kestrel,因此pace.js和signalr.js可以与此特定配置兼容。

参考:https://github.com/aspnet/SignalR/issues/2389

© www.soinside.com 2019 - 2024. All rights reserved.