divinelemon
divinelemon

Reputation: 2097

Is there an event for sending a message with WebSockets? (JS)

I want to check all of my sent websocket events to make sure that they are being sent.

For example:

websocket.on('send', (eventName, eventData) => {
    console.log('Event ' + eventName + 'emitted this data: \n' + eventData)
})

Is this possible?

Thanks

Upvotes: 0

Views: 239

Answers (1)

imaginate
imaginate

Reputation: 549

This isn't possible. However, you could inject a bit of Javascript before the page loads to do exactly what you need. I will use the Chrome extension tampermonkey, however, you could paste this is before the page loads and that could work if you do it quick enough.

See something like this:

// ==UserScript==
// @name         Websockets
// @namespace    https://<your site here>
// @version      1.0.0
// @description  Logs sent websocket events
// @author       videdivide
// @include      /https?:\/\/<YOUR SITE>\.com
// @grant        none
// ==/UserScript==


window.WebSocket = class extends window.WebSocket {
    constructor(url, proto){
        super(url, proto);
        this.addEventListener('message', event => {
            console.log('INCOMING:', event.data); // If you also want incoming messages
        });
    }
    send(data){
        console.log('OUTGOING:', data);
        super.send(data);
    }
}

Upvotes: 1

Related Questions