Skip to content

Commit bc5f529

Browse files
committed
Fix of WebSocket close status frame
1 parent c6d3cfa commit bc5f529

File tree

10 files changed

+13
-4
lines changed

10 files changed

+13
-4
lines changed

examples/WsChatServer/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public override void OnWsReceived(byte[] buffer, long offset, long size)
3434

3535
// If the buffer starts with '!' the disconnect the current session
3636
if (message == "!")
37-
Close(1000);
37+
Close();
3838
}
3939

4040
protected override void OnError(SocketError error)

examples/WssChatServer/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public override void OnWsReceived(byte[] buffer, long offset, long size)
3636

3737
// If the buffer starts with '!' the disconnect the current session
3838
if (message == "!")
39-
Close(1000);
39+
Close();
4040
}
4141

4242
protected override void OnError(SocketError error)

source/NetCoreServer/NetCoreServer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
5-
<Version>8.0.1.0</Version>
5+
<Version>8.0.2.0</Version>
66
<Authors>Ivan Shynkarenka</Authors>
77
<Copyright>Copyright (c) 2019-2023 Ivan Shynkarenka</Copyright>
88
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>

source/NetCoreServer/WebSocket.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ public bool PerformServerUpgrade(HttpRequest request, HttpResponse response)
257257
/// <param name="status">WebSocket status (default is 0)</param>
258258
public void PrepareSendFrame(byte opcode, bool mask, ReadOnlySpan<byte> buffer, int status = 0)
259259
{
260-
bool storeStatus = (opcode & WS_CLOSE) == WS_CLOSE;
260+
// Check if we need to store additional 2 bytes of close status frame
261+
bool storeStatus = ((opcode & WS_CLOSE) == WS_CLOSE) && ((buffer.Length > 0) || (status != 0));
261262
long size = storeStatus ? (buffer.Length + 2) : buffer.Length;
262263

263264
// Clear the previous WebSocket send buffer

source/NetCoreServer/WsClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ public class WsClient : HttpClient, IWebSocket
4545

4646
public override bool Connect() { _syncConnect = true; return base.Connect(); }
4747
public override bool ConnectAsync() { _syncConnect = false; return base.ConnectAsync(); }
48+
public virtual bool Close() => Close(0, Span<byte>.Empty);
4849
public virtual bool Close(int status) => Close(status, Span<byte>.Empty);
4950
public virtual bool Close(int status, string text) => Close(status, Encoding.UTF8.GetBytes(text));
5051
public virtual bool Close(int status, ReadOnlySpan<char> text) => Close(status, Encoding.UTF8.GetBytes(text.ToArray()));
5152
public virtual bool Close(int status, byte[] buffer) => Close(status, buffer.AsSpan());
5253
public virtual bool Close(int status, byte[] buffer, long offset, long size) => Close(status, buffer.AsSpan((int)offset, (int)size));
5354
public virtual bool Close(int status, ReadOnlySpan<byte> buffer) { SendClose(status, buffer); base.Disconnect(); return true; }
55+
public virtual bool CloseAsync() => CloseAsync(0, Span<byte>.Empty);
5456
public virtual bool CloseAsync(int status) => CloseAsync(status, Span<byte>.Empty);
5557
public virtual bool CloseAsync(int status, string text) => CloseAsync(status, Encoding.UTF8.GetBytes(text));
5658
public virtual bool CloseAsync(int status, ReadOnlySpan<char> text) => CloseAsync(status, Encoding.UTF8.GetBytes(text.ToArray()));

source/NetCoreServer/WsServer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class WsServer : HttpServer, IWebSocket
3737

3838
#region Session management
3939

40+
public virtual bool CloseAll() => CloseAll(0, Span<byte>.Empty);
4041
public virtual bool CloseAll(int status) => CloseAll(status, Span<byte>.Empty);
4142
public virtual bool CloseAll(int status, string text) => CloseAll(status, Encoding.UTF8.GetBytes(text));
4243
public virtual bool CloseAll(int status, ReadOnlySpan<char> text) => CloseAll(status, Encoding.UTF8.GetBytes(text.ToArray()));

source/NetCoreServer/WsSession.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class WsSession : HttpSession, IWebSocket
1919
public WsSession(WsServer server) : base(server) { WebSocket = new WebSocket(this); }
2020

2121
// WebSocket connection methods
22+
public virtual bool Close() => Close(0, Span<byte>.Empty);
2223
public virtual bool Close(int status) => Close(status, Span<byte>.Empty);
2324
public virtual bool Close(int status, string text) => Close(status, Encoding.UTF8.GetBytes(text));
2425
public virtual bool Close(int status, ReadOnlySpan<char> text) => Close(status, Encoding.UTF8.GetBytes(text.ToArray()));

source/NetCoreServer/WssClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ public class WssClient : HttpsClient, IWebSocket
4949

5050
public override bool Connect() { _syncConnect = true; return base.Connect(); }
5151
public override bool ConnectAsync() { _syncConnect = false; return base.ConnectAsync(); }
52+
public virtual bool Close() => Close(0, Span<byte>.Empty);
5253
public virtual bool Close(int status) => Close(status, Span<byte>.Empty);
5354
public virtual bool Close(int status, string text) => Close(status, Encoding.UTF8.GetBytes(text));
5455
public virtual bool Close(int status, ReadOnlySpan<char> text) => Close(status, Encoding.UTF8.GetBytes(text.ToArray()));
5556
public virtual bool Close(int status, byte[] buffer) => Close(status, buffer.AsSpan());
5657
public virtual bool Close(int status, byte[] buffer, long offset, long size) => Close(status, buffer.AsSpan((int)offset, (int)size));
5758
public virtual bool Close(int status, ReadOnlySpan<byte> buffer) { SendClose(status, buffer); base.Disconnect(); return true; }
59+
public virtual bool CloseAsync() => CloseAsync(0, Span<byte>.Empty);
5860
public virtual bool CloseAsync(int status) => CloseAsync(status, Span<byte>.Empty);
5961
public virtual bool CloseAsync(int status, string text) => CloseAsync(status, Encoding.UTF8.GetBytes(text));
6062
public virtual bool CloseAsync(int status, ReadOnlySpan<char> text) => CloseAsync(status, Encoding.UTF8.GetBytes(text.ToArray()));

source/NetCoreServer/WssServer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class WssServer : HttpsServer, IWebSocket
4141

4242
#region Session management
4343

44+
public virtual bool CloseAll() => CloseAll(0, Span<byte>.Empty);
4445
public virtual bool CloseAll(int status) => CloseAll(status, Span<byte>.Empty);
4546
public virtual bool CloseAll(int status, string text) => CloseAll(status, Encoding.UTF8.GetBytes(text));
4647
public virtual bool CloseAll(int status, ReadOnlySpan<char> text) => CloseAll(status, Encoding.UTF8.GetBytes(text.ToArray()));

source/NetCoreServer/WssSession.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class WssSession : HttpsSession, IWebSocket
1919
public WssSession(WssServer server) : base(server) { WebSocket = new WebSocket(this); }
2020

2121
// WebSocket connection methods
22+
public virtual bool Close() => Close(0, Span<byte>.Empty);
2223
public virtual bool Close(int status) => Close(status, Span<byte>.Empty);
2324
public virtual bool Close(int status, string text) => Close(status, Encoding.UTF8.GetBytes(text));
2425
public virtual bool Close(int status, ReadOnlySpan<char> text) => Close(status, Encoding.UTF8.GetBytes(text.ToArray()));

0 commit comments

Comments
 (0)