我的要求有点不同,即使它是可以实现的也不知道. 我使用Node.js开发后端应用程序服务器.这个服务器基本上做两个工作: (1)服务客户:我的客户都是将发送HTTP(S)请求的手机,收到响应后
我使用Node.js开发后端应用程序服务器.这个服务器基本上做两个工作:
(1)服务客户:我的客户都是将发送HTTP(S)请求的手机,收到响应后将关闭会话.
(2)调用其他一些异步工作服务:另一方面,服务器将连接到仅通过TCP / IP连接而不是HTTP工作的其他服务器.这里异步意味着,服务器将发送请求,不应等待响应.响应将通过相同的TCP / IP连接接收.
所以我想要实现的流程是:
>手机将HTTP请求发送到服务器
>服务器收到HTTP请求后,调用TCP / IP上的服务
>服务器通过TCP / IP连接从TCP / IP服务接收响应
>服务器通过响应响应电话.
为了表示上述流程,我附上了下图.
在上图中,TCP / IP服务器由其他一些提供商管理.
我在node.js中编写了以下代码,它根据我们的要求有时完美地工作,但有时它会向HTTP请求发送不正确的响应.我没有编写任何代码来处理这个问题.
var net = require('net');
var client = new net.Socket();
client.connect(2202, 'example_ip', function () {
console.log('Connected');
// client.write('Hello, server! Love, Client.');
});
//Lets require/import the HTTP module
var http = require('http');
//Lets define a port we want to listen to
const PORT = 8080;
//We need a function which handles requests and send response
function handleRequest(request, response) {
var body = '';
request.on('data', function (chunk) {
body += chunk;
});
request.on('end', function () {
console.log('Received request from JMeter------------>>>');
// console.log(body);
client.write(body);
var count = 0;
client.on('data', function (data) {
console.log('<<<------------Received from SSM: ' + data);
response.end(data);
// client.destroy(); // kill client after server's response
});
});
client.on('close', function () {
console.log('Connection closed');
});
}
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function () {
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});
请一些人指导我解决这个问题.
您可以为每个传入请求实例化一个新客户端吗?这样,每个请求的TCP连接将是唯一的.