Skip to content

Commit 50cddbb

Browse files
committed
Fix WebSocket not work problem.
1 parent 161bb33 commit 50cddbb

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

Quick.Protocol.WebSocket.Client/WebSocketClientStream.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,27 @@ public override int Read(byte[] buffer, int offset, int count)
4949
return result.Count;
5050
}
5151

52+
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
53+
{
54+
var result = await client.ReceiveAsync(new ArraySegment<byte>(buffer, offset, count), CancellationToken.None);
55+
return result.Count;
56+
}
57+
5258
public override void Write(byte[] buffer, int offset, int count)
5359
{
5460
if (closeReason != null)
5561
throw new IOException(closeReason);
56-
client.SendAsync(new ArraySegment<byte>(buffer, offset, count), System.Net.WebSockets.WebSocketMessageType.Binary, true, CancellationToken.None);
62+
63+
client.SendAsync(
64+
new ArraySegment<byte>(buffer, offset, count),
65+
System.Net.WebSockets.WebSocketMessageType.Binary,
66+
true,
67+
CancellationToken.None).Wait();
68+
}
69+
70+
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
71+
{
72+
return client.SendAsync(new ArraySegment<byte>(buffer, offset, count), System.Net.WebSockets.WebSocketMessageType.Binary, true, CancellationToken.None);
5773
}
5874

5975
protected override void Dispose(bool disposing)

Quick.Protocol.WebSocket.Server.AspNetCore/QuickProtocolMiddlewareExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.AspNetCore.Http;
22
using Newtonsoft.Json;
3+
using Quick.Protocol;
34
using Quick.Protocol.WebSocket.Server.AspNetCore;
45
using System;
56
using System.Collections.Generic;
@@ -28,14 +29,15 @@ public static IApplicationBuilder UseQuickProtocol(this IApplicationBuilder app,
2829
}
2930
else
3031
{
32+
var qpLibVersion = typeof(QpChannel).Assembly.GetName().Version;
3133
var message = $@"
3234
<html>
3335
<head>
3436
<title>Quick.Protocol</title>
3537
</head>
3638
<body>
37-
<p>Welcome to use <b>Quick.Protocol</b>.</p>
38-
<p>Source Code:<a href=""http://github.com/aaasoft/Quick.Protocol"">http://github.com/aaasoft/Quick.Protocol</a></p>
39+
<p>Welcome to use <b>Quick.Protocol {qpLibVersion.ToString(3)}</b></p>
40+
<p>Source Code:<a href=""https://github.com/QuickProtocol/"">https://github.com/QuickProtocol/</a></p>
3941
<p>ServerProgram:{string.Join(" | ", options.InstructionSet.Select(t => $"{t.Name}({t.Id})"))}</p>
4042
<p>InstructionSet:{DateTime.Now}</p>
4143
<p>MaxPackageSize:{options.MaxPackageSize}</p>

Quick.Protocol.WebSocket.Server.AspNetCore/WebSocketServerStream.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal class WebSocketServerStream : Stream
1212
{
1313
private System.Net.WebSockets.WebSocket webSocket;
1414
private CancellationToken cancellationToken;
15-
15+
1616
public WebSocketServerStream(System.Net.WebSockets.WebSocket webSocket, CancellationToken cancellationToken)
1717
{
1818
this.webSocket = webSocket;
@@ -37,6 +37,11 @@ public override int Read(byte[] buffer, int offset, int count)
3737
return result.Count;
3838
}
3939

40+
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
41+
{
42+
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer, offset, count), cancellationToken);
43+
return result.Count;
44+
}
4045

4146
public override void Write(byte[] buffer, int offset, int count)
4247
{
@@ -48,6 +53,16 @@ public override void Write(byte[] buffer, int offset, int count)
4853
.Wait();
4954
}
5055

56+
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
57+
{
58+
return webSocket.SendAsync(
59+
new ArraySegment<byte>(buffer, offset, count),
60+
System.Net.WebSockets.WebSocketMessageType.Binary,
61+
true,
62+
cancellationToken);
63+
}
64+
65+
5166
protected override void Dispose(bool disposing)
5267
{
5368
webSocket.Dispose();

0 commit comments

Comments
 (0)