NathanaelA / nativescript-websockets

Websockets for NativeScript
83 stars 43 forks source link

WebSocket closing after 1 minute, regardless of timeout setting #86

Closed vangola-silver closed 4 years ago

vangola-silver commented 4 years ago

Hi @NathanaelA ,

thanks for your library. I'm using it for my bachelor thesis project (developing an app for a ev charge station). Currently I'm running into some weird issue with the advanced websocket. It websocket is closing after one minute, regardless of timeout setting. I tried cyclically send ping messages from backend-server, the websocket responds automatically with a pong message, but it closes after 1 minute anyway.

Here is some sample code of using the advanced interface:

          
this._webSocket = new WS("ws://" + serverURL + "/ws", { protocols: [], timeout: 0, allowCellular: true, headers: {} });
this._webSocket.on('open', (socket) => this.onOpen(socket));
this._webSocket.on('message', (socket, message) => this.onMessage(socket, message));
this._webSocket.on('close', (socket, code, reason) => this.onClose(socket, code, reason));
this._webSocket.on('error', (socket, error) => this.onError(socket, error));
this._webSocket.open();

Your doc says that "timeout: 0" disables the timeout. But it did not help either. Am I missing something? I would appreciate any help on that issue!

NathanaelA commented 4 years ago
  1. You might need to check he server side. (You can try connecting to a public echo websocket server and see if it keeps the connection open, this will help you verify if it is your server or client causing the issues)
  2. If your app is suspending (or put into he background); then iOS and Android will close sockets.
vangola-silver commented 4 years ago

Hi @NathanaelA ,

thanks for the quick response. I dug deeper into the my server code and found the cause of the issue. The reason of this behavior was, that the ping message generated by the plugin has no content/payload. My backend server misinterpreted that and dropped the message. Since the plugin did not received any pong message, it closed the connection. So it was me misunderstanding the RFC regarding the ping-pong-mechanism. Sorry, for bothering you. I still have a questions though: Is it possible the set the payload of the ping-message?

NathanaelA commented 4 years ago

@vangola-silver - It has been a really long time since I looked at the code; but to my recollection it is not something you can change without recompiling the lower level android or ios native library.

vangola-silver commented 4 years ago

Hmm, ok then I will leave it for now. Thank you.