server for my chess game
Diffstat (limited to 'src/StreamTcp.js')
| -rw-r--r-- | src/StreamTcp.js | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/StreamTcp.js b/src/StreamTcp.js new file mode 100644 index 0000000..82306fe --- /dev/null +++ b/src/StreamTcp.js @@ -0,0 +1,18 @@ +const Transform = require('stream').Transform + +class StreamTcp extends Transform { + _transform (chunk, enc, done) { + let buffer = chunk + while (buffer.length > 0) { + const length = buffer.readUInt16LE(0) + + const bufferSplitted = buffer.slice(4, length + 4) // 4 cause the length bytes is in buffer + buffer = buffer.slice(length + 4, buffer.length) // 4 cause the length bytes is in buffer + + this.push(bufferSplitted) + } + done() + } +} + +module.exports = StreamTcp
\ No newline at end of file |