.NET MAUI WebSocket doesn't work on android 5 and 8

1.4k Views Asked by At

On my android 11 device my websocket works fine, but when i try to run it on older phone with android 5 or 8, it doesnt work and it only says Unable to connect to the remote server Is there some way to make it works ? I am using System.Net.WebSockets;

async Task StartWebSocketAsync()
{                
    await ws.ConnectAsync(new Uri("wss://url?token=" + Token), t);      
    await Task.Factory.StartNew(async () =>
    {
        while (true)
        {
            await ReadMessage();
        }
    }, t, TaskCreationOptions.LongRunning, TaskScheduler.Default);
    SendMessageAsync(msg);
}
async Task ReadMessage()
{
    WebSocketReceiveResult result;
    var message = new ArraySegment<byte>(new byte[4096]);
    do
    {
        result = await ws.ReceiveAsync(message, t);
        if (result.MessageType != WebSocketMessageType.Text)
            break;
        var messageBytes = message.Skip(message.Offset).Take(result.Count).ToArray();
        string receivedMessage = Encoding.UTF8.GetString(messageBytes);
        JObject jsonObject = JObject.Parse(receivedMessage);
        Status = (string)jsonObject["data"]["state"][0][1];
    }
    while (!result.EndOfMessage);        
}
async void SendMessageAsync(string message)
{
    var byteMessage = Encoding.UTF8.GetBytes(message);
    var segmnet = new ArraySegment<byte>(byteMessage);

    await ws.SendAsync(segmnet, WebSocketMessageType.Text, true, t);
}
0

There are 0 best solutions below