NodeJS: What is the proper way to handling TCP socket streams ? Which delimiter should I use?

前端 未结 2 717
滥情空心
滥情空心 2020-12-28 10:47

From what I understood here, \"V8 has a generational garbage collector. Moves objects aound randomly. Node can’t get a pointer to raw string data to write to socket.\" so I

相关标签:
2条回答
  • 2020-12-28 11:17

    It has indeed been said that there's extra work going on because Node has to take that buffer and then push it into v8/cast it to a string. However, doing a toString() on the buffer isn't any better. There's no good solution to this right now, as far as I know, especially if your end goal is to get a string and fool around with it. Its one of the things Ryan mentioned @ nodeconf as an area where work needs to be done.

    As for delimiter, you can choose whatever you want. A lot of binary protocols choose to include a fixed header, such that you can put things in a normal structure, which a lot of times includes a length. In this way, you slice apart a known header and get information about the rest of the data without having to iterate over the entire buffer. With a scheme like that, one can use a tool like:

    • node-buffer - https://github.com/substack/node-binary
    • node-ctype - https://github.com/rmustacc/node-ctype

    As an aside, buffers can be accessed via array syntax, and they can also be sliced apart with .slice().

    Lastly, check here: https://github.com/joyent/node/wiki/modules -- find a module that parses a simple tcp protocol and seems to do it well, and read some code.

    0 讨论(0)
  • 2020-12-28 11:28

    You should use the new stream2 api. http://nodejs.org/api/stream.html

    Here are some very useful examples: https://github.com/substack/stream-handbook

    https://github.com/lvgithub/stick

    0 讨论(0)
提交回复
热议问题