Skip to content

Commit f428d60

Browse files
committed
fix: invalid payload
1 parent b028725 commit f428d60

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/SocketIOClient/Transport/WebSocketTransport.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public WebSocketTransport(IClientWebSocket ws, int eio)
1818
ConnectionTimeout = TimeSpan.FromSeconds(10);
1919
ReceiveWait = TimeSpan.FromSeconds(1);
2020
_listenCancellation = new CancellationTokenSource();
21+
_sendLock = new SemaphoreSlim(1, 1);
2122
}
2223

2324
public int ReceiveChunkSize { get; set; }
@@ -31,6 +32,7 @@ public WebSocketTransport(IClientWebSocket ws, int eio)
3132
readonly int _eio;
3233
readonly IClientWebSocket _ws;
3334
readonly CancellationTokenSource _listenCancellation;
35+
readonly SemaphoreSlim _sendLock;
3436

3537

3638
/// <exception cref="WebSocketException"></exception>
@@ -70,19 +72,27 @@ public async Task SendAsync(byte[] bytes, CancellationToken cancellationToken)
7072

7173
private async Task SendAsync(WebSocketMessageType type, byte[] bytes, CancellationToken cancellationToken)
7274
{
73-
int pages = (int)Math.Ceiling(bytes.Length * 1.0 / SendChunkSize);
74-
for (int i = 0; i < pages; i++)
75+
try
7576
{
76-
int offset = i * SendChunkSize;
77-
int length = SendChunkSize;
78-
if (offset + length > bytes.Length)
77+
await _sendLock.WaitAsync().ConfigureAwait(false);
78+
int pages = (int)Math.Ceiling(bytes.Length * 1.0 / SendChunkSize);
79+
for (int i = 0; i < pages; i++)
7980
{
80-
length = bytes.Length - offset;
81+
int offset = i * SendChunkSize;
82+
int length = SendChunkSize;
83+
if (offset + length > bytes.Length)
84+
{
85+
length = bytes.Length - offset;
86+
}
87+
byte[] subBuffer = new byte[length];
88+
Buffer.BlockCopy(bytes, offset, subBuffer, 0, subBuffer.Length);
89+
bool endOfMessage = pages - 1 == i;
90+
await _ws.SendAsync(new ArraySegment<byte>(subBuffer), type, endOfMessage, cancellationToken).ConfigureAwait(false);
8191
}
82-
byte[] subBuffer = new byte[length];
83-
Buffer.BlockCopy(bytes, offset, subBuffer, 0, subBuffer.Length);
84-
bool endOfMessage = pages - 1 == i;
85-
await _ws.SendAsync(new ArraySegment<byte>(subBuffer), type, endOfMessage, cancellationToken).ConfigureAwait(false);
92+
}
93+
finally
94+
{
95+
_sendLock.Release();
8696
}
8797
}
8898

0 commit comments

Comments
 (0)