Search code examples
c#.netwebsocketclientwebsocket

WebSocketReceiveResult force to receive binary


I am using Websocket.Client which is a wrapper around ClientWebSocket, within its internals I can see WebSocketReceiveResult and it is using it to detect the message contents result.MessageType == WebSocketMessageType.Text.

Is there any way to force a web socker to only to receive binary messages (not do any conversion to string?), or is the data format specified by the server?

namespace Websocket.Client
{
    //
    // Summary:
    //     Received message, could be Text or Binary
    public class ResponseMessage
    {
        //
        // Summary:
        //     Received text message (only if type = WebSocketMessageType.Text)
        public string Text { get; }
        //
        // Summary:
        //     Received text message (only if type = WebSocketMessageType.Binary)
        public byte[] Binary { get; }
        //
        // Summary:
        //     Current message type (Text or Binary)
        public WebSocketMessageType MessageType { get; }

        //
        // Summary:
        //     Create binary response message
        public static ResponseMessage BinaryMessage(byte[] data);
        //
        // Summary:
        //     Create text response message
        public static ResponseMessage TextMessage(string data);
        //
        // Summary:
        //     Return string info about the message
        public override string ToString();
    }
}

Solution

  • This feature was implemented by the author of Websocket.Client within a few hours of posting on the github page... amazing!

    using var client = new WebsocketClient()
    {
       IsTextMessageConversionEnabled = false
    };