© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
11 replies
VeQox

❔ Raw Websockets, ref alternative in async programming

 public async Task HandleUpgrade()
    {
        if (!HttpContext.WebSockets.IsWebSocketRequest)
        {
            Logger.LogInformation("Request not a websocket request");
            HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
            return;
        }

        var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
        var connection = new WebSocketConnection(webSocket);
        Logger.LogInformation("Connection established");
        
        try
        {
            Room? room = null;
            Client? client = null;
            
            while (!webSocket.CloseStatus.HasValue)
            {
                var raw = await connection.ReceiveAsync();
                
                Logger.LogInformation("Received {Message} from connection[{Guid}]", raw, connection.Guid);
                
                if (raw is null) continue;

                var (webSocketClientMessage, _) = JsonUtils.Deserialize<WebSocketClientMessage>(raw);
                if (webSocketClientMessage is null) continue;
                
                await OnMessage(webSocketClientMessage, raw, connection, room, client);
            }
        }
        finally
        {
            await connection.CloseAsync();
            Logger.LogInformation("Connection closed with connection[{Guid}]", connection.Guid);
        }
    }
 public async Task HandleUpgrade()
    {
        if (!HttpContext.WebSockets.IsWebSocketRequest)
        {
            Logger.LogInformation("Request not a websocket request");
            HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
            return;
        }

        var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
        var connection = new WebSocketConnection(webSocket);
        Logger.LogInformation("Connection established");
        
        try
        {
            Room? room = null;
            Client? client = null;
            
            while (!webSocket.CloseStatus.HasValue)
            {
                var raw = await connection.ReceiveAsync();
                
                Logger.LogInformation("Received {Message} from connection[{Guid}]", raw, connection.Guid);
                
                if (raw is null) continue;

                var (webSocketClientMessage, _) = JsonUtils.Deserialize<WebSocketClientMessage>(raw);
                if (webSocketClientMessage is null) continue;
                
                await OnMessage(webSocketClientMessage, raw, connection, room, client);
            }
        }
        finally
        {
            await connection.CloseAsync();
            Logger.LogInformation("Connection closed with connection[{Guid}]", connection.Guid);
        }
    }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,828Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Websockets and Async
C#CC# / help
3y ago
✅ Is managed pointer (ref) or raw pointer (void*) null?
C#CC# / help
4y ago
ref keyword in C#
C#CC# / help
13mo ago
❔ Connecting to Websockets in C#
C#CC# / help
3y ago