You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to implement a websocket server in unity with watson websocket. For this I built watson websocket for .Net Standard 2.1 and imported the dll to unity. Starting the websocket seems to work but as soon as I try to connect with a simple javascript client it says non-websocket request.
Server:
publicclassTuioWatsonServer:MonoBehaviour{privatereadonlyWatsonWsServer_server=new();privatereadonlyList<Guid>_connections=new();privatevoidAwake(){_server.AcceptInvalidCertificates=true;_server.HttpHandler=HttpHandler;_server.Logger=Debug.Log;_server.Start();}privatevoidOnEnable(){_server.ClientConnected+=OnClientConnected;_server.ClientDisconnected+=OnClientDisconnected;_server.MessageReceived+=OnMessageReceived;}privatevoidOnDisable(){_server.ClientConnected-=OnClientConnected;_server.ClientDisconnected-=OnClientDisconnected;_server.MessageReceived-=OnMessageReceived;}privatevoidOnClientConnected(objectsender,ConnectionEventArgse){_connections.Add(e.Client.Guid);Debug.Log($"Client connected: {e.Client}");}privatevoidOnClientDisconnected(objectsender,DisconnectionEventArgse){_connections.Remove(e.Client.Guid);Debug.Log($"Client disconnected: {e.Client}");}privatevoidOnMessageReceived(objectsender,MessageReceivedEventArgse){Debug.Log($"Message received from {e.Client} -> {Encoding.UTF8.GetString(e.Data)}");}staticvoidHttpHandler(HttpListenerContextctx){HttpListenerRequestreq=ctx.Request;stringcontents=null;using(Streamstream=req.InputStream){using(StreamReaderreadStream=newStreamReader(stream,Encoding.UTF8)){contents=readStream.ReadToEnd();}}Debug.Log("Non-websocket request received for: "+req.HttpMethod.ToString()+" "+req.RawUrl);if(req.Headers!=null&&req.Headers.Count>0){Debug.Log("Headers:");varitems=req.Headers.AllKeys.SelectMany(req.Headers.GetValues,(k,v)=>new{key=k,value=v});foreach(variteminitems){Debug.Log($" {item.key}: {item.value}");}}if(!String.IsNullOrEmpty(contents)){Debug.Log("Request body:");Debug.Log(contents);}}privatevoidOnDestroy(){_server.Dispose();}}
Output when connecting:
I tried the test.server project and it worked. So this seems to be a unity related problem. Maybe somebody had similar experiences and found a solution.
The text was updated successfully, but these errors were encountered:
Did you ever get further on this? I found an issue, but I'm also stuck on this.
In WatsonWsServer.AcceptConnections, there's the following check: if (!ctx.Request.IsWebSocketRequest)
This always returns false. I changed it to the following: if( !ctx.Request.Headers["Connection"].Contains( "Upgrade" ) || !ctx.Request.Headers["Upgrade"].Contains( "websocket" ) )
which checks if the header "Connection" contains "Upgrade" and the header "Upgrade" contains "websocket" (two conditions for websocket requests).
I now get past the HTTP check.
Unforunately, it's now failing when trying to accept the web socket WebSocketContext wsContext = await ctx.AcceptWebSocketAsync( null );
This line of code (at line 492) just hangs. So I assume there's something else wrong with this code.
I'm currently using a C library (wrapped in C#) and I'm trying to switch to this library. I'm using the same javascript/websocket code for both. My C library accepts connections, while this library doesn't. I'm kinda stuck too.
Hello,
I want to implement a websocket server in unity with watson websocket. For this I built watson websocket for .Net Standard 2.1 and imported the dll to unity. Starting the websocket seems to work but as soon as I try to connect with a simple javascript client it says
non-websocket request
.Server:
Output when connecting:
I tried the
test.server
project and it worked. So this seems to be a unity related problem. Maybe somebody had similar experiences and found a solution.The text was updated successfully, but these errors were encountered: