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 badges1 Answer
Reset to default 6The 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条)