Skip to content

Commit

Permalink
Handling close events properly (#8)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
DenisBalan authored and doronguttman committed Jan 2, 2020
1 parent c91ba76 commit e2d2735
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 115 deletions.
2 changes: 1 addition & 1 deletion syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client/SockJS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ private async void ReceiveLoop(object obj)
var buffer = new byte[1024];
var segment = new ArraySegment<byte>(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;
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
6 changes: 0 additions & 6 deletions syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/App.config

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/packages.config

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,68 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{23265FD6-C038-4C36-9AD9-B7E5C26F876A}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>syp.biz.SockJS.NET.Test</RootNamespace>
<AssemblyName>syp.biz.SockJS.NET.Test</AssemblyName>
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>

<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>

<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
<ProjectReference Include="..\syp.biz.SockJS.NET.Client\syp.biz.SockJS.NET.Client.csproj" />
<ProjectReference Include="..\syp.biz.SockJS.NET.Common\syp.biz.SockJS.NET.Common.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\syp.biz.SockJS.NET.Client\syp.biz.SockJS.NET.Client.csproj">
<Project>{3409ed18-e8b6-43cc-bbb4-3dd617012d17}</Project>
<Name>syp.biz.SockJS.NET.Client</Name>
</ProjectReference>
<ProjectReference Include="..\syp.biz.SockJS.NET.Common\syp.biz.SockJS.NET.Common.csproj">
<Project>{8964A237-F8B2-4BB3-B3FB-B5B8A7DC42AB}</Project>
<Name>syp.biz.SockJS.NET.Common</Name>
</ProjectReference>
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

</Project>

0 comments on commit e2d2735

Please sign in to comment.