我有以下来自流媒体的数据:
'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)
你能帮我上面的代码有什么问题吗?
谢谢你。来自印度尼西亚的问候。