B
B
bizir2015-07-30 12:49:35
Node.js
bizir, 2015-07-30 12:49:35

Why is node.js crashing?

I found a script for creating a chat on note.js on the net. Launched the script. Connected to server via XMLSocket in AS3. Everything worked, messages were sent, displayed in other windows running on 1 computer. But when launched on another computer connected via local network, the server crashes.
Here is the actual server code:

//Загрузка TCP библиотеки
EventEmitter= require('events').EventEmitter;
var server=new EventEmitter;
net=require('net');
//var mysql = require('mysql');
var clients=[];
//старт TCP сервера
net.createServer(function(socket) {
  socket.setEncoding("utf8");
  
  socket.name=socket.remoteAddress+":"+socket.remotePort
  console.log(socket.name);
  
  clients.push(socket);
  
  socket.write(JSON.stringify ({Welcome:socket.name}).toString() + "\0");
  
  socket.on('data',function(data) {
    broadcast(data,socket)
  });
  function handleAction(datas,sock)
  {
    var obj = JSON.parse(datas,substr(0,datas.length-1));
  }
  socket.on('end',function () {
    clients.splice(clients.indexOf(socket),1);
    broadcast(socket.name+" left the chat.\n",socket);
  });
  server.on('error',function(){
    
  });
  server.emit('error'); 
  
  function broadcast(message,sender){
    clients.forEach(function(client){
      //if (client===sender) return;
      client.write(JSON.stringify ({message}).toString() + "\0");
      console.log(sender);
    });
  }
}).listen(8080,"192.168.1.2");

7471beca99c14e6bbe2b2b8a5de4b94d.jpg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Valentin Dubrovsky, 2015-07-30
@matroskin13

maybe the port is busy

S
SagePtr, 2015-07-30
@SagePtr

Did you forget to change the hardcoded IP address of the server?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question