Monday, 1 May 2017

node.js error upon client disconnect

https://i.stack.imgur.com/HWe4o.png I'm using node.js and I get the error in the picture every single time a client disconnects (This happens when I just close the tab and I've also tried using socket.disconnect() on both the client and the server side)

here's my code

var io = require('socket.io').listen(4000);
var fs = require('fs');

io.sockets.on('connection', function(socket) {
  socket.on('login', function(info)
  {
      fs.readFile("chatLog.txt", function(err, data)
      {
          if(err)
          {
              return console.error(err);
          }
          socket.emit('incomingMessage', data.toString());
      });
      socket.broadcast.emit('newUser', info);
  });
  socket.on('message', function(content) {
    console.log(content); 
    io.sockets.emit('incomingMessage', content);
  });
  socket.on('logout', function(name)
  {
     socket.broadcast.emit('incomingMessage', "user: " + name + " has logged out");
     //socket.disconnect();
  });
});

can anyone tell me how to disconnect without having the server crash with that error?



via Alex5775

No comments:

Post a Comment