Godot Version
4.3
Question
I am trying to connect my XR app to ping the WebSocket server at “wss://echo.websocket.org”, but I am encountering issues when running the app on an Android device, specifically the HTC XR Elite. Below is my code:
using Godot;
using System;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
public partial class Manager : Node3D
{
private ClientWebSocket _client;
public override void _Ready()
{
GD.Print("From C# Manager");
_client = new ClientWebSocket();
ConnectToWebSocket();
}
private async void ConnectToWebSocket()
{
try
{
await _client.ConnectAsync(new Uri("wss://echo.websocket.org"), CancellationToken.None);
GD.Print("Connected to WebSocket server");
// Send a message
await SendMessageAsync("Hello WebSocket Server!");
// Receive messages
await ReceiveMessagesAsync();
}
catch (Exception ex)
{
GD.PrintErr("Failed to connect: " + ex.Message);
}
}
private async Task SendMessageAsync(string message)
{
if (_client.State == WebSocketState.Open)
{
var buffer = Encoding.UTF8.GetBytes(message);
await _client.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);
GD.Print("Message sent: " + message);
}
}
private async Task ReceiveMessagesAsync()
{
var buffer = new byte[1024];
while (_client.State == WebSocketState.Open)
{
var result = await _client.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Close)
{
GD.Print("Server closed connection.");
break;
}
var message = Encoding.UTF8.GetString(buffer, 0, result.Count);
GD.Print("Received: " + message);
}
}
public override void _ExitTree()
{
if (_client != null && _client.State == WebSocketState.Open)
{
_client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None).Wait();
_client.Dispose();
GD.Print("WebSocket closed on exit.");
}
}
}
This code works fine in my Windows app. Here’s the Windows output:
From C# Manager
OpenXR initialized successfully
Connected to WebSocket server
Message sent: Hello WebSocket Server!
Received: Request served by 1781505b56ee58
Received: Hello WebSocket Server!
However, when I run the app on the HTC Vive XR Elite, I get the following error in adb, and the app quits:
12-03 11:38:47.166 14704 14745 I godot : Godot Engine v4.3.stable.mono.official.77dcf97d8 - https://godotengine.org
12-03 11:38:47.281 14704 14745 I godot : OpenXR: Running on OpenXR runtime: VIVE WAVE 0.0.1
12-03 11:38:47.316 14704 14745 I godot : OpenXR: XrGraphicsRequirementsVulkan2KHR:
12-03 11:38:47.316 14704 14745 I godot : - minApiVersionSupported: 1.0.0
12-03 11:38:47.316 14704 14745 I godot : - maxApiVersionSupported: 1.2.203
12-03 11:38:47.326 14704 14745 I godot : Vulkan 1.1.128 - Forward Mobile - Using Device #0: Qualcomm - Adreno (TM) 650
12-03 11:38:48.619 14704 14745 I godot :
12-03 11:38:50.242 14704 14745 I godot : from C# Manager
12-03 11:38:50.244 14704 14745 I godot : Starting WebSocket client connection
12-03 11:38:50.290 14704 14745 E godot : USER ERROR: BUG: Unreferenced static string to 0: .
12-03 11:38:50.290 14704 14745 E godot : at: unref (core/string/string_name.cpp:127)
12-03 11:38:50.290 14704 14745 E godot : USER ERROR: BUG: Unreferenced static string to 0: _enter_world
12-03 11:38:50.290 14704 14745 E godot : at: unref (core/string/string_name.cpp:127)
12-03 11:38:50.290 14704 14745 E godot : USER ERROR: BUG: Unreferenced static string to 0: Variant
12-03 11:38:50.290 14704 14745 E godot : at: unref (core/string/string_name.cpp:127)
12-03 11:38:50.291 14704 14745 E godot : USER ERROR: BUG: Unreferenced static string to 0: ShaderCompilation
12-03 11:38:50.291 14704 14745 E godot : at: unref (core/string/string_name.cpp:127)
The network permissions are enabled in the app. I also ran the sample WebSocket code from the Godot docs for GDScript link, and it works fine on the HTC. Here is the adb log for the GDScript WebSocket:
Godot Engine v4.3.stable.mono.official.77dcf97d8 - https://godotengine.org
12-03 10:13:16.909 9654 9694 I godot : OpenXR: Running on OpenXR runtime: VIVE WAVE 0.0.1
12-03 10:13:16.948 9654 9694 I godot : OpenXR: XrGraphicsRequirementsVulkan2KHR:
12-03 10:13:16.948 9654 9694 I godot : - minApiVersionSupported: 1.0.0
12-03 10:13:16.948 9654 9694 I godot : - maxApiVersionSupported: 1.2.203
12-03 10:13:16.960 9654 9694 I godot : Vulkan 1.1.128 - Forward Mobile - Using Device #0: Qualcomm - Adreno (TM) 650
12-03 10:13:17.701 9654 9694 I godot :
12-03 10:13:18.331 9654 9694 I godot : OpenXR initialized successfully
12-03 10:13:18.734 9654 9694 I godot : Got data from server: Request served by 1781505b56ee58
12-03 10:13:20.303 9654 9694 I godot : Got data from server: Test packet
P.S.: I also tried using WebSocketSharp but encountered the same issue.
If anyone has insights into this problem or suggestions for a solution, I would greatly appreciate it!