NodeJS环境下实现socket简单通信

NodeJS环境下实现socket简单通信

myserver.js

const net = require( 'net' );
const port = 8000;
const hostname = '127.0.0.1';

// 定义两个变量, 一个用来计数,一个用来保存客户端
let clients = {};
let clientName = 0;

// 创建服务器
const server = new net.createServer();

server.on('connection', (client) => {
  client.name = ++clientName; // 给每一个client起个名
  clients[client.name] = client; // 将client保存在clients

  client.on('data', function (msg) { //接收client发来的信息
    console.log(`客户端${client.name}发来一个信息:${msg}`);
  });

  client.on('error', function (e) { //监听客户端异常
    console.log('client error' + e);
    client.end();
  });

  client.on( 'close', function () {
    delete clients[client.name];
    console.log(`客户端${ client.name }下线了`);
  });

});

server.listen( port,hostname,function () {
  console.log(`服务器运行在:http://${hostname}:${port}`);
});

client.js

const net = require('net');

const socket = new net.Socket();

const port = 8000;

const hostname = '127.0.0.1';

socket.setEncoding = 'UTF-8';

socket.connect( port,hostname,function(){
  socket.write('hello 大家好~~');
});

socket.on( 'data', function ( msg ) {
  console.log( msg );
});

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

socket.on('close',function(){
  console.log('服务器端下线了');
});

运行效果

客户端上线和下线
NodeJS环境下实现socket简单通信_第1张图片
服务器端下线后
在这里插入图片描述

你可能感兴趣的:(node.js)