E
E
Electrician Seven -Bar2015-06-20 19:24:48
Node.js
Electrician Seven -Bar, 2015-06-20 19:24:48

Why does the websocket server crash?

WebSocket server runs on nodejs. When I crashed, I noticed the following errors:

Error: not opened
    at WebSocket.send (/root/node/node_modules/ws/lib/WebSocket.js:214:16)
    at WebSocket.<anonymous> (/root/node/server.js:30:19)
    at WebSocket.emit (events.js:112:17)
    at Receiver.ontext (/root/node/node_modules/ws/lib/WebSocket.js:797:10)
    at /root/node/node_modules/ws/lib/Receiver.js:473:18
    at Receiver.applyExtensions (/root/node/node_modules/ws/lib/Receiver.js:360:5)
    at /root/node/node_modules/ws/lib/Receiver.js:462:14
    at Receiver.flush (/root/node/node_modules/ws/lib/Receiver.js:336:3)
    at Receiver.opcodes.1.finish (/root/node/node_modules/ws/lib/Receiver.js:478:12)
    at Receiver.expectHandler (/root/node/node_modules/ws/lib/Receiver.js:447:33)
/root/node/node_modules/ws/lib/WebSocket.js:214
    else throw new Error('not opened');
               ^
Error: not opened
    at WebSocket.send (/root/node/node_modules/ws/lib/WebSocket.js:214:16)
    at WebSocket.<anonymous> (/root/node/server.js:30:19)
    at WebSocket.emit (events.js:112:17)
    at Receiver.ontext (/root/node/node_modules/ws/lib/WebSocket.js:797:10)
    at /root/node/node_modules/ws/lib/Receiver.js:473:18
    at /root/node/node_modules/ws/lib/Receiver.js:357:7
    at /root/node/node_modules/ws/lib/PerMessageDeflate.js:217:5
    at afterWrite (_stream_writable.js:363:3)
    at onwrite (_stream_writable.js:354:7)
    at WritableState.onwrite (_stream_writable.js:107:5)

Found a similar question in English: stackoverflow.com/questions/24326413/node-js-webso...
Do I understand correctly that the crash is due to a socket attempting to send a message to an already disconnected client? Those. Do you need an additional check for connection activity before sending a message?
And this error occurs extremely infrequently and it has not yet been possible to catch (reproduce) the specific cause of the fall.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Shemsedinov, 2015-06-21
@Inkvizitor

The problem is that you don't check if the connection is alive before sending data to it. To find the problem, there is not enough stack output depth, add the key when starting the node node --stack-trace-limit=1000 application.js and it will be visible there and from what place in your code it is called. You need to catch ws.on('close', fn) and ws.on('error', fn) and remove these sockets from the set that you loop through and send, you can additionally put a check like if (connection.readyState ===WebSocket.OPEN) ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question