Olivine-Labs / Alchemy-Websockets

An extremely efficient C# WebSocket server.
http://AlchemyWebsockets.net
Other
309 stars 105 forks source link

Early disposal? Not on my watch! #72

Open Apelsin opened 11 years ago

Apelsin commented 11 years ago

This typo (I'm assuming it's a typo) was causing all kinds of malfeasance in WebSocketClient. This fixie is best viewed with whitespace ignored (git diff -w)

Apelsin commented 11 years ago

For your convenience:

C:\code\fb2\Alchemy-Websockets>git diff --cached -w HEAD~
diff --git a/src/Alchemy/WebSocketClient.cs b/src/Alchemy/WebSocketClient.cs
index 2472078..12ad613 100644
--- a/src/Alchemy/WebSocketClient.cs
+++ b/src/Alchemy/WebSocketClient.cs
@@ -148,8 +148,6 @@ namespace Alchemy
                 connectError = true;
             }

-            using (_context = new Context(null, _client))
-            {
             _context = new Context(null, _client);
             _context.BufferSize = 512;
             _context.UserContext.DataFrame = new DataFrame();
@@ -176,7 +174,6 @@ namespace Alchemy
                 NewClients.Enqueue(_context);
             }
         }
-        }
steforster commented 10 years ago

Without this fix, the c# WebSocketClient does not work at all (see issue #78). @segor has fixed it in his fork also. Additionally, the WebSocketClient throws a null-reference exception (on _context), when the socket cannot be opened. I fixed it in WebSocketClient.Disconnect() as follows:

@@ -348,10 +348,13 @@ namespace Alchemy

        var bytes = dataFrame.AsFrame()[0].Array;

-            ReadyState = ReadyStates.CLOSING;
+            if (_context != null && ReadyState == ReadyStates.OPEN)
+            {
+                ReadyState = ReadyStates.CLOSING;
+                bytes[0] = 0x88;
+                _context.UserContext.Send(bytes);
+            }

-            bytes[0] = 0x88;
-            _context.UserContext.Send(bytes);
             _client.Close();
            _client = null;
             ReadyState = ReadyStates.CLOSED;