Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Net maui cannot get information from WebSocket api #2629

Open
zor038 opened this issue Nov 14, 2024 · 0 comments
Open

Net maui cannot get information from WebSocket api #2629

zor038 opened this issue Nov 14, 2024 · 0 comments
Labels
dotnet-maui/subsvc dotnet-mobile/svc Pri2 support-request Support-style question;customer needs help solving a problem [org]

Comments

@zor038
Copy link

zor038 commented Nov 14, 2024

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

Related Issues

@dotnetrepoman dotnetrepoman bot added the ⌚ Not Triaged Not triaged label Nov 14, 2024
@davidbritch davidbritch added the support-request Support-style question;customer needs help solving a problem [org] label Dec 3, 2024
@dotnetrepoman dotnetrepoman bot removed the ⌚ Not Triaged Not triaged label Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-maui/subsvc dotnet-mobile/svc Pri2 support-request Support-style question;customer needs help solving a problem [org]
Projects
None yet
Development

No branches or pull requests

2 participants