Onclose is always called after onerror for WebSocket

Is it possible that a callback onerror can be raised where onclose will not be called right after? In other words, is it possible to get a WebSocket error that does not match a closed connection?

If possible, I would like to test this case. I have a laughed backend that uses node.js and express-ws. What can I do in the backend to trigger the onerror event callback in the interface.

+5
source share
1 answer

The error event will only be fired before it also fires the close event, at least with the help of implementations that properly execute the specification, i.e. you will get error and close as a pair or just close .

The process for closing websocket consists of 3 steps , the second of which, if necessary, triggers the error event:

  1. If the user agent needed a WebSocket connection failure or if the WebSocket connection was closed after it was marked as full, fire a simple event called error in the WebSocket object. [PVA]

Before the third step, close calls:

  1. Create a trusted event that uses the CloseEvent interface with the event type close ...

And since both of these steps are one after another inside the same task in the queue, your event handlers should be called one after another, without any other events or tasks occurring between them.

+6
source

Source: https://habr.com/ru/post/1258330/


All Articles