Forum > Pas2JS
Websocket and arraybuffer
(1/1)
dennis_uniwin:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- 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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var mystring: string; myarray:TJSUint8Array; begin myarray := TJSUint8Array.new(TJSUint8Array.from(Msg.Data)); mystring := myarray.toLocaleString; Writeln(mystring); end;
Thaddy:
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:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- 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);
Navigation
[0] Message Index