javascript - NodeJS socket data splitting - Stack Overflow

When I need to split data I have to convert it to string.Here is my data handler function:socket.on(&#

When I need to split data I have to convert it to string.
Here is my data handler function:

  socket.on('data', function (data) {
    var str = data.toString().split("|");
    switch(str[0]){
        case "setUser":
        setUser(str[1], socket);
        break;
        case "joinChannel":
        joinChannel(str[1], socket);
        break;
    }

  });

When I send data like "setUser|Name" and then "joinChannel|main" from AS3 client. NodeJS reads it as one data packet.
My question is how to make that as two different data packets?

When I need to split data I have to convert it to string.
Here is my data handler function:

  socket.on('data', function (data) {
    var str = data.toString().split("|");
    switch(str[0]){
        case "setUser":
        setUser(str[1], socket);
        break;
        case "joinChannel":
        joinChannel(str[1], socket);
        break;
    }

  });

When I send data like "setUser|Name" and then "joinChannel|main" from AS3 client. NodeJS reads it as one data packet.
My question is how to make that as two different data packets?

Share Improve this question edited Dec 25, 2015 at 10:59 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 7, 2012 at 15:47 GugisGugis 53912 silver badges25 bronze badges 3
  • What character separates the two pieces? A New line? You have no control over the packets themselves. – loganfsmyth Commented May 7, 2012 at 15:52
  • AS3 code: server.send("setUser|"+name_txt.text)+"\n"; server.send("joinChannel|aha")+"\n"; – Gugis Commented May 7, 2012 at 22:02
  • Did you try my answer? Also, the code in your ment won't put newlines in the data sent, it appends a newline to the result of the send mand... – loganfsmyth Commented May 8, 2012 at 5:55
Add a ment  | 

1 Answer 1

Reset to default 3

Normally you would buffer all of the data together, and then parse it as one string. Or if you need to part it as it es in, then you would do the splitting in the data callback and keep track of any leftover partial mands to prepend on the net chunk received.

var data = '';
socket.setEncoding('utf8');
socket.on('data', function(chunk) {
  data += chunk;
});
socket.on('end', function() {

  var lines = data.split('\n');
  lines.forEach(function(line) {
    var parts = line.split('|');
    switch (parts[0]) {
      case 'setUser':
        setUser(str[1], socket);
        break;
      case 'joinChannel':
        joinChannel(str[1], socket);
        break;
    }
  });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745438817a4627735.html

相关推荐

  • javascript - NodeJS socket data splitting - Stack Overflow

    When I need to split data I have to convert it to string.Here is my data handler function:socket.on(&#

    8小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信