无法连接到.Net Core Blazor WASM应用程序中的WebSocket控制器

rxztt3cl  于 2023-06-06  发布在  .NET
关注(0)|答案(1)|浏览(179)

我正在尝试在我的WASM应用程序中建立WebSocket连接。我已经按照MSDN教程并在我的Program.cs中启用了WebSockets:

app.UseWebSockets();

然后,我添加了一个新的控制器,如下所示:

[AllowAnonymous]
[ApiController]
[Route("[controller]")]
internal class ShellyPlusDataController : ControllerBase
{
    [HttpGet]
    [Route("[controller]/OnDataReceived")]
    public async Task OnDataReceived()
    {
        if (HttpContext.WebSockets.IsWebSocketRequest)
        {
            using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();

            var buffer = new byte[1024 * 4];
            WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);

            while (!result.CloseStatus.HasValue)
            {
                string raw = Encoding.UTF8.GetString(buffer, 0, result.Count);
                result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
            }

            await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
        }
        else
        {
            HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
        }
    }
}

使用PostMan打开到'wss://localhost:7220/ShellyPlusData/OnDataReceived'的连接不起作用。显示的错误为:错误:服务器异常响应:200
我在 OnDataReceived() 的开始处放置了一个断点;它永远不会被击中。我也尝试过将URL更改为ws://或省略方法名称,但没有成功。
Microsoft教程在Program.cs中也建议这样做:

app.Use(async (context, next) =>
{
    if (context.Request.Path == "/ws")
    {
        if (context.WebSockets.IsWebSocketRequest)
        {
            using var webSocket = await context.WebSockets.AcceptWebSocketAsync();
            await Echo(webSocket);
        }
        else
        {
            context.Response.StatusCode = StatusCodes.Status400BadRequest;
        }
    }
    else
    {
        await next(context);
    }
});

也不管用有什么建议吗?

i86rm4rw

i86rm4rw1#

所以我得自己想办法。有两件事需要解决:
1.本地WebSocket连接不起作用。我不知道在本地调试模式下启用WebSockets需要配置什么,但是当我将应用程序部署到远程IIS服务器时,我可以建立连接。
1.我的控制器类是内部的,当使用WS连接时,路由似乎以不同的方式工作。这是工人阶级:
public class ShellyPlusDataConnectionController:ControllerBase {

[HttpGet("/ShellyPlusDataConnection/OnDataReceived")]
    public async Task OnDataReceived()
    {
        if (HttpContext.WebSockets.IsWebSocketRequest)
        {
            using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();

            var buffer = new byte[1024 * 4];
            WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);

            while (!result.CloseStatus.HasValue)
            {
                string raw = Encoding.UTF8.GetString(buffer, 0, result.Count);

                result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
            }

            await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
        }
        else
        {
            HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
        }
    }
}

相关问题