javascript - ASP.NET Core WebSocket failed during handshake - Stack Overflow

I'm trying to use ASP.NET Core WebSocket, but got an error like this when new WebSocket("ws:

I'm trying to use ASP.NET Core WebSocket, but got an error like this when new WebSocket("ws://localhost:5702"); in JavaScript:

WebSocket connection to 'ws://localhost:5702/' failed: Error during WebSocket handshake: Unexpected response code: 200

My Configure method in Startup.cs:

public void Configure(IApplicationBuilder app)
{
     app.UseDefaultFiles();

     app.UseStaticFiles();

     app.UseWebSockets();
     app.UseWebSocketHandler();
}

My Middlewares\WebSocketMiddleware.cs:
WebSocketMiddleware.cs

Does anyone know?

Thank you.

I'm trying to use ASP.NET Core WebSocket, but got an error like this when new WebSocket("ws://localhost:5702"); in JavaScript:

WebSocket connection to 'ws://localhost:5702/' failed: Error during WebSocket handshake: Unexpected response code: 200

My Configure method in Startup.cs:

public void Configure(IApplicationBuilder app)
{
     app.UseDefaultFiles();

     app.UseStaticFiles();

     app.UseWebSockets();
     app.UseWebSocketHandler();
}

My Middlewares\WebSocketMiddleware.cs:
WebSocketMiddleware.cs

Does anyone know?

Thank you.

Share Improve this question asked Mar 23, 2017 at 15:38 Justinus HermawanJustinus Hermawan 1,2141 gold badge26 silver badges47 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

The behaviour you are experiencing is because some other middleware (UseStaticFiles) is returning a 200 OK code to your websocket client.

To avoid this, make sure to place your WebSocket middleware before UseStaticFiles(). Personally, I'd put it at the top, like so:

    public void Configure(IApplicationBuilder app)
    {
        //first handle any websocket requests
        app.UseWebSockets();
        app.UseWebSocketHandler();

        //now handle other requests (default, static files, mvc actions, ...)
        app.UseDefaultFiles();
        app.UseStaticFiles();
        //app.UseMvc(...);
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744131593a4559867.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信