Jetty Web Socket Timeout

292 Views Asked by At

I am trying to establish a Web Socket Connection using Jetty 9.3.0 RC.

function checkDetails(port) {

    var ws = new WebSocket("ws://localhost:9995/application");

    ws.onopen = function(event) {
      console.log("onopen called...");
    }
    ws.onerror = function(event){
      console.log('onerror called...');
    }

    ws.onmessage = function(event) {
      console.log("onmessage called..." + event.data);
    }

    ws.onclose = function(event) {
      console.log("onclose called..." + port);
      console.log(event);
      ws.close();
    }
  }

The code works fine if the port 9995 used for creating the Web Socket connection is not occupied by some other process.

var ws = new WebSocket("ws://localhost:9995/application");

But in case if the port is occupied by some other process, then it keeps on trying to connect with that port until the port is released.

I need to provide a timeout so that if the port does not respond in 3 mins then the Web Socket should release (or stop listening) the port and display a console log.

Please let me know the simplest way to achieve this.

1

There are 1 best solutions below

0
On BEST ANSWER

From client side you are connecting to some web socket. If port (9995 in your case) is available to connect to then it means that some program (in server mode) is listening and responding. And does something - answers with some data. So, you can connect to such program if it exists and answers or you cannot if there is no server listener for port 9995. When you say "port is occupied" by some other process that means that this process exist and answers. And this process will respond with whatever it is designed for. So, from client side, all what you do is connect to existing and running process which listens for this port in the server mode. That's it and that's all.

However, if we ignore your comment that OP is only about client side then my first suggestion would be to look on the server configuration and check that it is in multithread mode and can answer and proceed multiple requests at once. What you are describing looks like you have singe thread process which works with only one request and can answer next one when current is finished. That sounds like "process occupied". But since comment insist that we are talking only about client side then this speculation would be unnecessary.