简体   繁体   English

如何在同一服务器上处理TCP / IP原始请求和HTTP请求

[英]How to handle tcp/ip raw requests and http requests on the same server

I am working on a gps tracking system and have built a server on node js. 我正在使用GPS跟踪系统,并已在节点js上构建了服务器。 This is how the file looks like for reference. 这是文件的外观,以供参考。

const net = require('net');
const lora_packet = require('lora-packet');
const dataParser = require('./dataParser');

const clients = [];


net.createServer(function(socket) {
    socket.name = socket.remoteAddress + ":" + socket.remotePort;
    clients.push(socket);


    socket.on('data', function(data) {
        console.log("Buffer sent by terminal : ");
        console.log(data);
        const packet = lora_packet.fromWire(data, 'hex');
        const str = packet._packet.PHYPayload.toString('hex');
        dataParser.parse_data(str, socket);

    });
    socket.on('end', function() {
        clients.splice(clients.indexOf(socket), 1);
        //broadcast(socket.name + "has left the cartel.\n");
    });

    function broadcast(message, sender) {
        clients.forEach(function(client) {
            if (client === sender) client.write(message + "\nsent\n");
            return;
            client.write(message);
        });
        process.stdout.write(message);
    }
}).listen(8080);

console.log("cartel is running on the port 8080\n");

This server file handles only requests from the hardware and processes raw tcp/ip requests. 该服务器文件仅处理来自硬件的请求,并处理原始的tcp / ip请求。

I want the server to handle http requests also and want to incorporate routing feature in the server too for client side applicarions for browser. 我还希望服务器也处理http请求,并希望在服务器中也包含路由功能,以用于浏览器的客户端应用程序。

1) Is there any way that http requests can also be handled by the same server or should I open another port and deploy an express node js app on that? 1)是否可以通过同一服务器处理http请求,还是应该打开另一个端口并在该端口上部署Express Node js应用?

2) If I use the same 8080 port for http, how can the routing be achieved? 2)如果为http使用相同的8080端口,如何实现路由?

3) If I use different ports for http and raw tcp/ip, what would be the best way for communication between the two server. 3)如果我对http和raw tcp / ip使用不同的端口,那么在两台服务器之间进行通信的最佳方法是什么。 The communication between tcp/ip server and http server should happen via socket(sending data dynamically). tcp / ip服务器和http服务器之间的通信应通过套接字(动态发送数据)进行。

From http server using socket, data has to be sent dynamically to browser to update live location 从使用套接字的http服务器,必须将数据动态发送到浏览器以更新实时位置

So is the flow right? 流程对吗?

Hardware (<---->)TCP/IP server(<--->)Http server(<--->)Browser 硬件(<---->)TCP / IP服务器(<--->)HTTP服务器(<--->)浏览器

If more information is needed to solve the query, I'll provide with that! 如果需要更多信息来解决查询,我将为您提供!

Thank you 谢谢

It's very complicated to try to speak multiple protocols on the same port. 尝试在同一端口上讲多种协议非常复杂。 It requires some sort of scheme at the beginning of each connection to sample the incoming data and identify which protocol it is and then shunt that connection off to the right code to handle that protocol. 在每个连接的开始都需要某种方案,以对传入的数据进行采样并确定它是哪个协议,然后将该连接分流到正确的代码以处理该协议。 I wouldn't suggest it. 我不会建议。

It is way, way easier to just open a second server on a different port for an Express server to field your http requests. 只需在另一个端口上打开第二个服务器,Express服务器即可通过该端口来访问您的http请求,这样更容易。 Very simple. 很简单。 You can do it right in the same app. 您可以在同一应用程序中完成操作。 Because both servers can be in the same app, you can just directly read from one connection and write to the other. 因为两个服务器都可以在同一个应用程序中,所以您可以直接从一个连接读取并向另一个连接写入。 There's no need for interprocess communication. 无需进行进程间通信。

Is there any way that http requests can also be handled by the same server or should I open another port and deploy an express node js app on that? 是否可以通过同一服务器处理http请求,还是应该打开另一个端口并在该端口上部署Express Node js应用程序?

Open another port. 打开另一个端口。 No need to write another app unless you have a specific reason to use two processes. 除非您有特定原因要使用两个进程,否则无需编写其他应用程序。 You can put both the plain TCP server and the Express server in the same node.js app. 您可以将普通TCP服务器和Express服务器都放在同一node.js应用程序中。

If I use the same 8080 port for http, how can the routing be achieved? 如果我对http使用相同的8080端口,如何实现路由?

It's not easy. 这并不容易。 Not suggest to use the same port for multiple protocols. 不建议对多个协议使用同一端口。

If I use different ports for http and raw tcp/ip, what would be the best way for communication between the two server. 如果我对http和原始tcp / ip使用不同的端口,那么这是两台服务器之间进行通信的最佳方式。 The communication between tcp/ip server and http server should happen via socket(sending data dynamically). tcp / ip服务器和http服务器之间的通信应通过套接字(动态发送数据)进行。

You can put both servers in the same node.js app and then you can just read/write directly from one to the other with the same code. 您可以将两个服务器都放在同一个node.js应用程序中,然后可以使用相同的代码直接在一个服务器之间进行读写。 No need for interprocess communication. 无需进程间通信。

From http server using socket, data has to be sent dynamically to browser to update live location 从使用套接字的http服务器,必须将数据动态发送到浏览器以更新实时位置

Sending data dynamically to a browser usually means you want the browser to hold something like a webSocket or socket.io connection to your server so you can then send data to the browser at any time over the existing connection. 将数据动态发送到浏览器通常意味着您希望浏览器保留与服务器的webSocket或socket.io连接之类的东西,以便您可以随时通过现有连接将数据发送到浏览器。 Otherwise, you would have to "wait" for the browser to request data and then respond with the data when it asks. 否则,您将不得不“等待”浏览器请求数据,然后在请求时用数据进行响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM