Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 462
Jared Sprague Avatar asked Mar 26 '16 22:03

Jared Sprague


1 Answers

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).

like image 81
mscdex Avatar answered Sep 28 '22 06:09

mscdex