admin管理员组文章数量:1193729
I am seeing strange behavior when trying to use both SignalR and plain old web sockets on the same Kestrel web server hosing an ASP.NET Core 8 Web API project.
The project has a REST interface, uses SignalR, and plain old web sockets were added to accommodate a client for which there is no SignalR client library.
Independently, either of them both seem to work fine. As soon as I try and use them both, all communication through both of them stops until one of them disconnects.
SignalR is being added to the builder services in Program.cs
like this:
builder.Services.AddSignalR(options => {
options.EnableDetailedErrors = true;
});
And websockets are being setup like this:
app.UseWebSockets();
app.Map("/ws", WebSocketHandler.HandleWebSocketConnection);
Here is the WebSocketHandler.HandleWebSocketConnection
method:
public static async Task HandleWebSocketConnection(HttpContext context)
{
if (!context.WebSockets.IsWebSocketRequest)
{
context.Response.StatusCode = 400; // Bad Request
await context.Response.WriteAsync("WebSocket request expected.");
return;
}
var clientId = Guid.NewGuid().ToString();
WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
Console.WriteLine($"Client connected: {clientId}");
try
{
await HandleClientMessagesAsync(webSocket);
}
finally
{
Console.WriteLine($"Client disconnected: {clientId}");
}
}
I wouldn't think this is a problem because under the covers, there is only one websocket server running on one port. How can both of these be enabled on the same web server without conflicting?
本文标签: cUse BOTH SignalR and raw web sockets in ASPNET Core 8 Web API projectStack Overflow
版权声明:本文标题:c# - Use BOTH SignalR and raw web sockets in ASP.NET Core 8 Web API project - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738456910a2087808.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论