Description
- I've searched for any related issues and avoided creating a duplicate
issue.
Description
For compatibility with browser implementations, the HTML standard, and RFC 6455, calling send
on a WebSocket
whose readyState
is CLOSING
or CLOSED
should discard the data and not throw an exception. (As a QOL issue, it may log an error to the console.)
Currently it throws an exception such as "WebSocket is not open: readyState 2 (CLOSING)".
Reproducible in:
- version: 5.2.2
- Node.js version(s): v10.14.1
- OS version(s): macOS 10.14.3
Steps to reproduce:
-
Start a WebSocket server on port 9000.
-
Run the following in node:
const WebSocket = require("ws");
const ws = new WebSocket("ws://localhost:9000");
ws.onopen = () => {
ws.close();
ws.send("test");
console.log("sent");
};
- Open the following in browsers:
<script>
const ws = new WebSocket("ws://localhost:9000");
ws.onopen = () => {
ws.close();
ws.send("test");
console.log("sent");
};
</script>
Expected result:
All result in "sent" being printed to the console.
Actual result:
- Node prints
Error: WebSocket is not open: readyState 2 (CLOSING)
, with an exception stack trace pointing towebsocket.js:320
. - Chrome prints the error diagnostic "WebSocket is already in CLOSING or CLOSED state.", but does not throw an exception ("sent" is printed).
- Firefox prints "sent" and no error.
- Safari prints "sent" and no error.
References:
The send(data) method transmits data using the connection. If the readyState attribute is CONNECTING, it must throw an "InvalidStateError" DOMException. Otherwise, the user agent must run the appropriate set of steps from the following list:
Note that an exception must be thrown only in the CONNECTING
state, and the "following list" does not include any steps that involve throwing an exception.
The endpoint MUST ensure the WebSocket connection is in the OPEN state (cf. Sections 4.1 and 4.2.2.) If at any point the state of the WebSocket connection changes, the endpoint MUST abort the following steps.
Note "abort the following steps"; i.e. stop processing, but do not raise an exception.