Node.js中的并发请求

时间:2019-09-04 13:17:28

标签: node.js

我要提交并发客户端请求以检查服务器功能。

如何在单线程的节点环境中实现它?我想同时在线程中的代码下运行。

以下是我的代码:-

var net = require('net');
var sleep = require('system-sleep');
var express= require('express');

module.exports = {

  Activation:function() {
    var client = new net.Socket();
    client.setTimeout(60000);
    var strSendData="6534265365342135425366532121323767";
    var sendData = Buffer.from(strSendData, 'hex');

    console.log('Connecting...');

    client.connect(8094, '192.168.126.129', function(data,err) {
      if (err) {
        console.log('Not able to connect with server');
      } else {
        console.log('Connected');
        client.write(sendData);
      }
    });

    client.on('data', function(data) {
      console.log('Received: ' + data);
      client.destroy(); // kill client after server's response
    });

    client.on('close', function() {
      console.log('Connection closed');
    });
  }
}

0 个答案:

没有答案