WebsocketV2


  • I am successfully fetch the websocket 2.0 like this
    but problem i am facing that , how to parse binary data in client side . Please help me with this..
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket Example</title>
    </head>
    <body>

    </body>
    <script>
    const clientCode = "***";
    const feedToken = "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VybmFtZSI6Ik41NDA3ODE0MiIsImlhdCI6MTcyNDA0NDAyNiwiZXhwIjoxNzI0MTMwNDI2fQ.V67xpYHh8RME2Qq7R3RGMUqAUI6Nyk4KA1nBQUcqYFPJdbRNoIE06O7wsrezY2xEg_cu3mQyO51QtvOeV-";
    const apiKey = "
    ";
    const websocketUrl = ws://smartapisocket.angelone.in/smart-stream?clientCode=${clientCode}&feedToken=${feedToken}&apiKey=${apiKey};
    console.log(websocketUrl);

    const ws = new WebSocket(websocketUrl);
    
    const payload = {
        "correlationID": "abcde12345",
        "action": 1,
        "params": {
            "mode": 3,
            "tokenList": [
                {
                    "exchangeType": 1,
                    "tokens": [
                        "99926000",
                    ]
                },
            ]
        }
    };
    
    ws.onopen = function() {
        console.log('Connected to WebSocket');
        ws.send(JSON.stringify(payload));
        console.log('Payload sent:', payload);
        setInterval(() => {
            if (ws.readyState === WebSocket.OPEN) {
                ws.send('ping');
                console.log('Heartbeat sent: ping');
            }
        }, 30000);
    };
    
    ws.onmessage = function(event) {
        if (event.data) {
    

    console.log(event);

            console.log(event.data);
            
            // Read the Blob as an ArrayBuffer
           
        } else {
            console.log('Unexpected data type:', typeof event.data);
        }
    
        if (event.data === 'pong') {
            console.log('Received heartbeat response: ' + event.data);
        }
    };
    
    ws.onerror = function(error) {
        console.error('WebSocket error:', error);
    };
    
    ws.onclose = function() {
        console.log('WebSocket connection closed');
    };
    

    </script>
    </html>