Websocket and arraybuffer lazarus / 76-Pas2JS ---------------------------------------------------------------------- dennis_uniwin · 2023-06-27 09:13 Hello, I started with the websocket demo and I am trying to make a websocket connection to our own software. So I have made sure that the websocket is created in the following way: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i         URL:='ws://'+aHost+':5001/ws/';    end;  try    WS:=TJSWebsocket.New(url,'csr');    ws.binaryType:='arraybuffer';    WS.onopen:=@DoOpen;    WS.onmessage:=@DoIncomingMessage;    WS.onclose:=@DoClosed;   The connection is made and I receive a message from out software. In the DoIncomingMessage event I try to handle the received data. This should be an ArrayBuffer. And I try to handle this in the following way. Only myarray remains an array with a length of 0. So obviously I am doing something wrong. Does someone know what it is I am doing wrong? And the next question how do I cast the byte array to a string? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  mystring: string;  myarray:TJSUint8Array;     begin     myarray := TJSUint8Array.new(TJSUint8Array.from(Msg.Data));    mystring := myarray.toLocaleString;    Writeln(mystring);   end; ---------------------------------------------------------------------- Thaddy · 2023-06-27 10:46 Since UTF8, UTF8 and UTF32 can at some point contain 4 bytes per character, your buffer needs to assume SizeOf(Byte) * 4 in all cases.... Basic... ---------------------------------------------------------------------- dennis_uniwin · 2023-06-27 13:18 I got it! I first had to make it an TJSObject and then a TJSUint8Array and then I can create a readable string finally. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i      myObject := toObject(Msg.Data);     myarray  := TJSUint8Array.new(myObject);         for i := 0 to myarray.byteLength - 1 do begin       mystring:= mystring + Char(myarray.values[i]);     end;      Writeln('myString ' + mystring);