Hello everyone! I’m working on a little project, where you can control the unity game with camera. The image processing happens in python from where i send the data via websockets to unity. First I want to make it locally, but I’m running into a problem where, in the unity editor the C# script connects fine to the python websocket, but once i build the game in WebGL (run it locally with python -m http.server), with the same method the game wont connect anymore. Does anyone know why this happens, and what would the solution for it be? I attached the mentioned c# script that handles the connecting in case i overlooked something in it. Any help would be greatly appreciated, and thanks in advance! Since i cant attach files because i am a new user, i can show my script only this way:
using System;
using System.Collections.Concurrent; // For thread-safe data queue
using UnityEngine;
using UnityEngine.UI;
using WebSocketSharp;
using System.Threading.Tasks;
using TMPro;
public class PythonSocketClient : MonoBehaviour
{
private WebSocket ws; // WebSocket instance
public Text ClientConnect;
public TMP_InputField urlInputField; // InputField for user to input the WebSocket URL
private ConcurrentQueue<int> gestureQueue = new ConcurrentQueue<int>(); // Thread-safe queue for gestures
public async void Start()
{
string url = urlInputField.text; // Get the URL from the InputField
if (string.IsNullOrEmpty(url))
{
ClientConnect.text = "Please enter a valid URL!";
return;
}
// Automatically format the full WebSocket URL
string fullUrl = $"{url}/hand_data";
Debug.Log($"WebSocket URL: {fullUrl}");
try
{
// Initialize the WebSocket connection
ws = new WebSocket(fullUrl);
// Event for when WebSocket opens
ws.OnOpen += (sender, e) =>
{
Debug.Log("Connected to Python WebSocket server.");
ClientConnect.text = "Successfully Connected to Camera!";
};
// Event for when WebSocket receives a message
ws.OnMessage += (sender, e) =>
{
try
{
int gesture = int.Parse(e.Data.Trim());
// Queue the gesture for processing on the main thread
gestureQueue.Enqueue(gesture);
}
catch (Exception ex)
{
Debug.LogError($"Error parsing gesture data: {ex.Message}");
}
};
// Event for when WebSocket encounters an error
ws.OnError += (sender, e) =>
{
Debug.LogError($"WebSocket error: {e.Message}");
ClientConnect.text = $"Failed to Connect to Camera!\n{e.Message}";
};
// Event for when WebSocket closes
ws.OnClose += (sender, e) =>
{
Debug.Log("Disconnected from WebSocket server.");
ClientConnect.text = "Disconnected from Camera!";
};
// Connect to the WebSocket server
ws.Connect();
// Await a short delay to update the UI
if (ws.ReadyState == WebSocketState.Open)
{
await Task.Delay(3000); // Wait for 3 seconds
ClientConnect.text = "";
}
else
{
Debug.LogError("Failed to connect to WebSocket server.");
ClientConnect.text = "Failed to Connect to Camera!\nRefresh Page or Play with Traditional Controls";
await Task.Delay(6000); // Wait for 6 seconds
ClientConnect.text = "";
}
}
catch (Exception e)
{
Debug.LogError($"WebSocket connection error: {e.Message}");
ClientConnect.text = "Failed to Connect to Camera!\nRefresh Page or Play with Traditional Controls";
await Task.Delay(6000); // Wait for 6 seconds
ClientConnect.text = "";
}
}
void Update()
{
// Process all queued gestures
while (gestureQueue.TryDequeue(out int gesture))
{
// Update the singleton with the new gesture
GestureManager.Instance?.UpdateGesture(gesture);
}
}
void OnApplicationQuit()
{
if (ws != null && ws.ReadyState == WebSocketState.Open)
{
ws.Close(CloseStatusCode.Normal); // Gracefully close the WebSocket connection
}
}
}