endel / NativeWebSocket

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

"Unable to connect to the remote server" error when trying to connect to my websocket server #17

Closed student020341 closed 4 years ago

student020341 commented 4 years ago

Hey! I hope this is something simple. It seems to have been a common error that spans the last decade of stack overflow, but of course a lot of them end without a solution. Just a conversation that gets about as far as I have - I can connect to the server with chrome's simple websocket server and send & receive messages, but I get the unable to connect error firing on the error callback when trying to setup a connection as shown in the readme. Here is my server and the websocket code:

https://github.com/student020341/game-server/blob/master/src/sock/eat.go#L26

I'm using the golang websocket from this package and all I can say about my understanding of that is that connections from the web browser work.

https://godoc.org/golang.org/x/net/websocket

I appreciate any suggestions, thanks! In the mean time I'm going to try a much more painful approach using System.Net.WebSockets to see if anything changes or that lets me get a more specific error message.

edit with more info:

I was able to connect to wss://echo.websocket.org with the c# client (from this project)

So... I can create a websocket from the dev tools console and connect to my server, I can connect to echo.websocket.org with this project, but I can't connect to my LAN server with this project. Hopefully that makes the problem more clear to someone :)

The url is ws://192.168.86.201:2001/sock/connect , it's a computer in my local network that is running the server in the repo I linked.

edit 2:

I was able to reproduce this with a small dotnet project that tries the connection right in main. No issues to the echo site, same error to mine. Will continue random things but that's probably the last useful tidbit I'll be able to provide. At least that rules out unity as the problem.

student020341 commented 4 years ago

I very strongly doubt anyone is going to run into this issue, but I did just solve it with these 2 checks.

1. Even though it's in the same network, ensure the port is open. Must be a C# thing.

  1. I had to change the way I was serving the websocket
var handler websocket.Handler = socketStuff
handler.ServeHTTP(w, r)

to

    wserver := websocket.Server{Handler: websocket.Handler(socketStuff)}
    wserver.ServeHTTP(w, r)

The difference being the latter allows you to construct the socket server and by omitting a handshake I was able to get around some 403 vs 101 issue. Not sure what's different about this working in a web browser - something about the origin, but this is solved now!

--edit: strike issue 1, just got it working without changing the port. I figured that should be the case because the machine is on the local network, 2 is definitely the main fix.

metalogica commented 1 year ago

hey! any chance you could help me out with this? I'm encountering exactly the same issue as you have but cant get it to work.

metalogica commented 1 year ago

Im trying to connect to a websocket server hosted on my lan at wss://192.168.0.42:6868. It doesn't require authorization.

I can connect via the the tUI app wscat, but when I try to connectvia the Unity editor I get the following error:

Connection Error! Please double check your URL and ensure the cortex API is runningURL: wss://192.168.0.42:6868 Error: Unable to connect to the remote server
UnityEngine.Debug:LogError (object)
CortexClient:<Start>b__13_1 (string) (at Assets/Scripts/Clients/CortexClient.cs:56)
NativeWebSocket.WebSocket/<Connect>d__27:MoveNext () (at Library/PackageCache/com.endel.nativewebsocket@c5101c0776/WebSocket/WebSocket.cs:478)
UnityEngine.UnitySynchronizationContext:ExecuteTasks () (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/UnitySynchronizationContext.cs:107)
student020341 commented 1 year ago

Hey, sorry to hear this issue is still around. I have ceased all of my unity related efforts since this post. Are you sure it's wss instead of ws? For LAN testing I would guess you do not have a certificate / secure connection.