becvert/cordova-plugin-websocket-server

wsserver still open after closing the app?

christophsaile opened this issue · 6 comments

Hello :),

just recently I noticed an issue with closing the wsserver.
If I do the following steps I get an error:

  1. start app
  2. open a server
  3. close the app with the taskmanager (android)
  4. call restartServer function (see below)
    -> "stopped listening on :::9090. Reason: Address already in use"

This bug only happens if I close the app, reopen the app and call the restartServer function in less then ~ 1-3 min.
My wsserver.start - function is nested in the startUpServer function and is the same as in the docs.

Is it possible that the server is somehow still open, even though I closed the app with the taskmanager?
Thanks in advance

restartServer function:

public restartServer = (_port: number): void => {
    this.connectedClientsCollection = [];
    this.wsserver.stop(
      function onStop(addr: string, _port: number) {
        console.log('Stopped listening on %s:%d', addr, _port);
      },
      function onFailure() {
        console.log('failure');
      }
    );
    this.startUpServer(_port);
  };

Console.log of the error

console

Hello,
stop is async.
shouldn't you wait for the callback before starting it up again ?

function onStop(addr: string, _port: number) {
        startUpServer(_port);
}

Hey,
thanks for your answer. Yes you are right with the callback.
I tested your solution but the problem still exists.

  • If I open the server
  • close the app
  • open the app again (taskmanager on android)
  • open the server again (you have to do this fast to reproduce the bug)

I get the following error:
"Stopped listening on :::8000. Reason: Address already in use"

I created a test repo to show you the bug:
https://github.com/christophsaile/websocket-issue-test

I think the problem is:
if you reopen the app, the success callback of the wsserver.stop function doens´t get triggert because there is no port open, but if you then call the wsserver.start function in the failure callback, you get the error message from above. This means that the port is somehow still not closed?

Thanks in advance :)

Found a potential issue: Port stays bound after calling WebSocketServer.close.

Also, TooTallNate's server does not seem to do anything to unbind its port:
wsf.close()
//Nothing to do for a normal ws factory

Are you able to investigate those 2 points please?

Yes I can have a look on it,
but it will probably take some time.
Currently this bug has not top prio on my study project. I need to fix some other stuff first :)

I am facing the same problem. Somebody help me!

I solved the problem. net/becvert/cordova/WebSocketServerPlugin.java in node_modules.
Add
At line 157
newserver.setReuseAddr(true)

And save then npx cap sync
It works fine !!