I am trying to establish a websocket connection in a MAUI program in a background thread and output the incoming websocket messages. The websocket server sends correctly. This has already been tested in a console application.
App.xaml.cs class:
namespace MauiWebSocketTest;
public partial class App : Application
{
public App()
{
InitializeComponent();
WebSocketConnection webSocket = new WebSocketConnection();
Thread t = new Thread(() => webSocket.OpenWebSocketConnection());
t.Start();
MainPage = new AppShell();
}
}
WebSocketConnection.cs
using Newtonsoft.Json;
using WebSocketSharp;
namespace MauiWebSocketTest
{
public class WebSocketConnection
{
public void OpenWebSocketConnection()
{
using (var ws = new
WebSocket("ws://82.165.185.245:8081/deephub/v1/ws/socket"))
{
ws.OnOpen += (sender, e) => ws.Send(JsonConvert.SerializeObject(new
FenceEvents()));
ws.OnMessage += (sender, e) => {
Console.WriteLine(e.Data);
};
}
}
}
}
After starting the program the thread is started but does not return any results. Only the subscribing is executed once but no answers are received. Maybe the thread is killed? I don't have much experience with threads. For any help I would be very grateful