2

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);
}
iBener
  • 469
  • 6
  • 18
Lukas
  • 65
  • 5
  • 1
    Try updating kernel. See : https://trendoceans.com/upgrade-linux-kernel-to-5-16-release/#:~:text=The%20first%20thing%20to%20do%20before%20upgrading%20the,install%20headers%2C%20modules%2C%20and%20images%20for%20kernel%205.16. – jdweng Jul 21 '22 at 23:16
  • Thank you for idea, and maybe that could work, but i need some code solution inside Visual Studio – Lukas Jul 22 '22 at 10:55
  • Older kernels may not support the encryption mode. Since Net 4.7.2 encryption for TLS is done in the operating system. The operating system has to support the encryption mode. To do in Net you would have to disable the Operating System option and use Net for TLS. Then you would have to get a HTTP library that supported TLS and use OpenSSL (or equivalent). I would check the csproj (open in notebook) and see if the option for TLS is Net or operating system. – jdweng Jul 22 '22 at 12:04
  • Yeah, it's in encryption, when i connect to unsecured websocket it works, i tried to find where i can change that setting, but i didnt find it – Lukas Jul 22 '22 at 13:38
  • You are using HTTP (not HTTPS secure). The server usually has HTTPS required. To disable on server probably requires a registry change. – jdweng Jul 22 '22 at 15:14
  • You need to use the latest Android 5 and Android 8 updates. – jdweng Jul 24 '22 at 09:10
  • It only dont work on .NET MAUI app. In Xamarin app it works fine. I copied this from xamarin project where secure websocket work on android 8. `             Xamarin.Android.Net.AndroidClientHandler                     ` But after i paste it in my .NET MAUI project file, when i run app it crash on startup. – Lukas Aug 01 '22 at 08:45
  • See following which is from March. Things may have changed : https://learn.microsoft.com/en-us/answers/questions/788451/enable-tls-12-on-net-maui-on-android-vs-2022-previ.html?force_isolation=true – jdweng Aug 01 '22 at 09:06
  • Also : https://github.com/dotnet/maui/discussions/8131?force_isolation=true – jdweng Aug 01 '22 at 09:15

0 Answers0