2020年12月25日星期五

Sending data as binary via websockets encodes numbers as text

I'm working on a project where I'll be sending binary data across a websocket connection to an LED matrix. I want to send the binary data in the form of a byte array, e.g. drawing a diagonal line from right to left on the matrix would look like this:

0b00000001  0b00000010  0b00000100  0b00001000  0b00010000  0b00100000  0b01000000  0b10000000  

The problem that I run into when I try to do this is that when I try to send this data, regardless of the websocket client tester I use, the numbers get encoded as strings.

That is, when I try to send the binary number 1, instead of sending a 0x01, the socket client sends a 49 in decimal/0x31 in hex which is the character code for the string '1' in ascii or unicode.

enter image description here enter image description here

At first I thought this issue was on the arduino side in the code that's driving the matrix, but then I traced it back through the websocket server and all of the way to the testing clients. If I pop up wireshark and look at the data in flight from the client to the server, the encoding happens at the client, i.e. the first part of the websocket transmission.

wireshark analysis

I thought this may be me just using Firecamp wrong, but the same thing happens when using a different client, in this case websocat:

websocat test

So, my question is: what is the proper way of sending binary numbers via websockets? Am I just misunderstanding how the binary feature is supposed to work, like is it expected that I send my numbers as strings and convert them back to numbers on the other side?

https://stackoverflow.com/questions/65449143/sending-data-as-binary-via-websockets-encodes-numbers-as-text December 25, 2020 at 10:55PM

没有评论:

发表评论