Awaiting ClientWebSocket.ReceiveAsync only receives one packet of information, and then all other awaits never return

I’m using the System.Net.WebSockets.ClientWebSocket class in C# on .NET 6.0.

I have a timer set up that repeatedly will poll the websocket for connections, shown here. The timer is elapsed every 16 ms.

When the packet is received, it’ll deserialize it as JSON and create an event. This works, but it only does it once.

I added the print lines for testing:

Console.WriteLine("Awaiting started") 

and

Console.WriteLine("Awaiting ended")

Awaiting started should be printed when the websocket is polled. It should print “Awaiting ended” after the websocket has finished the polling, and then if the packet received isn’t empty, it’ll deserialize it.

private async void poll(Object? sender, ElapsedEventArgs event_args) 
{
    switch (web_socket.State) 
    {
        case WebSocketState.Open: 
            {
                if (socket_connected == false) 
                {
                    socket_connected = true;
                }

                try 
                {
                    // ReceiveAsync automatically polls and receives the packet.
                    Memory<Byte> memory = new Memory<Byte>(new Byte[1024]);
                    Console.WriteLine("Awaiting started");
                    ValueWebSocketReceiveResult result = await web_socket.ReceiveAsync(memory, CancellationToken.None);
                    Console.WriteLine("Awaiting ended");

                    // Only attempt deserialization if a packet was received, aka the buffer is not empty.
                    if (result.Count != 0) 
                    {
                        deserialize_event(memory.Slice(0, result.Count).ToArray());
                    }
                } 
                catch (Exception exception) 
                { 
                    Console.WriteLine("Pusher polling error: " + exception.ToString());
                }
            } 
            break;

        case WebSocketState.Closed: 
            {
                connection_closed(web_socket.CloseStatus);
            } 
            break;
    }
}

What’s supposed to happen: it should first print that its awaiting, then await the packet, then write the contents to the buffer, then move on with the execution, printing that the Await ended.

What actually happens:

It looks like it only receives one singular packet, and then all calls to ReceiveAsync never finish.

New contributor

FireCatMagic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Using a timer will call ReceiveAsync again before the previous call is complete, which is not supported.

Exactly one send and one receive is supported on each ClientWebSocket
object in parallel. Issuing multiple sends or multiple receives at the
same time (for example, without awaiting, or from multiple threads
without synchronization) is not supported and will result in an
undefined behavior.

https://learn.microsoft.com/en-us/dotnet/api/system.net.websockets.clientwebsocket?view=net-8.0

Instead of invoking the read from a timer, create a thread (or Long Lived Task), and invoke await websocket.ReceiveAsync in a loop.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật