1. RPC
- Remote Procedure Call (远程过程调用)
- 和Ajax类似
- 和Ajax区别
- 使用特定服务寻址(不一定使用DNS作为寻址服务)
- 使用二进制协议(应用层协议一般不使用HTTP)
- 基于TCP或UDP协议
- TCP通信方式
- HTTP协议
- 1.1之前使用短连接
- 1.1使用长连接,半双工通信,多路复用
- 2.0使用全双工
2. Buffer模块
- 编解码二进制数据包
- API
- 大小端问题
- 几个Byte里,高位和低位的编排顺序不同
- int8:8位二进制数
const buffer1 = Buffer.from('geek');
const buffer2 = Buffer.from([1,2,3]);
const buffer3 = Buffer.alloc(10);
console.log(buffer1);
console.log(buffer2);
console.log(buffer3);
console.log(`${
buffer1}`)
console.log(buffer2.readInt8(0));
buffer2.writeInt8(127, 1);
console.log(buffer2);
buffer2.writeInt16BE(128, 1);
console.log(buffer2);
buffer2.writeInt16LE(128, 1);
console.log(buffer2);
const buf = Buffer.from([0, 5]);
console.log(buf)
console.log(buf.readInt16BE(0));
console.log(buf.readInt16LE(0));
const fs = require('fs');
const protobuf = require('protocol-buffers')
const schemas = protobuf(fs.readFileSync(`${
__dirname}/test.proto`));
const buffer = schemas.Course.encode({
id: 1,
name: 'hh',
price: 30.1
})
console.log(buffer);
console.log(schemas.Course.decode(buffer));
message Course {
required float id = 1;
required string name = 2;
required float price = 3;
}
3. net模块
const net = require('net'