Seems to be that .readyState still TRUE even though connection is absolutely closed:
const wss = new WebSocket.Server({ port: 666 });
wss.on('connection', function connection(u) {
... etc etc
u.on('close', function incoming(m) {
handleFoundApparentDisconnection()
});
So the connection is totally closed...
Note that connections is just a set I manage myself in the usual way
var connections = [];
wss.on('connection', function connection(u) {
u.name = "john" etc
connections.push(u)
So the connection is totally closed
I then message all connections...
connections.forEach(function tell(c) {
if (c.readyState == WebSocket.OPEN)
c.send(.. some message)
});
In fact on that one .readyState == WebSocket.OPEN is still TRUE
i.e. it DOES try to do this
c.send(.. some message)
(giving an error/crash correctly)
how can it be that .readyState is still true when the connection is completely closed? 'closed' was indeed already called.
(Obviously, i can remove that connection from the list, but, that does not solve the problem. In a million other places one could be .send and check that the socket is open.)
how can it be that .readyState is still true when the connection is completely closed? 'closed' was indeed already called.
It can't be. Double check that the WebSocket that emitted the 'close' event and the one whose ready state is WebSocket.OPEN are the same WebSocket. This loop
connections.forEach(function tell(c) {
if (c.readyState == WebSocket.OPEN)
c.send(message)
})
does not give you WebSocket objects that already emitted the 'close' event.
Actually that loop could give closed WebSockets if you manage the connections set yourself but I find it hard to believe that the ready state of a WebSocket that emitted 'close' is changed back to WebSocket.OPEN. See https://github.com/websockets/ws/blob/d09daaf67c282e301eeebe21797215ddffd819c5/lib/websocket.js#L178-L192
@lpinca Thanks
• yes 'connections' is just a list that I manage myself (notice edit)
• I will surely triple check everything as you say.
However simply consider this code:
if (c.readyState == WebSocket.OPEN)
c.send(.. some message)
});
how can it ever crash on .send ?
right there in line 1 I am checking .readyState ... and then it crashes in line 2
What is the error message and stack? It's hard to tell without a test case to reproduce the issue.
The error message is just the usual
Error: WebSocket is not open: readyState 3 (CLOSED) at WebSocket.send (/home/ubuntu/node_modules/ws/lib/websocket.js:329:19)
To save your time please stand by while I triple check everything as you suggest !
Thanks! @lpinca
@lpinca here's an example:

Notice it works perfectly if you try the actual connection "u"
But if you look in the list of connections, it is still "3"
How can that be?
(I did the test with only one connection, so, it's definitely the same one)
I see nothing wrong in that snippet.
'close' event is emitted.'close' event take two arguments. The first is the code (a number), the second is the reason. So m in your example is the code and mrs is undefined because it's the value of the readyState property on a number. For exampleconst n = 1000; console.log(n.readyState).u object in the connections array and use that index to access the item in the array and log its readyState which is 3 or WebSocket.CLOSED as expected.The error is that m is the close code not and not a WebSocket object.
Ayy, (2) sorry, in the post I stupidly typed "m.readyState" rather than "u.readyState".
Regarding the issue at hand, I will close this issue.
I will carefully investigate and if I can repro I will alert
Thank you as always!