首页 > 解决方案 > 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');
    });
  }
}

标签: node.js

解决方案


推荐阅读