sta / websocket-sharp

A C# implementation of the WebSocket protocol client and server
http://sta.github.io/websocket-sharp
MIT License
5.7k stars 1.66k forks source link

socket closed when debugging Unity game using Visual Studio #259

Open tkluysk opened 8 years ago

tkluysk commented 8 years ago

We use a websocket to communicate between our Unity app (server) and a webapp. All works swell until we fire up the Visual Studio debugger ('Attach to Unity'). The websocket is summarily closed (1001), or becomes unreachable by the client.

All permutations of (re)loading app/VS debugger/Unity were tried to no avail. Tried a few different ports, too.

Any pointers welcome here, as we're back to stone age debugging!


Unity/C#:

            webSocketServerHTML = new WebSocketServer ( 50081 );
            webSocketServerHTML.AddWebSocketService<RemoteHTML> ( "/HTML" );
            webSocketServerHTML.Start ();

JavaScript: new WebSocket("ws://127.0.0.1:50081/HTML")

Error: WebSocket connection to 'ws://127.0.0.1:50081/HTML' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

sta commented 8 years ago

Hello there,

This issue seems to be same as issue #111.

So, is your Attach to Unity provided by UnityVS or later one?

tkluysk commented 8 years ago

No, we do not have UnityVS installed. Just the vanilla VS integration from Unity3D on Windows.

alvyxaz commented 8 years ago

@tkluysk Did you manage to work the issue out?

TapioRantala commented 8 years ago

I have both client and server running under Unity, and when attaching a debugger (either Visual Studio or MonoDevelop), existing connection is closed with status 1011 and new connection fails with status 1006. Tried patch from issue #96 but that didn't seem to change anything. Ideas?

TapioRantala commented 7 years ago

Looks like I've got this https://issuetracker.unity3d.com/issues/debug-running-project-with-attached-debugger-causes-socket-exception-if-socket-is-in-another-thread

tkluysk commented 7 years ago

@alvyxaz No, sorry.

TapioRantala commented 7 years ago

In my case only the server broke under debugger, and not the client. That inspired me to do some testing and resulted in the following patch. Websockets now works for me under a debugger.

commit 94ef945450e054b400284c636e2e2142f165d1ef
Author: Tapio Rantala <tapio.rantala@fragmentproduction.fi>
Date:   Wed Oct 26 15:57:15 2016 +0300

    Blocking on AcceptTcpClient() is broken, block on AsyncWaitHandle.WaitOne() instead
    In Unity under a debugger AcceptTcpClient gets interrupted by WSACancelBlockingCall
    Unity issue ID 780580:
    https://issuetracker.unity3d.com/issues/debug-running-project-with-attached-debugger-causes-socket-exception-if-socket-is-in-another-thread

diff --git a/websocket-sharp/Server/WebSocketServer.cs b/websocket-sharp/Server/WebSocketServer.cs
index 22f8c82..460cf22 100644
--- a/websocket-sharp/Server/WebSocketServer.cs
+++ b/websocket-sharp/Server/WebSocketServer.cs
@@ -762,7 +762,9 @@ namespace WebSocketSharp.Server
     {
       while (true) {
         try {
-          var cl = _listener.AcceptTcpClient ();
+          var asyncResult = _listener.BeginAcceptTcpClient (null, null);
+          asyncResult.AsyncWaitHandle.WaitOne ();
+          var cl = _listener.EndAcceptTcpClient (asyncResult);
           ThreadPool.QueueUserWorkItem (
             state => {
               try {
alvyxaz commented 7 years ago

@tazpa

Good catch!