tiagosiebler / binance

Node.js & JavaScript SDK for Binance REST APIs & WebSockets, with TypeScript & browser support, integration tests, beautification & more.
MIT License
728 stars 264 forks source link

browser ping issue #405

Closed Tindtily closed 4 months ago

Tindtily commented 4 months ago

https://github.com/tiagosiebler/binance/blob/2bb5172782e11af60ce0eb704301a524043542ca/src/websocket-client.ts#L300

for browser, i replaced

ws.ping();
ws.pong();

with the follow code

            if(window){
                function getBuffer(bufferSuffix){
                    const ab = new ArrayBuffer(5);  
                    const view = new Uint8Array(ab);
                    view[0] = bufferSuffix; 
                    view[1] = 0x05;
                    view[2] = 0x00;  

                    view[3] = 0x00;
                    view[4] = 0x00;  
                    return ab
                }
                const pingBuffer = getBuffer(0x089);
                const pongBuffer = getBuffer(0x08a);

                // ping
               ws.send(pingBuffer);
               // pong
               ws.send(pongBuffer);
             } else {
               ws.ping();
               ws.pong();
             }

the ping and pong works, but the socket connection break and reconnected every 10000ms(options.pingInterval)

Tindtily commented 4 months ago

Is this a normal phenomenon?

Tindtily commented 4 months ago
image
tiagosiebler commented 4 months ago

Oh interesting, you've successfully managed to send raw ping/pong frames? I hadn't considered just sending them as hex for some reason. How come you've closed this? Did you get it working?

Tindtily commented 4 months ago

Oh interesting, you've successfully managed to send raw ping/pong frames? I hadn't considered just sending them as hex for some reason. How come you've closed this? Did you get it working?

The chrome process ping and pong natively,and no need to process them manually. So you can consider that the related code in your project can be removed when used in browser runtime.

Since I no longer need to do this manually, I closed the issue