From e2d273585e187c1c4bc9037a6c86d49f617a57c1 Mon Sep 17 00:00:00 2001 From: Denis Balan <33955091+DenisBalan@users.noreply.github.com> Date: Thu, 2 Jan 2020 23:44:26 +0200 Subject: [PATCH] Handling close events properly (#8) * Add WebSocketMessageType.Close handling in WebSocketDriver.cs Fix typo in SockJS.cs Fire close event in WebSocketTransport.cs (as in original sockjs implementation) * Change test project to netstandard * Refactored Close method to fire events * Refactored Close method to fire events --- .../syp.biz.SockJS.NET.Client/SockJS.cs | 2 +- .../Transports/Lib/Driver/WebSocketDriver.cs | 21 +++--- .../Streaming/WebSocketTransport.cs | 1 + .../syp.biz.SockJS.NET.Test/App.config | 6 -- .../Properties/AssemblyInfo.cs | 36 ---------- .../syp.biz.SockJS.NET.Test/packages.config | 4 -- .../syp.biz.SockJS.NET.Test.csproj | 72 ++++--------------- 7 files changed, 27 insertions(+), 115 deletions(-) delete mode 100644 syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/App.config delete mode 100644 syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/Properties/AssemblyInfo.cs delete mode 100644 syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/packages.config diff --git a/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client/SockJS.cs b/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client/SockJS.cs index 1cb1246..bffca1b 100644 --- a/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client/SockJS.cs +++ b/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client/SockJS.cs @@ -250,7 +250,7 @@ private void Connect() // var transportObj = new Transport(transport.TransportName, transportUrl, this.TransportUrl, options); var transportObj = transport.Build(transport.TransportName, transportUrl, this.TransportUrl, options); transportObj.On("message", this.OnTransportMessage); - transportObj.Once("closed", this.TransportClose); + transportObj.Once("close", this.TransportClose); // transportObj.transportName = Transport.transportName; // moved to constructor this.Transport = transportObj; return; diff --git a/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client/Transports/Lib/Driver/WebSocketDriver.cs b/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client/Transports/Lib/Driver/WebSocketDriver.cs index 6a21b65..967501e 100644 --- a/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client/Transports/Lib/Driver/WebSocketDriver.cs +++ b/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client/Transports/Lib/Driver/WebSocketDriver.cs @@ -69,6 +69,11 @@ private async void ReceiveLoop(object obj) var buffer = new byte[1024]; var segment = new ArraySegment(buffer); var result = await this._socket.ReceiveAsync(segment, this._cancel.Token); + if (result.MessageType == WebSocketMessageType.Close) + { + await this.Close(WebSocketCloseStatus.NormalClosure, "Server sent close message"); + break; + } var data = Encoding.UTF8.GetString(buffer, 0, result.Count); builder.Append(data); if (!result.EndOfMessage) continue; @@ -111,15 +116,15 @@ private async Task Close(WebSocketCloseStatus status, string reason) { switch (this._socket.State) { - case WebSocketState.Aborted: - case WebSocketState.Closed: - case WebSocketState.CloseReceived: - case WebSocketState.CloseSent: - case WebSocketState.None: - return; + + case WebSocketState.Connecting: + case WebSocketState.Open: + await this._socket.CloseAsync(status, reason, this._cancel.Token); + this._cancel.Cancel(); + break; + } - await this._socket.CloseAsync(status, reason, this._cancel.Token); - this._cancel.Cancel(); + this.Emit("close", status, reason); } catch (Exception ex) { diff --git a/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client/Transports/Streaming/WebSocketTransport.cs b/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client/Transports/Streaming/WebSocketTransport.cs index 44bef68..77b8920 100644 --- a/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client/Transports/Streaming/WebSocketTransport.cs +++ b/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client/Transports/Streaming/WebSocketTransport.cs @@ -60,6 +60,7 @@ private void WebSocketOnClose(object sender, object[] e) var code = (int)e[0]; var reason = e[1] as string; Log.Debug($"{nameof(this.WebSocketOnClose)}: {code} {reason}"); + this.Emit("close", e); this.Cleanup(); } diff --git a/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/App.config b/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/App.config deleted file mode 100644 index 787dcbe..0000000 --- a/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/Properties/AssemblyInfo.cs b/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/Properties/AssemblyInfo.cs deleted file mode 100644 index 24eab44..0000000 --- a/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("StopTest")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("StopTest")] -[assembly: AssemblyCopyright("Copyright © 2018")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("23265fd6-c038-4c36-9ad9-b7e5c26f876a")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.4.0.0")] -[assembly: AssemblyFileVersion("1.4.0.0")] diff --git a/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/packages.config b/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/packages.config deleted file mode 100644 index c54e093..0000000 --- a/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/packages.config +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/syp.biz.SockJS.NET.Test.csproj b/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/syp.biz.SockJS.NET.Test.csproj index 4578462..0a4abf0 100644 --- a/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/syp.biz.SockJS.NET.Test.csproj +++ b/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/syp.biz.SockJS.NET.Test.csproj @@ -1,68 +1,20 @@ - - - + + - Debug - AnyCPU - {23265FD6-C038-4C36-9AD9-B7E5C26F876A} - Exe - syp.biz.SockJS.NET.Test - syp.biz.SockJS.NET.Test - v4.7.1 - 512 - true + netstandard2.0 - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - 7.1 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - 7.1 - - - - ..\..\..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll - - - - - - - - - - + - - + + - - + + + - - {3409ed18-e8b6-43cc-bbb4-3dd617012d17} - syp.biz.SockJS.NET.Client - - - {8964A237-F8B2-4BB3-B3FB-B5B8A7DC42AB} - syp.biz.SockJS.NET.Common - + - - \ No newline at end of file + +