Monday, 29 May 2017

Socket connection closed automatically on heroku with nodejs

I am working on an IoT project. With nodejs on heroku, i have created a server on heroku. Then my IoT device will try to establish a connection. But when i tried it, the connection automatically closes. Below is the script i am using. It is working locally.

net = require('net');
var clients = [];
net.createServer(function (socket) {
  socket.name = socket.remoteAddress + ":" + socket.remotePort
  clients.push(socket);
  broadcast(socket.name + " joined the chat\n", socket);
  socket.on('data', function (data) {
    broadcast(socket.name + "> " + data, socket);
  });
  socket.on('end', function () {
    clients.splice(clients.indexOf(socket), 1);
    broadcast(socket.name + " left the chat.\n");
  });
  function broadcast(message, sender) {
   clients.forEach(function (client) {
      if (client === sender) return;
      client.write(message);
    });
    process.stdout.write(message)
  }
}).listen(process.env.PORT || 8899);
console.log("Chat server running at port 8899\n");

Please help.



via Nikhildas C L

No comments:

Post a Comment