如何在Node js中连续接收tcp连接数据 [英] How to receive tcp connection data in Node js Continously

查看:75
本文介绍了如何在Node js中连续接收tcp连接数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前的项目是用jsxmlsocket(https://www.developerfusion.com/project/18979/jsxmlsocket).它正在使用闪存.Js xml 套接字与 ip 和端口连接,然后像这样的响应 {"ClientID":"202.75.xx.xxx:xxxxx","ServerDist":"1"} 通过使用这个客户端 ID 我正在发送车辆设备 ID,所以我正在持续获取所有车辆信息.

My previous Project is developed with jsxmlsocket(https://www.developerfusion.com/project/18979/jsxmlsocket). It is working with flash. Js xml socket connected with ip and port then The response like this {"ClientID":"202.75.xx.xxx:xxxxx","ServerDist":"1"} By using this client id i am sending vehicle device id, So i am getting all the vehicle information continously.

xmls.onData = function(data1) {
        console.log('getData: ' + data1)
        console.log(data1)
        var strs = (data1.slice(0, -1));
        data = JSON.parse(strs)
        if (data.ServerDist == 1) {
            var sentMsg = '{\"ClientID\":\"' + data.ClientID + '\",\"TrackSystemNos\":\"1399288XXXX|\"}';
            console.log('Sent Msg: ' + sentMsg)
            xmls.send(sentMsg);
        } else {
            console.log('New Positions')
        });

但现在我在 NODEJS 中工作,我得到了客户端 ID 和 ServerDist,在使用设备 ID sednig ClientID 后,我没有得到正确的客户端信息.它不是在调用 client.on().那么请你帮我如何获取设备数据

But now i am working in NODEJS i am getting Client id and ServerDist, After sednig ClientID with device id i am not getting correct client information. It is not calling client.on(). So can you please help me how to get the device data

client.on('data', function(data1) {
console.log('getData: ' + data1)
var strs = (data1.slice(0, -2));
data = JSON.parse(strs)
const newLocal = 'New Positions';
if (data.ServerDist == 1) {
    var sentMsg = '{\"ClientID\":\"' + data.ClientID + '\",\"TrackSystemNos\":\"1399288XXXX|\"}';
    console.log('Sent Msg: ' + sentMsg)
    client.emit(sentMsg);
    client.write(sentMsg);
} else
    console.log(newLocal);

});

第一次获取客户端id,但是第二次data1没有获取任何信息

First time client id is getting, But Second time data1 is not getting any information

我需要这样的回应

{ "SimID":"139928XXXXX",   "SignalType":"Locate",   "DateTime":"2019-10-24 13:31:40",   "Longitude1":"0.00000",   "Latitude1":"0.00000",   "Longitude":"0.00000",   "Latitude":"0.00000",   "Velocity":"0",   "Angle":"0",   "LocateStatus":"NoLocate",   "StatusA":"OFF",   "StatusB":"OFF",   "StatusC":"OFF",   "StatusD":"OFF",   "TurnStatus":"0",   "AccStatus":"OFF",   "Temperature":"0",   "Temperature2":"",   "Temperature3":"",Temperature4":"",   "Oil":"0",   "Oil2":"0",   "LevelNum":"12.13",   "OilIn":"0",   "OilOut":"0",   "Miles":"7611669",   "ParkingSpan":"1.11:54:59",   "TodayMile":"61321"}

推荐答案

所以对于连续数据(在本例中为 1Hz).我将 server.js 设置为

So for continuous data (in this case at 1Hz). I setup the server.js as

const net = require('net');

var init = {
    ClientID: null,
    ServerDist: 1,
};

var dummy = {
    SimID: '139928XXXXX',
    SignalType: 'Locate',
    DateTime: '2019-10-24 13:31:40',
    ...
};


function send_data(socket) {
    let temp = JSON.stringify(dummy) + '# ';
    socket.write(temp);
}

const server = net.createServer(function (socket) {
    socket.on('data', data => {
        # init is a raw string buffer so I had to .toString it
        if (data.toString() === 'init') {
            init.ClientID = socket.remoteAddress + ':' + socket.remotePort;
            let temp = JSON.stringify(init) + '# ';
            socket.write(temp);
        }
        else {
            # Call send_data every 1000 ms with socket as an arg.
            setInterval(send_data, 1000, socket )

        }
    });
});

server.listen(1337, '127.0.0.1');

那么坦率地说客户就变得容易了

Then the client becomes easier frankly

client.connect(PORT, HOST, function () {
    console.log('CONNECTED TO: ' + HOST + ':' + PORT);
    client.write('init');
    console.log('Sent: ', 'init');
});

var ClientID = '';

client.on('data', function (data) {
        let strs = (data.slice(0, -2));
        let Cdata = JSON.parse(strs);
        console.log('Response:' + JSON.stringify(Cdata));
        if (Cdata.ServerDist === 1) {
            ClientID = Cdata.ClientID;
            let sentMsg = {ClientID: ClientID, TrackSystemNos: '13992881XXX|'};
            client.write(JSON.stringify(sentMsg));
            console.log('Sent: ' + JSON.stringify(sentMsg));
        }
    }
);

我回来了

CONNECTED TO: 127.0.0.1:1337
Sent:  init
Response:{"ClientID":"127.0.0.1:35554","ServerDist":1}
Sent: {"ClientID":"127.0.0.1:35554","TrackSystemNos":"13992881XXX|"}
Response:{"SimID":"139928XXXXX","SignalType":"Locate","DateTime":"2019-10-24 13:31:40","Longitude1":0,"Latitude1":0,"Longitude":0,"Latitude":0,"Velocity":0,"Angle":0,"LocateStatus":"NoLocate","StatusA":"OFF","StatusB":"OFF","StatusC":"OFF","StatusD":"OFF","TurnStatus":0,"AccStatus":"OFF","Temperature":0,"Temperature2":null,"Temperature3":null,"Temperature4":null,"Oil":0,"Oil2":0,"LevelNum":12.13,"OilIn":0,"OilOut":0,"Miles":7611669,"ParkingSpan":"1.11:54:59","TodayMile":"61321"}
Response:{"SimID":"139928XXXXX","SignalType":"Locate","DateTime":"2019-10-24 13:31:40","Longitude1":0,"Latitude1":0,"Longitude":0,"Latitude":0,"Velocity":0,"Angle":0,"LocateStatus":"NoLocate","StatusA":"OFF","StatusB":"OFF","StatusC":"OFF","StatusD":"OFF","TurnStatus":0,"AccStatus":"OFF","Temperature":0,"Temperature2":null,"Temperature3":null,"Temperature4":null,"Oil":0,"Oil2":0,"LevelNum":12.13,"OilIn":0,"OilOut":0,"Miles":7611669,"ParkingSpan":"1.11:54:59","TodayMile":"61321"}
Response:{"SimID":"139928XXXXX","SignalType":"Locate","DateTime":"2019-10-24 13:31:40","Longitude1":0,"Latitude1":0,"Longitude":0,"Latitude":0,"Velocity":0,"Angle":0,"LocateStatus":"NoLocate","StatusA":"OFF","StatusB":"OFF","StatusC":"OFF","StatusD":"OFF","TurnStatus":0,"AccStatus":"OFF","Temperature":0,"Temperature2":null,"Temperature3":null,"Temperature4":null,"Oil":0,"Oil2":0,"LevelNum":12.13,"OilIn":0,"OilOut":0,"Miles":7611669,"ParkingSpan":"1.11:54:59","TodayMile":"61321"}

这篇关于如何在Node js中连续接收tcp连接数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆