пятница, 25 августа 2017 г.

Node.js Net Socket Client request

var net = require('net');

var options = {
    host: 'google.com',
    port: 80
}

var client = net.connect(options, function () {
    console.log('client connected');
    client.end(
        'GET / HTTP/1.0\r\n' +
        'Host: google.com\r\n' +
        '\r\n'
    );
});

client.on('data', function (chunk) {
    console.log(chunk.toString());
});


client.on('end', function () {
    console.log('client disconnected');
});

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

client.on('timeout', function () {
    console.log('Timeout');
});


вторник, 22 августа 2017 г.

Net Socket Server and Net Socket Client

var net = require('net');

var HOST = '127.0.0.1';
var PORT = 6969;

// TCP Socket Server

// Create a server instance, and chain the listen function to it
// The function passed to net.createServer() becomes the event handler for the 'connection' event
// The sock object the callback function receives UNIQUE for each connection

var server = net.createServer();

server.on('connection', function(sock) {

    // We have a connection - a socket object is assigned to the connection automatically
    console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

    // Add a 'data' event handler to this instance of socket
    sock.on('data', function(data) {

        console.log('DATA ' + sock.remoteAddress + ': ' + data);
        // Write the data back to the socket, the client will receive it as data from the server
        sock.write('You said "' + data + '"');

    });

    // Add a 'close' event handler to this instance of socket
    sock.on('close', function(data) {
        console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
    });

});

server.listen(PORT, HOST);

console.log('Server listening on ' + HOST +':'+ PORT);

// TCP Socket Client

var client = new net.Socket();

// Add a 'data' event handler for the client socket
// data is what the server sent to this socket
client.on('data', function(data) {

    console.log('DATA: ' + data);
    // Close the client socket completely
    client.destroy();

});

// Add a 'close' event handler for the client socket
client.on('close', function() {
    console.log('Connection closed');
});

client.connect(PORT, HOST, function() {

    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
    // Write a message to the socket as soon as the client is connected, the server will receive it as message from the client
    client.write('I am Chuck Norris!');

});

Простой Socket Server для Чата

Файл chatServer.js

// Load the TCP Library
var net = require('net');

// Keep track of the chat clients
var clients = [];

// Start a TCP Server
net.createServer(function (socket) {

  // Identify this client
  socket.name = socket.remoteAddress + ':' + socket.remotePort;

  // Put this new client in the list
  clients.push(socket);

  // Send a nice welcome message and announce
  socket.write('Welcome ' + socket.name + '\n');
  broadcast(socket.name + ' joined the chat\n', socket);

  // Handle incoming messages from clients.
  socket.on('data', function (data) {
    broadcast(socket.name + '> ' + data, socket);
  });

  // Remove the client from the list when it leaves
  socket.on('end', function () {
    clients.splice(clients.indexOf(socket), 1);
    broadcast(socket.name + ' left the chat.\n');
  });
 
  // Send a message to all clients
  function broadcast(message, sender) {
    clients.forEach(function (client) {
      // Don't want to send it to sender
      if (client === sender) {return;}
      client.write(message);
    });
    // Log it to the server output too
    process.stdout.write(message)
  }

}).listen(5000);

// Put a friendly message on the terminal of the server.
console.log('Chat server running at port 5000');