net.js - A Node.JS package to work with TCP

TCP (Transmission Control Protocol) is a robust and efficient protocol of the fourth layer of the OSI model, the transport layer, and on which most of the services of Internet is mounted as SSH, FTP and HTTP. Became suitable for large network because it is a protocol that verifies that the data is being sent correctly, in sequence, and without error.

net.js is a package that integrates the core of Node.js
framework, was and is specifically developed and enhanced to work with TCP connections using Javascript. I will explain the use of this package by creating a chat that will be accessed via terminal using an application protocol, such as TELNET or any protocol that uses TCP as the transport protocol


// Instantiate the package to work with TCP, "net.js"
var net = require("net");

// Creating a array to put all clients "sockets" connected
var clientList = [];
var ids = 0

// Creating a server from "net" object 
var servidor =  net.createServer(function(client){
 // Adding all client who conect to server to clientList
 client.id = ids++
 clientList.push(client);
 
 // Send a wellcome message to client
 client.write("Wellcome to Chat!\n");
 
 // Event actived every time a client connect to server
 client.on("connect", function(){
  // Printing on server the client ID
  console.log(client.id+" connected!");
 });
 
 // Event actived every time a client send data to server
 client.on("data", function(data){
  // Creating a logout system
  if(String(data).match(/sair/)){
   // Disconecting the client
   client.end();
  }
  // Creating a broadcast system
  for(var i=0; i < clientList.length; i++){
   // Avoiding sent message from client to itself
   if(clientList[i] == client){
    continue;
   }
   // Sending data to a client on i position of clientList
   clientList[i].write(data);
  }
 });
 
 // Event actived every time a client desconnect from server
 client.on("end", function(){
  // finding the position of desconected client
  var i = clientList.indexOf(client);
  // excluding the client from list
  clientList.splice(i, 1);
  // print a message of desconnection from a client 
  console.log(client.id+" desconnected!");
 });
});

// Setting the port to listen
servidor.listen(8000);

// Printing a message of success on server
console.log("TCP Server connected on 127.0.0.1 port 8000, waiting connections!!!");

As we have highlighted in the code:

// Create a instance of package
var net = require("net");

// Creating a server fron net object
// Note, the method param is a recipient to every client will be putted in
var servidor =  net.createServer(function(client){});

Other important points are the event handlers of the net object.

// Event actived every time a client connect to server
client.on("connect", function(){});

// Event actived every time a client send data to server
// The param data is a recipient to every data will be putted in
client.on("data", function(data){});

// Event actived every time a client desconnect from server
client.on("end", function(){});

Finally, just configure which port the server should be listening for connections

// Setting a port to server to listen
servidor.listen(8000);

With these methods and a help of .write(data), a method of the client object, used to send messages to the customer in question. I implemented a broadcast and disconnect system to my chat, now, feel free to enhance the code to better serve them.

To play with this chat:

Open a terminal or start any GUI to handle with TELNET and set
  
Host: localhost or 127.0.0.1 on Port: 8000

comand: > telnet localhost 8000  
net.js - A Node.JS package to work with TCP net.js - A Node.JS package to work with TCP Reviewed by AJ Alves on terça-feira, setembro 11, 2012 Rating: 5

Nenhum comentário:

Tecnologia do Blogger.