javascript - Using NodeJS buffer -
i have following code:
var packet = "\xff\xff\xff\xff"; packet += "\x6d"; packet += "127.0.0.1:" + this.port; packet += "\x00"; packet += this.name; packet += "\x00"; packet += this.map; packet += "\x00"; packet += "game1"; packet += "\x00"; packet += "x-z"; packet += "\x00"; packet += string.fromcharcode(this.players.length); packet += string.fromcharcode(this.maxplayers); packet += string.fromcharcode(this.protocol); packet += "\x64"; packet += "\x6c"; packet += "\x00"; packet += "\x01"; packet += "\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x01\x00\x01\x00"; return new buffer(packet, "binary");
i'm creating buffer string now, think isn't practice , string concatenation not efficient. how can replace buffer functions , write buffer directly? can't understand how buffer works, example, how write 4 \xff
bytes @ beginning.
thank you.
if size of packet fixed, use one of many buffer methods write directly buffer.
initialize static string containing static data , leave custom data blank. later directly write custom data buffer using precalculated offsets.
this somehow this:
var static = "\xff\xff\xff\xff........."; var buff = new buffer(static, 'binary'); buff.write(this.port, portstartoffset, portstringlength, 'binary');
Comments
Post a Comment