endel / NativeWebSocket

🔌 WebSocket client for Unity - with no external dependencies (WebGL, Native, Android, iOS, UWP)
Other
1.2k stars 158 forks source link

I'm using the code sample Connection.cs to connect to ws://localhost:3000 but I am getting a Unable to connect to the remote server. #79

Open eduardoseitz opened 1 year ago

eduardoseitz commented 1 year ago

` public class WebSocketController : MonoBehaviour { WebSocket websocket;

    // Start is called before the first frame update
    async void Start()
    {
        websocket = new WebSocket("ws://localhost:3000");

        websocket.OnOpen += () =>
        {
            Debug.Log("Connection open!");
        };

        websocket.OnError += (e) =>
        {
            Debug.Log("Error! " + e);
        };

        websocket.OnClose += (e) =>
        {
            Debug.Log("Connection closed!");
        };

        websocket.OnMessage += (bytes) =>
        {
            Debug.Log("OnMessage!");
            Debug.Log(bytes);

            // getting the message as a string
            // var message = System.Text.Encoding.UTF8.GetString(bytes);
            // Debug.Log("OnMessage! " + message);
        };

        // Keep sending messages at every 0.3s
        InvokeRepeating("SendWebSocketMessage", 0.0f, 0.3f);

        // waiting for messages
        await websocket.Connect();
    }

    void Update()
    {

if !UNITY_WEBGL || UNITY_EDITOR

        websocket.DispatchMessageQueue();

endif

    }

    async void SendWebSocketMessage()
    {
        if (websocket.State == WebSocketState.Open)
        {
            // Sending bytes
            await websocket.Send(new byte[] { 10, 20, 30 });

            // Sending plain text
            await websocket.SendText("plain text message");
        }
    }

    private async void OnApplicationQuit()
    {
        await websocket.Close();
    }
}

`

eduardoseitz commented 1 year ago

The server is running with no errors.

Steve-Morales commented 1 year ago

I ran into a similar problem and the solution is quite simple. You have to use your host's IP address rather than localhost -- this goes for all clients (i.e different devices or scripts). For clarity, look up your IPv4 address (it should be something like 192.168.X.X) and replace localhost with the IP address which should result in looking something more like "ws://192.168.X.X:3000".

Also note that this is only on local networks (which should be on the same router!).

Here's a stackoverflow answer that helped me reach to this answer and may be additional use if you run into more errors. https://stackoverflow.com/questions/21410435/connect-websocket-server-by-lan-ip-address

MarekZeman91 commented 1 year ago

@Steve-Morales sadly that didn't solve it. I have the same issue. I have a Node.js server and I am able to connect from terminal, local website, remote website but I simply can't connect with Unity. I tried localhost/127/192/hosts and nothing worked for Unity.

I even created new URL in /etc/hosts/ and used Docker for more separation. Nothing.

string address = "unity-sockets.com:8080";

// this connects fine
UnityWebRequest webReq = new($"http://{address}", "GET");
webReq.SendWebRequest();

// this won't even ping the server
websocket = new WebSocket($"ws://{address}");
websocket.Connect();

I tried to create a local https/wss server but it won't connect due to certificate issue. Still, the socket did not even ping the server.

glebov21 commented 5 months ago

Maybe your server have https redirection. Try to open http://localhost:3000 in chrome and view F12 network