Hi, I have been stuck to this problem for days now and looked up many StackOverflow posts and Discord channels but could not find a solution yet.
So what I am trying to do is getting some data from a WebSocket server and use the data, but the incoming data is ‘Buffer’ type that I have no experience handling.
My code for WebSocket is: “` const upBit: WebSocket | null = new WebSocket(
‘wss://api.upbit.com/websocket/v1’
);
upBit.binaryType = ‘arraybuffer’;
upBit.onopen = () => {
const message = [
{
ticket: ‘test’,
},
{ type: ‘ticker’, codes: ‘KRW-BTC’ },
];
upBit?.send(JSON.stringify(message));
};
upBit!.onmessage = (message) => { const decoder = new TextDecoder(‘utf-8’);
const data = new Uint8Array(message.data);
console.log(decoder.decode(data)); } “`
Some of you may be noticed, I am trying to get the price of BitCoin in Korean Won for real-time using an API that a Korean cryptocurrency trading website provides.
However, the incoming data is ‘Buffer’ type and I found that I found some working example code from Google, which uses Javascript, but I am using Typescript and I think that is the problem.
In the line const data = new Uint8Array(messasge.data); inside the onmessage, I am getting errors saying ‘Argument of type ‘Data’ is not assignable to parameter of type ‘ArrayBufferLike” and ‘Type ‘string’ is not assignable to type ‘ArrayBufferLike”.
I think I have tried almost everything that I could find Google for this, but the error persists. Can anyone please help me with this?
Thank you!
submitted by /u/auclown
[link] [comments]