kerryjiang / SuperSocket

SuperSocket is a light weight, cross platform and extensible socket server application framework.
Apache License 2.0
3.95k stars 1.15k forks source link

WebSocket Server doesn't exit with application #693

Closed Antoni-Czaplicki closed 10 months ago

Antoni-Czaplicki commented 10 months ago

After implementing code from the docs server works as expected until i want to close the application, than the app closes itself but the server is still running.

Logs:

Microsoft.Hosting.Lifetime: Information: Application is shutting down...
Microsoft.Hosting.Lifetime: Information: Waiting for the host to be disposed. Ensure all 'IHost' instances are wrapped in 'using' blocks.

Code for the server:

public async Task StartAsync(int port)
{
    // Stop in case of already running
    StopAsync();

    var host = WebSocketHostBuilder.Create()
        .UseWebSocketMessageHandler(
            async (session, message) =>
            {
                await session.SendAsync(message.Message);
            }
        )
        .UseSessionHandler(async (s) =>
        {
            await Server_NewSessionConnected(s as WebSocketSession);
        },
        async (s, e) =>
        {
            // s: the session
            // e: the CloseEventArgs
            // e.Reason: the close reason
            // things to do after the session closes
            await Server_SessionClosedAsync(s as WebSocketSession, e.Reason);
        })
        .ConfigureSuperSocket(options =>
        {
            options.Name = "test";
            options.AddListener(new ListenOptions
            {
                Ip = "Any",
                Port = port
            }
            );
            options.ReceiveBufferSize = 1024 * 1024;
            options.MaxPackageLength = 1024 * 1024;
        })
        .ConfigureLogging((hostCtx, loggingBuilder) =>
        {
            loggingBuilder.AddConsole();
        })
        .Build();

    await host.RunAsync();

}

Rest of the code: https://github.com/Antoni-Czaplicki/SteamVR.HA-Agent

I'm quite new in .net so if this is my simple mistake somewhere in the code I apologize for the inconvenience 🫣