1
1
using System . Buffers ;
2
2
using System . Net . WebSockets ;
3
+ using System . Text ;
3
4
using System . Text . Json ;
4
5
using Microsoft . IO ;
5
6
@@ -8,7 +9,7 @@ namespace OpenShock.SDK.CSharp.Live.Utils;
8
9
public static class JsonWebSocketUtils
9
10
{
10
11
private const uint MaxMessageSize = 512_000 ; // 512 000 bytes
11
-
12
+
12
13
public static readonly RecyclableMemoryStreamManager RecyclableMemory = new ( ) ;
13
14
14
15
public static async Task < OneOf . OneOf < T ? , DeserializeFailed , WebsocketClosure > > ReceiveFullMessageAsyncNonAlloc < T > (
@@ -32,7 +33,7 @@ await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closure during mess
32
33
}
33
34
34
35
if ( buffer . Length + result . Count > MaxMessageSize ) throw new MessageTooLongException ( ) ;
35
-
36
+
36
37
message . Write ( buffer , 0 , result . Count ) ;
37
38
} while ( ! result . EndOfMessage ) ;
38
39
@@ -42,7 +43,11 @@ await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closure during mess
42
43
}
43
44
catch ( Exception e )
44
45
{
45
- return new DeserializeFailed { Exception = e } ;
46
+ return new DeserializeFailed
47
+ {
48
+ Message = Encoding . UTF8 . GetString ( message . GetBuffer ( ) . AsSpan ( 0 , bytes ) ) ,
49
+ Exception = e
50
+ } ;
46
51
}
47
52
}
48
53
finally
@@ -51,10 +56,12 @@ await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closure during mess
51
56
}
52
57
}
53
58
54
- public static Task SendFullMessage < T > ( T obj , WebSocket socket , CancellationToken cancelToken , JsonSerializerOptions jsonSerializerOptions , int maxChunkSize = 256 ) =>
59
+ public static Task SendFullMessage < T > ( T obj , WebSocket socket , CancellationToken cancelToken ,
60
+ JsonSerializerOptions jsonSerializerOptions , int maxChunkSize = 256 ) =>
55
61
SendFullMessageBytes ( JsonSerializer . SerializeToUtf8Bytes ( obj , jsonSerializerOptions ) , socket , cancelToken ) ;
56
62
57
- public static async Task SendFullMessageBytes ( byte [ ] msg , WebSocket socket , CancellationToken cancelToken , int maxChunkSize = 256 )
63
+ public static async Task SendFullMessageBytes ( byte [ ] msg , WebSocket socket , CancellationToken cancelToken ,
64
+ int maxChunkSize = 256 )
58
65
{
59
66
var doneBytes = 0 ;
60
67
@@ -74,6 +81,7 @@ public static async Task SendFullMessageBytes(byte[] msg, WebSocket socket, Canc
74
81
/// </summary>
75
82
public readonly struct DeserializeFailed
76
83
{
84
+ public required string Message { get ; init ; }
77
85
public required Exception Exception { get ; init ; }
78
86
}
79
87
0 commit comments