N
N
Nicholas2016-02-07 08:25:54
Node.js
Nicholas, 2016-02-07 08:25:54

How to release port in NodeJS after websocket disconnect?

How to release port in NodeJS after websocket disconnect?
WS module
https://www.npmjs.com/package/ws
I create a client on the server

var WebSocket = require('ws');

var ws = new WebSocket('ws://www.host.com:13310');

ws.on('open', function open() {
  ws.send('something');
});

ws.on('message', function(data, flags) {
  // flags.binary will be set if a binary data is received.
  // flags.masked will be set if the data was masked.
});



var adsadas = setTimeout(function() {

var ws = new WebSocket('ws://127.0.0.1:8778');

ws.on('open', function open() {
  ws.send('something');
});

ws.on('message', function(data, flags) {
  // flags.binary will be set if a binary data is received.
  // flags.masked will be set if the data was masked.
});

  }, 10000);

Successfully connected and close the socket server and open it on a new one, and try to connect to it on a new one, BUT! client (code above) throws an error:
events.js:154
      throw er; // Unhandled 'error' event
      ^

Error: connect ECONNREFUSED 127.0.0.1:8778
    at Object.exports._errnoException (util.js:856:11)
    at exports._exceptionWithHostPort (util.js:879:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1062:14)

They say the port is occupied by the first connection (but it is already closed), how can it be released for reassignment?
Ps I'm doing an auto-reconnect when the server crashes.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question