Created
July 21, 2020 21:36
-
-
Save Yovach/7e3c4b6bb1ab89d9e29b511a4b7acc27 to your computer and use it in GitHub Desktop.
A class that allows to separate buffers thanks to a byte.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class MessageData extends Array { | |
private readonly delimiter: number | |
constructor (delimiter: number) { | |
super() | |
this.delimiter = delimiter | |
} | |
isFinished (): boolean { | |
return this.length === 0 || !this.includes(this.delimiter) | |
} | |
next (): number[] | null { | |
const delimiterIndex = this.indexOf(this.delimiter) | |
if (delimiterIndex !== -1) { | |
return this.splice(0, delimiterIndex + 1) | |
} | |
return null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On Node.JS, you can parse packet from 'data' event.
Example :