I have a project in which I need to transmit binary data over a websocket connection to an LED matrix. My plan is to send the binary data as a byte array, for example, creating a diagonal line from right to left on the matrix would be represented like this:
0b00000001
0b00000010
0b00000100
0b00001000
0b00010000
0b00100000
0b01000000
0b10000000
However, when attempting to send this data, regardless of the WebSocket client tool I use, the numbers are being encoded as strings.
For instance, when attempting to send the binary number 1
, instead of transmitting 0x01
, the socket client is sending 49
in decimal/0x31
in hex, which represents the character code for the string '1'
in ASCII or Unicode.
This encoding issue seems to occur at the client side during websocket transmission, rather than being related to the Arduino code controlling the matrix or the server itself, as confirmed by monitoring with Wireshark.
The same problem persists whether using Firecamp or another client such as websocat
:
Given these challenges, my question is: what is the correct method for sending binary numbers via websockets? Could it be that I am misunderstanding how the binary feature operates, and should I be sending numbers as strings and converting them back at the receiving end?