0

I have a working Python code that send/receive messages by websocket. I had copied the message (prepared to be sending):

\x00\x00U\xaa\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00x\xef\x80n\x14\x90\xdc2Qm9\xc0\xd0gTD\xd9\x9f%\xfa «w\xbd\xec\xf2\xec`\xf9\x84\x8cVFL\x16\xa6K\xd8\xe2\xbf$\x8b\xd5P\xa3\xbb\x0c\xe6\xaa\xd5\xd6\x81F\x96\xfd\xc7E\x88\x9b\xdf(\x87.\x1d\x00E\x9a%4XT\xa0\xf9d\xaf\n\xd2\xe3\xc6\x86\x02\xbc\x19V=\xd8\x8a[\xf7\xdb\xbdN\x1a\xbe\xee/\xc7\x10\xa3\xf3\x02\x016\x13g}\t=\xda\xdc~\xa9\xd0\x91\xc9\x0e\x93\x00\x00\xaaU

Now I try to send it from javascript. How I can send it? Cause it's string, but I need to send as raw bytes.

bguiz
  • 27,371
  • 47
  • 154
  • 243
Luss Sh
  • 21
  • 5

1 Answers1

1

Conversion to a typed array:

let data = '\x00\x00U\xaa\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00x\xef\x80n\x14\x90\xdc2Qm9\xc0\xd0gTD\xd9\x9f%\xfa «w\xbd\xec\xf2\xec`\xf9\x84\x8cVFL\x16\xa6K\xd8\xe2\xbf$\x8b\xd5P\xa3\xbb\x0c\xe6\xaa\xd5\xd6\x81F\x96\xfd\xc7E\x88\x9b\xdf(\x87.\x1d\x00E\x9a%4XT\xa0\xf9d\xaf\n\xd2\xe3\xc6\x86\x02\xbc\x19V=\xd8\x8a[\xf7\xdb\xbdN\x1a\xbe\xee/\xc7\x10\xa3\xf3\x02\x016\x13g}\t=\xda\xdc~\xa9\xd0\x91\xc9\x0e\x93\x00\x00\xaaU'
let typedArray = Uint8Array.from(data, e => e.charCodeAt(0) );

Here, e => e.charCodeAt(0) is used to convert '\x00' to 0 , 'U' to 85, etc.

And assuming you use a have a plain web socket object let socket,

socket.send(typedArray)
qrsngky
  • 2,263
  • 2
  • 13
  • 10