小编Yuc*_*ked的帖子

ASP.NET 核心 WebSocket

我正在尝试在 ASP.NET Core 上启动并运行 WebSocket 服务器。我创建了一个空的 web 项目,dotnet new web将其更改Program.cs为:

public static void Main(string[] args) {
    Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder => {
        webBuilder.UseStartup<Startup>();
    })
    .Build()
    .Run();
}
Run Code Online (Sandbox Code Playgroud)

AndStartup.csConfigureServices方法:

public void ConfigureServices(IServiceCollection services) {
    services.AddControllers();
    services.AddWebSockets();
}
Run Code Online (Sandbox Code Playgroud)

Configure方法:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
    app.UseWebSockets();
    app.UseRouting();
    app.UseEndpoints(endpoints => {
        endpoints.MapControllers();
        endpoints.MapConnectionHandler<WebSocketHandler>("/ws");
    });
}
Run Code Online (Sandbox Code Playgroud)

WebSocketHandlerOnConnectedAsync方法如下:

public override async Task OnConnectedAsync(ConnectionContext connection) 
{
    var context = connection.GetHttpContext();
    var endpoint = …
Run Code Online (Sandbox Code Playgroud)

c# websocket .net-core asp.net-core

10
推荐指数
1
解决办法
9849
查看次数

标签 统计

.net-core ×1

asp.net-core ×1

c# ×1

websocket ×1