Jared Sprague
Jared Sprague

Reputation: 249

WebSocket ws how to simulate error event?

I'm using the WebSocket library ws for node.js and I'm trying to simulate an error event on the server that would trigger my error handling code:

    ws.on('error', function(e) {
        console.log("error occured");
    });

I tried referencing an undefined variable in the on('message') event but that just crashed the whole server and the 'error' event never fired.

Can anyone tell me how to simulate a ws error event on the server?

Thank you!

Upvotes: 6

Views: 5477

Answers (1)

mscdex
mscdex

Reputation: 106698

Manually emitting the event should work (ws.emit('error', new Error('foo'))) as well as calling the error event handler directly (by pulling it out and naming it).

Upvotes: 8

Related Questions