const WebSocket = require('ws'); // Assuming you've installed ws with npm install ws class YourClassName { constructor(clientId, accessToken) { this.clientId = clientId; this.accessToken = accessToken; this.ws = new WebSocket('ws://your_websocket_url'); // Replace with your WebSocket URL this.isAuthorized = false; } padWithZeros(buffer, length) { const padding = Buffer.alloc(length - buffer.length, 0); return Buffer.concat([buffer, padding], length); } async sendAuthorizationPacket() { let apiAccessToken = Buffer.from(this.accessToken, 'utf-8'); apiAccessToken = this.padWithZeros(apiAccessToken, 500); const authenticationType = Buffer.from("2P", 'utf-8'); const payload = Buffer.concat([apiAccessToken, authenticationType]); const feedRequestCode = 11; const messageLength = 83 + apiAccessToken.length + authenticationType.length; let clientId = Buffer.from(this.clientId, 'utf-8'); clientId = this.padWithZeros(clientId, 30); const dhanAuth = Buffer.alloc(50, 0); const header = Buffer.alloc(83); // Adjust the size based on your actual header structure header.writeInt8(feedRequestCode, 0); header.writeUInt16LE(messageLength, 1); clientId.copy(header, 3); dhanAuth.copy(header, 33); const authorizationPacket = Buffer.concat([header, payload]); // Send authorization packet this.ws.on('open', () => { this.ws.send(authorizationPacket); this.isAuthorized = true; console.log('Authorization packet sent'); }); // Handle messages from the server this.ws.on('message', function incoming(data) { console.log('Received:', data); }); // Handle any errors this.ws.on('error', function error(error) { console.error('WebSocket error:', error); }); } } // Usage const instance = new YourClassName('yourClientId', 'yourAccessToken'); instance.sendAuthorizationPacket();