예제 #1
0
        private ValueTask SendTextAsync(string text, WebSocket websocket, bool disableCompression = false)
        {
            WebSocketMessageFlags flags = WebSocketMessageFlags.EndOfMessage;

            if (disableCompression)
            {
                flags |= WebSocketMessageFlags.DisableCompression;
            }
            byte[] bytes = Encoding.UTF8.GetBytes(text);
            return(websocket.SendAsync(bytes.AsMemory(), WebSocketMessageType.Text, flags, CancellationToken));
        }
        private static WebSocketMessageFlags GetMessageFlags(bool endOfMessage, bool disableCompression)
        {
            WebSocketMessageFlags messageFlags = WebSocketMessageFlags.None;

            if (endOfMessage)
            {
                messageFlags |= WebSocketMessageFlags.EndOfMessage;
            }

            if (disableCompression)
            {
                messageFlags |= WebSocketMessageFlags.DisableCompression;
            }

            return(messageFlags);
        }
예제 #3
0
        public async Task SendHelloWithDisableCompression()
        {
            WebSocketTestStream stream = new();

            using WebSocket websocket = WebSocket.CreateFromStream(stream.Remote, new WebSocketCreationOptions
            {
                IsServer = true,
                DangerousDeflateOptions = new WebSocketDeflateOptions()
            });

            byte[] bytes = "Hello" u8.ToArray();
            WebSocketMessageFlags flags = WebSocketMessageFlags.DisableCompression | WebSocketMessageFlags.EndOfMessage;
            await websocket.SendAsync(bytes, WebSocketMessageType.Text, flags, CancellationToken);

            Assert.Equal(bytes.Length + 2, stream.Available);
            Assert.True(stream.NextAvailableBytes.EndsWith(bytes));
        }
예제 #4
0
 public virtual ValueTask SendAsync(ReadOnlyMemory <byte> buffer, WebSocketMessageType messageType, WebSocketMessageFlags messageFlags, CancellationToken cancellationToken = default)
 {
     return(SendAsync(buffer, messageType, messageFlags.HasFlag(WebSocketMessageFlags.EndOfMessage), cancellationToken));
 }