最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - WebSocket ws how to simulate error event? - Stack Overflow

programmeradmin5浏览0评论

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!

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!

Share Improve this question asked Mar 26, 2016 at 22:43 Jared SpragueJared Sprague 2494 silver badges13 bronze badges 2
  • 4 Couldn't you just emit it manually (e.g. ws.emit('error', new Error('foo'))) or pull the error event handler out and call it directly? – mscdex Commented Mar 26, 2016 at 23:00
  • 1 ws.emit('error') worked, thanks! – Jared Sprague Commented Mar 26, 2016 at 23:10
Add a ment  | 

1 Answer 1

Reset to default 8

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

发布评论

评论列表(0)

  1. 暂无评论