question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Websocket protocol for binary message

See original GitHub issue

Hi Guys, i’m trying to reproduce the protocol for Binary message to use with my custom websocket code in Xamarin since the SDK doesn’t work there.

below is the code i’m using to create the header

public struct WSHeader
    {
        public string Path;
        public string XRequestId;
        public string XTimestamp;
        public string ContentType;
    }

public static int GetHeaderSize(WSHeader header)
        {
            var str = "";
            str += header.Path;
            str += header.XRequestId;
            str += header.XTimestamp;
            str += header.ContentType;
            return str.Length;
        }

 public static WSHeader makeAudioHeaders(string type, string reqId)
        {
            var header = new WSHeader()
            {
                Path = $"path:{type}\r\n",
                XRequestId = $"x-requestid:{reqId}\r\n",
                XTimestamp = $"x-timestamp:{DateTime.UtcNow.ToString("o", System.Globalization.CultureInfo.InvariantCulture)}\r\n"
            };
            return header;
        }

Then i write to the stream giving the 2 byte header length value


public static void WriteHeaderToStream(Stream stream, string type, Func<WSHeader> makeHeader)
        {
            var header = makeHeader();
            Encoding encoding = type == "audio" ? Encoding.ASCII : Encoding.UTF8;
            using (var writer = new BinaryWriter(stream, encoding, true))
            {
                if (type == "audio")
                {
                    writer.Write($"{(short)GetHeaderSize(header)}\r\n"); // i also tried without '\r\n'
                    // writer.Write((short)GetHeaderSize(header)); // this doesn't work for some reason
                }

                writer.Write(encoding.GetBytes(header.Path));
                writer.Write(encoding.GetBytes(header.XRequestId));
                writer.Write(encoding.GetBytes(header.XTimestamp));
                writer.Write(encoding.GetBytes(header.ContentType));

                writer.Write(encoding.GetBytes("\r\n"));
            }
        }

The turn.start works and gives me the below (my console is printing the requestId all weird)

X-RequestId:6666666666664666A666666666666668
Content-Type:application/json; charset=utf-8
Path:turn.start

{
  "context": {
    "serviceTag": "9c3e3a3565f64cc8b0d099952e9546ab"
  }
}

After that, i get this error for subsequent requests, i think i’m not writing the header to the stream properly, i’m new to C#, could anyone help me please ?

[0:] Status Code InvalidPayloadData
[0:] Status Code Description Incorrect message format. Binary message has invalid header size

p.s. this is all spaghetti code for now until i get this working.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
rhureycommented, Aug 3, 2019

BinaryWriter.Write(Int16) is documented to write little endian.

https://docs.microsoft.com/en-us/dotnet/api/system.io.binarywriter.write

So it reversed the bytes on you. By writing only one byte you didn’t give it any choice.

A decent article on endianness: https://en.m.wikipedia.org/wiki/Endianness

0reactions
jhakulincommented, Aug 19, 2019

Closing this issue with the note that Xamarin support is being worked on currently and targeted to be available in the coming speech SDK release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Send and receive binary data over web sockets in ...
The WebSocket protocol allows to send arbitrary binary data (not even UTF-8 or Base-64 encoded) BUT that data are encapsulated in frames whose ......
Read more >
WebSocket: binaryType property - Web APIs - MDN Web Docs
WebSocket : binaryType property. The WebSocket.binaryType property controls the type of binary data being received over the WebSocket connection.
Read more >
Browser APIs and Protocols: WebSocket
WebSocket enables bidirectional, message-oriented streaming of text and binary data between client and server. It is the closest API to a raw network...
Read more >
17. WebSocket - High Performance Browser Networking ...
WebSocket enables bidirectional, message-oriented streaming of text and binary data between client and server. It is the closest API to a raw network...
Read more >
Processing Binary Protocols with Client-Side JavaScript
The two possible values for binaryType , of the WebSocket, are arraybuffer and blob . In most cases arraybuffer will be the one,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found