faye/faye-websocket-node

How to handle it when ws.send return false?

Closed this issue · 6 comments

hi, guys,
I find that the ws.send API has return a value(true/false) in faye-webosocket-node source code, how should I deal with it? Could be part of the data sent to client(or put into TCP buffer)? How to confirm all the data was sent to client?

In our server side program, I tried to call ws.send again when ws.send return false, but the client would receive duplicated data sometimes, when I ignored the return false case, sometimes the client would lost data.

If send() returns false it means the socket is no longer open and none of the message will be sent. In this event you should open a new socket and use that to send your message.

@jcoglan
we use faye-websocket-node in a MMORPG server side, not the client, so cannot open a new ws. Maybe it is not safe to close the ws directly in the server side. We need to improve the connection robustness with client.
When the ws received 'message' event, is it sure the ws readyState > API.OPEN ?

In that case, you need to wait for the client to open another connection and send the message then, if appropriate.

If you receive a message event, the socket will be open at the point, but it may close between you receiving the message and calling send() with your reply.

@jcoglan thanks.
We'll try that.

@aceway Did this fix your problem? Should I close this issue?

@jcoglan yes, it work ok. When ws.send failed, we call ws.close to fire 'close' event, and then try to reconnet.
Thank you.