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 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;
これは buf.inspect() を与えるバイトオーダーです
<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
何が起こったか?