如何获取正在发送的整个数据

时间:2015-05-05 06:59:33

标签: javascript node.js

我有这段代码,我想获取客户端发送的全部数据。但问题在于它是逐个打印角色。

像这样

receive data ?
receive data ?
receive data _
receive data Z
receive data
receive data O
receive data ?
receive data }
receive data .
receive data ?
receive data (
receive data ?
receive data ?
receive data G
receive data ?
receive data "
receive data ?
receive data ?
receive data ?
receive data ?
receive data C
receive data ?
receive data C
receive data ?
receive data ?
receive data a

var net  = require('net');

var server = net.createServer(function(socket){
      socket.on('data',function(data){
           console.log("receive data",data.toString());
      });
});

server.listen(3333,'127.0.0.1', function () {
    console.log("server is up!");
});

1 个答案:

答案 0 :(得分:2)

未经测试但请尝试此操作:

    var net  = require('net');
    var response="";
    var server = net.createServer(function(socket){
          socket.on('data',function(data){
               response+=data.toString();
          });
          socket.on('end',function(data){
               console.log("received data", response );
          });
    });

server.listen(3333,'127.0.0.1', function () {
    console.log("server is up!");
});