Node.js nodejs缓冲区()的问题
我使用Buffer()将数据包头设置为100字节 根据标题的形成规则,我必须指定以下内容:Node.js nodejs缓冲区()的问题,node.js,buffer,Node.js,Buffer,我使用Buffer()将数据包头设置为100字节 根据标题的形成规则,我必须指定以下内容: Offset Length(bytes) Type Description 0 4 Int The length of the message (no header) 4 4 Int Time to create the query (the number of seconds since Janu
Offset Length(bytes) Type Description
0 4 Int The length of the message (no header)
4 4 Int Time to create the query (the number of seconds since January 1, 1970 GMT)
8 4 Int The message ID
12 32 Reserved (filled with null byte)
44 2 Int The client ID
46 1 1st byte of message flags
47 1 2nd byte of message flags
48 4 Int The identifier of the symmetric key
52 48 Reserved (filled with null byte)
这是我的密码:
var query="<?xml version=\"1.0\" encoding=\"utf-8\"?><sirena><query><get_currency_rates><curr1>RUB</curr1><curr2>USD</curr2><owner>IATA</owner></get_currency_rates></query></sirena>";
var buf=new Buffer(100);
var query1=new Buffer(query);
console.log(query1.length);
buf.writeInt32BE(query1.length, 0, true);//Длина текста сообщения (без заголовка)
var foo = new Date;
var unixtime_ms = foo.getTime();
var unixtime = parseInt(unixtime_ms / 1000);
console.log(unixtime);
buf.writeInt32BE(unixtime, 4, true);//Время создания запроса (кол-во секунд с 1 января 1970 GMT)
buf.writeInt32BE(1, 8, true);//id сообщения. потом нужно автоинкрементить
for(var i=12; i<44;i++){//Зарезервировано (заполнено нулевым байтом)
buf.writeInt8(0, i, true);
}
buf.writeInt16BE(5985, 44, true);//Идентификатор клиента
buf.writeInt8(0, 46, true);//1-й байт флагов сообщения - обязательно ли это??
buf.writeInt8(0, 47, true);//2-й байт флагов сообщения - обязательно ли это??
buf.writeInt32BE(0, 48, true);//Идентификатор симметричного ключа - обязательно ли это??
for(var i=52; i<100;i++){//Зарезервировано (заполнено нулевым байтом)
buf.writeInt8(0, i, true);
}
var packet=buf.toString();//+query;
发生了什么事?您在转换stringbuffer中使用了不同的编码 -> ->
我需要utf8编码。但是当我使用它时,我遇到了这些问题。使用utf8和头按原样(即二进制)编码xml。另外,为什么需要将缓冲区转换为字符串<代码>数据包=新的缓冲区(100+缓冲区.ByTeleLength(查询,utf8);报头.复制(数据包);数据包.写入(查询,100,'utf8');报头.复制(数据包);
是什么?将报头
缓冲区复制到数据包
缓冲区的位置0
<Buffer 00 00 00 a6 51 c0 15 db 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 17 61 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>
0000 00ef bfbd 51ef bfbd 15ef bfbd 0000
0001 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 1761 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000
//...
console.log(buf);
var packet=buf.toString('binary');//+query;
console.log(Buffer(packet, 'binary'));
//...
<Buffer 00 00 00 a6 51 c0 27 d6 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 17 61 00 00 00 00 00 ...>
<Buffer 00 00 00 a6 51 c0 27 d6 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 17 61 00 00 00 00 00 ...>
//...
console.log(buf);
var packet=buf.toString();//+query; // looks like utf8 here
console.log(Buffer(packet, 'binary')); // and binary here (even without explicit 'binary')
//...
<Buffer 00 00 00 a6 51 c0 27 d6 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 17 61 00 00 00 00 00 ...>
<Buffer 00 00 00 fd 51 fd 28 28 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 17 61 00 00 00 00 00 ...>