我有一个 ues 案例,我需要连接到远程套接字并发送传入的 HTTP 有效负载。这是我编写的示例代码,
const net = require('net');
const express = require('express');
var backendConfig = require('./backend_config')
const app = express();
const port = 8080;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.post('/', function(req, res){
const backendName = "UBPS";
var client = new net.Socket();
client.connect(backendConfig[backendName]["port"], backendConfig[backendName]["url"], function() {
console.log('Connected');
var a = Buffer.from(req.body.isoMessage, 'hex');
console.log(a)
client.write(a);
});
client.on('data', function(data) {
console.log('Received: ' + data);
res.send(data);
client.end(); // kill client after server's response
});
client.on('close', function() {
console.log('Connection closed');
});
});
app.listen(port, function () {
console.log('Example app listening on port 8080!');
});
当我卷曲到端点时,我收到一条消息,表明套接字已连接并且已写入数据,但是,我没有收到收到响应的消息。还有一点,独立的 nodejs 代码工作得很好。
var client = new net.Socket();
client.connect(port, ip, function() {
console.log('Connected');
var a = Buffer.from("245463", 'hex');
console.log(a)
client.write(a);
});
client.on('data', function(data) {
console.log('Received: ' + data);
var b = Buffer.from(data,"str");
console.log(b.toString());
var a = Buffer.from(data, 'hex');
console.log(a.toString());
client.end(); // kill client after server's response
});
client.on('close', function() {
console.log('Connection closed');
});
有人可以帮助我了解我做错了什么吗?
我也可以接受任何其他解决方案。本质上,我想从节点服务器连接到套接字服务器。对库没有限制。