0
스트리밍 데이터를 처리하는 경우 나, 스트리밍에서 다음 데이터가 미정 :Nodejs 및 형식 오류 방법 "트림"
'ID|20120206|080500|0000001|0|1|record1|END'
'ID|20120206|080500|0000002|0|1|record2|END'
'ID|20120206|080500|0000003|0|1|record3|END'
하고 난 다음 nodejs 코드를 이용하여 행함으로써, 스트리밍 데이터 행을 처리 할 :
var net = require('net');
var HOST = 'localhost',
PORT = 9010,
lastSeqNo = 0;
var client = new net.Socket();
client.setEncoding('ascii');
client.connect(PORT, HOST, function() {
console.log('Connected to: %s : %d', HOST, PORT);
});
client.on('close', function (hadError) {
console.log(hadError);
});
client.on('error', function (ex) {
console.log(ex);
});
var i = 1;
var Line = function (rows, data) {
this.SeqNo = parseFloat(rows[3].trim());
this.Date = rows[1].trim();
this.Time = rows[2].trim();
this.MsgType = parseInt(rows[4].trim(), 10);
this.Data = data;
};
client.on('data', function (data) {
var content = data.split('\r\n');
content.forEach(function (item) {
if (item.length > 0) {
console.log(i, item);
i++;
var rows = item.split('|'),
line = new Line(rows, data),
seqNo = line.SeqNo,
msgType = line.MsgType;
console.log('seqno:', seqNo);
}
});
});
을
D:\test node\app.js:33
this.SeqNo = parseFloat(rows[3].trim());
^
TypeError: Cannot call method 'trim' of undefined
at new <anonymous> (D:\test node\app.js:33:37)
at D:\test node\app.js:48:24
at Array.forEach (native)
at Socket.<anonymous> (D:\test node\app.js:42:13)
at Socket.emit (events.js:67:17)
at TCP.onread (net.js:362:31)
당신이 무엇을 나에게 도움이 될 수 :
몇 가지 데이터 라인 공정 후, 나는 몇 가지 오류가 아래와 같이 있어요 위의 코드가 잘못 되었습니까?
감사합니다. 인도네시아 인사말.
감사합니다. @ jani-hartikainen, 이제 정상적으로 작동합니다. 귀하의 회신을 읽은 후 내 코드 변경 아래 : 나는 완전히 소켓에서 client.on '데이터'를 제거하고이 것으로 바꿉니다. var carrier = require ('carrier'); var my_carrier = carrier.carry (클라이언트); my_carrier.on ('라인', 기능 (선) { 경우 (line.length> 0) { VAR 행 = line.split ('|'), 에 맞추다 = 새로운 라인 (행 선), SEQNO = aLine.SeqNo, MSGTYPE = aLine.MsgType; CONSOLE.LOG ('라인'라인) }}); – hbinduni