becvert/cordova-plugin-websocket-server

webserver does not start? permission denied

christophsaile opened this issue · 2 comments

Hey,

I just found your plugin and want to use it for my project.
The only problem is it somehow does not work for me :/

I copy pasted the example code you posted in the usage section but everytime I run the cordova app and want to start the webserver I get the following message:

"stopped listening on :::8080. Reason: Permission denied"

I have already tried the following things

  • remove the "other options"
  • remove all options
  • tried different ports 8888, 8080, 5000, 12345 ....

Somehow I am unable to get the addresse which is probably the reason the success callback is not fired :/
Do you have any Idea why this happens?

document.addEventListener("deviceready", function () {
  initNetworkFunctions();
});

function initNetworkFunctions() {
  const wsserver = cordova.plugins.wsserver;
  const port = 8080;
  startWebserverConnection(wsserver, port);
}

function startWebserverConnection(wsserver, port) {
  document
    .querySelector(".button__startServer")
    .addEventListener("click", function () {
      wsserver.start(
        port,
        {
          // WebSocket Server handlers
          onFailure: function (addr, port, reason) {
            console.log(
              "Stopped listening on %s:%d. Reason: %s",
              addr,
              port,
              reason
            );
          },
          // WebSocket Connection handlers
          onOpen: function (conn) {
            /* conn: {
             'uuid' : '8e176b14-a1af-70a7-3e3d-8b341977a16e',
             'remoteAddr' : '192.168.1.10',
             'httpFields' : {...},
             'resource' : '/?param1=value1&param2=value2'
             } */
            console.log("A user connected from %s", conn.remoteAddr);
          },
          onMessage: function (conn, msg) {
            console.log(conn, msg); // msg can be a String (text message) or ArrayBuffer (binary message)
          },
          onClose: function (conn, code, reason, wasClean) {
            console.log("A user disconnected from %s", conn.remoteAddr);
          },
        },
        function onStart(addr, port) {
          console.log("Listening on %s:%d", addr, port);
        },
        function onDidNotStart(reason) {
          console.log("Did not start. Reason: %s", reason);
        }
      );
    });
}

Thanks in advance,

Christoph

Hi
Are you testing on an Android device ?
Check for the INTERNET and ACCESS_WIFI_STATE permissions in your manifest ?

Hi @becvert,
many thanks for your quick answer!!
now it works :)