endel / NativeWebSocket

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

Problem connecting to websocket server on local network wit an external ip 192.168.0.xx #72

Open metalogica opened 1 year ago

metalogica commented 1 year ago

PROBLEM

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

The websocket client fails the TLS handshake if i use an external ip like wss://192.168.0.xxx:6868 but it works fine if i use wss://localhost:6868

DETAILS

I can connect via the the TUI app wscat, but when I try to connect via 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)

Ideally I'd like to connect to the same local web socketserver over the local network on the Unity editor, and via a compiled device in VR.

metalogica commented 1 year ago

I used a debugger to figure out some more context, which I detail below.

// WebSocket.cs
// we crash here
await m_Socket.ConnectAsync(uri, m_CancellationToken); l.472 

Stack Trace

UNITYTLS_INTERNAL_ERROR

at System.Net.WebSockets.WebSocketHandle.ConnectAsyncCore (System.Uri uri, System.Threading.CancellationToken cancellationToken, System.Net.WebSockets.ClientWebSocketOptions options) [0x00391] in <2fe115660d9c47728edff248f3625297>:0 \n  at System.Net.WebSockets.ClientWebSocket.ConnectAsyncCore (System.Uri uri, System.Threading.CancellationToken cancellationToken) [0x000d1] in <2fe115660d9c47728edff248f3625297>:0 

 at NativeWebSocket.WebSocket.Connect () [0x001a5] in /Users/richardjarram/Documents/unity/unity-bp-physics-master/Library/PackageCache/com.endel.nativewebsocket@c5101c0776/WebSocket/WebSocket.cs:472 

Screen Shot 2022-10-28 at 3 20 13 PM

Config that does not work

Screen Shot 2022-10-28 at 3 14 19 PM Screen Shot 2022-10-28 at 3 14 55 PM

Config that works

Screen Shot 2022-10-28 at 3 16 38 PM

Screen Shot 2022-10-28 at 3 17 09 PM

endel commented 1 year ago

Does it work if you try with ws instead of wss? ws://192.168.0.xxx:6868

wss means secure and it will expect a TLS certificate configured

metalogica commented 1 year ago

unfortunately the web socket server I am accessing is a propriety application running on my local raspberry pi. just for info, its the emotiv cortex brain machine interface api.

It only uses wss:// and there is no config for ws://.

metalogica commented 1 year ago

Do you think it could be related to certificate parsing in the websocket library?

In Unity, when I run wss://127.0.0.1:6868 I get the same connection error as before.

I have a self-signed cert for localhost but not for 127.0.0.1.

Perhaps I need to create self signed cert for the ip 192.168.0.42 (or create a hostname for it, then a cert for that) in order to get this thing running?

metalogica commented 1 year ago

I created a self signed cert on my local machine for this service but to no avail.

metalogica commented 1 year ago

Seems top be similar to these issues: https://github.com/endel/NativeWebSocket/issues/1 https://github.com/endel/NativeWebSocket/issues/70