Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 this issue Apr 12, 2023 · 4 comments

Comments

@eduardoseitz
Copy link

eduardoseitz commented Apr 12, 2023

`
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
Copy link
Author

The server is running with no errors.

@Steve-Morales
Copy link

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
Copy link

@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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants