Websocket protocol for binary message
See original GitHub issueHi 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:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
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.