简体   繁体   中英

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. 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.

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.

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?

2) If I use the same 8080 port for http, how can the routing be achieved?

3) If I use different ports for http and raw tcp/ip, what would be the best way for communication between the two server. The communication between tcp/ip server and http server should happen via socket(sending data dynamically).

From http server using socket, data has to be sent dynamically to browser to update live location

So is the flow right?

Hardware (<---->)TCP/IP server(<--->)Http server(<--->)Browser

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. 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?

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.

If I use the same 8080 port for http, how can the routing be achieved?

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. The communication between tcp/ip server and http server should happen via socket(sending data dynamically).

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. No need for interprocess communication.

From http server using socket, data has to be sent dynamically to browser to update live location

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. Otherwise, you would have to "wait" for the browser to request data and then respond with the data when it asks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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