Is there a way to efficiently gather all WebSockets on the current page in order to easily send messages through them? I've been attempting the following:
const prototype = await page.evaluateHandle("WebSocket.prototype");
var socketInstances = await page.queryObjects(prototype);
await page.evaluate((instances:Array<WebSocket>) => {
instances[0].send("message");
}, socketInstances);
const cdp = await page.createCDPSession();
await cdp.send('Network.enable');
await cdp.send('Page.enable');
cdp.on("Network.webSocketFrameReceived", response => console.log(response));
cdp.on('Network.webSocketFrameSent', response => console.log(response));
While this allows me to monitor received WebSocket messages, I'm struggling to find a more efficient way to send messages through these sockets without having to call page.evaluate each time. I attempted to reference the WebSockets like so:
await cdp.send('Runtime.enable');
const { exceptionDetails, result: remoteObject } = await cdp.send('Runtime.evaluate', {
expression: 'WebSocket'
});
await cdp.send("Runtime.queryObjects", {prototypeObjectId: remoteObject.objectId});
However, this approach doesn't seem to work as desired. Does anyone have any thoughts on a simpler or alternate method to achieve this?