Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在Node js中连续接收tcp连接数据_Node.js_Sockets_Tcp - Fatal编程技术网

Node.js 如何在Node js中连续接收tcp连接数据

Node.js 如何在Node js中连续接收tcp连接数据,node.js,sockets,tcp,Node.js,Sockets,Tcp,我以前的项目是用jsxmlsocket()开发的。它正在使用flash。Js xml套接字与ip和端口连接,然后响应如下{“ClientID”:“202.75.xx.xxx:xxxxx”,“ServerDist”:“1”}通过使用此客户端id,我发送车辆设备id,因此我不断获取所有车辆信息 xmls.onData = function(data1) { console.log('getData: ' + data1) console.log(data1)

我以前的项目是用jsxmlsocket()开发的。它正在使用flash。Js xml套接字与ip和端口连接,然后响应如下{“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 sednigclientid之后,我没有得到正确的客户机信息。它没有调用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"}
因此,对于连续数据(在这种情况下为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"}

emit在这种情况下做什么?谢谢!。我需要完全一样,但差别很小。您正在使用server.js吗?我的问题是服务器在.Net中(我不知道里面写了什么)。但是,当我使用flash运行jsxmlsocket()时,它不断地提供数据。在未来的flash不工作,所以我移动到节点js这是为实时车辆监控系统的目的。发送客户端ID+Sim卡ID后,它将提供实时数据。但我还是无法获取实时数据好吧,我想可能就是这样,所以我也这么做了。等一下,我会更新的。我能和你私下谈谈吗?从上个月开始,我一直在为这个问题而挣扎。我只能编辑客户端程序。我不会写服务器程序。服务器已从.Net发出响应。它已在使用jsxmlsocket。所以我只需要在nodejs中更改客户机