3

I tried to build a chat box server by node.js. When the browser requestes the page, it workes well at first. But when I refresh the page, the Server crashes.

Below is the error message:

events.js:183
  throw er; // Unhandled 'error' event
  ^

Error: read ECONNRESET
    at _errnoException (util.js:1022:11)
    at TCP.onread (net.js:615:25)

I used the node --inspect index.js, but could not find the point.

Below is the code of index.js:

const http = require('http');
const fs = require('fs');
const extract = require('./extract');
const wss = require('./websockets-server');

var handleError = function (err,res) {
  res.writeHead(404);
  res.end();
}

var server = http.createServer(function (req, res) {
  console.log("Responding to a request.");
  var filePath = extract(req.url);
  console.log("filePath:"+filePath);
  fs.readFile(filePath,function (err,data) {
    if(err){
      handleError(err,res);
      return;
    }else {
      res.end(data);
    }
  })
})

server.listen(3000);

When I comment the 4th line, the import of websockets-server. Server works well when I refresh the page. Maybe it's about the websocket while it works without websocket.

Below is code of websockets-server.js:

const WebSocket = require('ws');
var WebSocketServer = WebSocket.Server;
var port = 3001;
var ws = new WebSocketServer({
  port:port
});

var message = [];

console.log('websockets server started');

ws.on('connection', function (socket) {
  console.log('client connection established');

  message.forEach(function (msg) {
    socket.send(msg);
  })

  socket.on('message', function (data) {
    console.log('message received: ' + data);
    message.push(data);
    ws.clients.forEach(function (clientSocket) {
      clientSocket.send(data);
    });
  });
});

Does the problem is about the websocket? Whether should I do process when the client shutdown the connection with the server while refreshing the page.

extract.js below:

const path = require('path');

var extractFilePath = function (url) {
  var filePath;
  var fileName = 'index.html';

  if(url.length > 1){
    fileName = url.substring(1);
  }
  console.log('The fileName is: ' + fileName);

  filePath = path.resolve(__dirname, 'app', fileName);
  return filePath;
}
module.exports = extractFilePath;
Zobia Kanwal
  • 4,085
  • 4
  • 15
  • 38
周雪静
  • 153
  • 1
  • 3
  • 10

4 Answers4

3

I guess that you maybe execute var ws = new WebSocket("ws://localhost:3001"); in html file. I haven't figured out exact reason about your error as I'm not proficient in WebSocket. But there is a solution:

window.onbeforeunload = function () {
    ws.close();
}

close connection before reload, then the error will not reappear.

SHUMING LU
  • 129
  • 9
2

You need to add an error listener on the socket. Error listener only on the websocket instance does not help in this case.


    socket.on('error', function(e){
        console.log(e);
    });

imran
  • 88
  • 10
1

The ECONNRESET error means that the other side (browser) closed the connection abruptly. On browser refresh, browser simple killed the connection with the websocket server.

To solve this, you have to listen for the error event on the websocket server instance.

// listen for "error" event so that the whole app doesn't crash
wss.on("error", function(error){
    console.log(error);
}
Usama Ejaz
  • 1,105
  • 10
  • 20
  • Can you give more details about what was the outcome by doing this? In your websockets-server.js, right before the line `ws.on('connection', function (socket) {`, have you tried using the code to listen for the "error" event? You will use `ws` instead of `wss` (if thats the case). – Usama Ejaz Jan 13 '18 at 08:46
0

I was having the same problem, but it resolved after this command:

npm install @ionic/app-scripts@nightly --save-dev
Andrew Myers
  • 2,754
  • 5
  • 32
  • 40