docs-maui
                                
                                 docs-maui copied to clipboard
                                
                                    docs-maui copied to clipboard
                            
                            
                            
                        Net maui cannot get information from WebSocket api
Type of issue
Other (describe below)
Description
I have a WebSocket application that I used in C# console before. I had no problems with it working in console, but I want to convert it to a multimedia project with netmau. Android emulator does not work on my computer. That's why I do my tests in Windows emulator.
When I check with brekboint in the code I shared below, it gets stuck when it reaches WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment(buffer), CancellationToken.None); I can't get any response. I don't know if I'm starting the application incorrectly or if there's a part I need to add.
My codes are as follows, I would be glad if you could guide me :(
`using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Net.WebSockets; using System.Text; using System.Threading.Tasks;
namespace MauiApp1.Model { public static class WebSocket { public static string checker = Ayarlar.ComputeSha256Hash(Ayarlar.APIKEY + Ayarlar.hostname + "/ws"); public static ClientWebSocket webSocket = new ClientWebSocket(); public static DateTime senddate = DateTime.Now; public static async Task ConnectToWebsocket() { try { webSocket.Options.SetRequestHeader("APIKEY", Ayarlar.APIKEY); webSocket.Options.SetRequestHeader("Authorization", Ayarlar.HASH); webSocket.Options.SetRequestHeader("Checker", checker); await webSocket.ConnectAsync(new Uri(Ayarlar.websocketurl), CancellationToken.None);
        await Task.WhenAll(Receive(webSocket), Send(webSocket));
    }
    catch (Exception ex)
    {
    }
}
private static async Task Receive(ClientWebSocket webSocket)
{
    byte[] buffer = new byte[1024];
    while (webSocket.State == System.Net.WebSockets.WebSocketState.Open)
    {
        try
        {
            WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
            string a = System.Text.Encoding.UTF8.GetString(buffer, 0, result.Count);
            WebsocketData model = new WebsocketData();
            model = JsonConvert.DeserializeObject<WebsocketData>(a);
            if (model != null && model.Type == "D")
            {
                Depth depthmodel = JsonConvert.DeserializeObject<Depth>(JsonConvert.SerializeObject(model.Content));
                Console.WriteLine(JsonConvert.SerializeObject(depthmodel));
            }
            else if (model != null && model.Type == "T")
            {
                Tick tickmodel = JsonConvert.DeserializeObject<Tick>(JsonConvert.SerializeObject(model.Content));
                Console.WriteLine(JsonConvert.SerializeObject(tickmodel));
            }
            //Console.WriteLine(a);
        }
        catch (Exception ex)
        {
        }
    }
    if (webSocket.State != System.Net.WebSockets.WebSocketState.Open)
    {
        ConnectToWebsocket();
    }
}
private static async Task Send(ClientWebSocket webSocket)
{
    while (webSocket.State == System.Net.WebSockets.WebSocketState.Open)
    {
        if (senddate < DateTime.Now)
        {
            senddate = DateTime.Now.AddMinutes(15);
            string message = "{\"Type\":\"H\",\"Token\":\"" + Ayarlar.HASH + "\"}";
            var encoded = Encoding.UTF8.GetBytes(message);
            var buffer = new ArraySegment<Byte>(encoded, 0, encoded.Length);
            await webSocket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
        }
    }
}
}`
The code I use to start the code is as follows
` public MainPage() { InitializeComponent();
WebSocket.ConnectToWebsocket(); }`
I would be very glad if you could support me on this subject. Thank you in advance
Page URL
https://learn.microsoft.com/tr-tr/dotnet/maui/whats-new/dotnet-8?view=net-maui-8.0
Content source URL
https://github.com/dotnet/docs-maui/blob/main/docs/whats-new/dotnet-8.md
Document Version Independent Id
1abc248e-68ab-afa8-4b3d-7fc5e77f255b
Article author
@davidbritch
Metadata
- ID: 1abc248e-68ab-afa8-4b3d-7fc5e77f255b
- Service: dotnet-mobile
- Sub-service: dotnet-maui