0

I know this question was asked already, but the solutions provided didn't work for me.

Here is the websocket server code

const https = require('https'); const fs = require('fs'); const path = require('path'); const WebSocket = require('ws'); let connectionsList = []; /* */ var server = https.createServer({ cert: fs.readFileSync(path.resolve(__dirname, '../cert/certificate.crt')), key: fs.readFileSync(path.resolve(__dirname, '../cert/private.key')) }, function (request, response) { console.log((new Date()) + ' Received request for ' + request.url); response.writeHead(404); response.end(); }); wsServer = new WebSocket.Server({ server }); function originIsAllowed(origin) { // put logic here to detect whether the specified origin is allowed. return true; } wsServer.on('connection', function (connection) { //save new connections here connectionsList.push(connection); connection.on('message', function (message) { const data = JSON.parse(message) || null; if (data !== null && data.type === 'push') { connectionsList.forEach((connection, index) => { //Must Skip First Item This One Pumps Data To The Others if (index > 0) { if (connection.state === 'closed') { ConnectionsList.splice(index); } connection.send(JSON.stringify(data.message)); } }) } }); }); wsServer.on("error", function(err){ console.log(err); }); module.exports = server; 

Here is the runner or starter

// A simple pid lookup var exec = require('child_process').execFile; const wss = require('./ws_server/wss'); const config = require('./config'); var fun = function () { const process = exec(`${config.EXE.PATH}/${config.EXE.NAME}`, function () { wss.close(); fun(); }); //if process is created, then makea websocket server if (process.pid != null && process.pid !== undefined) { try{ wss.on('error', function(error){ console.log(error); }); wss.listen({port: config.PORT,host: config.HOST}, function () { console.log((new Date()) + ` Server is listening on port ${config.PORT}`); }); } catch(err){ } } } fun(); 

I keep having this error below even after I have checked and can't find anything using that port. I have tried all the approached mentioned from the here How to fix Error: listen EADDRINUSE while using nodejs?

but nothing seems to work for me, please can anyone explain to me what's really the problem here. I am using windows server to run this nodejs script. thanks

How to fix Error: listen EADDRINUSE while using nodejs?

6
  • Did you try netstat? Commented Nov 9, 2020 at 10:21
  • yes I did and I can't find any used IP based on that port Commented Nov 9, 2020 at 10:58
  • It seems that you are calling fun() multiple times cause the exec callback that trigger wss.listen again Commented Nov 9, 2020 at 11:07
  • okay, so how do i cause the program to continue executing? Commented Nov 9, 2020 at 11:11
  • What are you trying to archive? Commented Nov 9, 2020 at 11:24

2 Answers 2

1

The issue is that the close is not awaited since:

  • wss.close is called
  • fun is executed in sync and the wss.listen execute before the closing has been completed

It should be necessary to run fun in the close callback

 const process = exec(`${config.EXE.PATH}/${config.EXE.NAME}`, function () { wss.close(function(){ // now the server is closed fun(); }); }); 
Sign up to request clarification or add additional context in comments.

6 Comments

I didn't think of that
let me try that out
I followed the docs of ws. I don't know your exact version. You may verify that or user the close event
i did and no error yet. but i will keep watching to see if there's any. I appreciate
the error is back again. Error: listen EADDRINUSE: address already in use 0.0.0.0:8118
|
0

I found the problem. It was a guess. I updated the Node.js version in the computer and after that Node connected to the port and to the db successfully

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.