如何连续接收Node js中的TCP连接数据

时间:2019-10-29 11:46:08

标签: node.js sockets tcp

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

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()。所以请您帮我获取设备数据

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没有获取任何信息

我需要这样的回复

{ "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"}

1 个答案:

答案 0 :(得分:0)

因此对于连续数据(在这种情况下为1Hz)。我将server.js设置为

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');

然后,坦率地说,客户变得更容易

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"}