thoov / mock-socket

Javascript mocking library for WebSockets and Socket.IO
MIT License
800 stars 119 forks source link

Spec doesn't actually say that `send()` should throw when the socket is `CLOSING` or `CLOSED` #381

Closed steveluscher closed 1 year ago

steveluscher commented 1 year ago

This is actually somewhat controversial:

https://github.com/thoov/mock-socket/blob/eab4bf59ab83298ee42a21fc535a369432fe22c6/src/websocket.js#L141-L144

The long discussion at https://github.com/websockets/ws/issues/1515 convinced me that mock-socket shouldn't throw an exception here. Browsers don't actually throw when the socket is CLOSING or CLOSED; they simply print a red console message.

Try:

const socket = new WebSocket('wss://api.devnet.solana.com');
socket.close(1000);
try {
  socket.send({jsonrpc: '2.0', id: 1, method: 'ping'});
} catch (e) {
  // Despite the fact that the console shows red messages...
  console.error('this never gets hit', e);
}
steveluscher commented 1 year ago

Related, which is actually the opposite problem: #377.

Atrue commented 1 year ago

Release 9.3.0. Thanks @steveluscher