The long discussion at https://github.com/websockets/ws/issues/1515 convinced me that mock-socketshouldn'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);
}
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 isCLOSING
orCLOSED
; they simply print a red console message.Try: