Table of Contents generated with DocToc
1. Package Agreement
// [4]byte -- length fixed_size,binary big endian encode
// [4]byte -- messageID fixed_size,binary big endian encode
// [4]byte -- headerLength fixed_size,binary big endian encode
// [4]byte -- bodyLength fixed_size,binary big endian encode
// []byte -- header marshal by json
// []byte -- body marshal by json
2. Related operations
2.1 Big-endian coding
- Convert the number of uint32 to [4]byte. []byte corresponds to uint8Array in js
// params:: uint32
// return uInt8Array[4]
function daduanbianma(params) {
var bytes = new ArrayBuffer(4)
var view = new DataView(bytes)
// 大端编码
var number = params
view.setUint32(0, number, false)
return new Uint8Array(bytes)
}
2.2 Big-endian decoding
// params: uint8Array[4]
// return uint32
function daduanjiema(params) {
const buf = Buffer.from(params);
return buf.readUInt32BE(0)
}
2.3 string to utf8-uint8Array[], utf8-uint8Array to string
- For text structures such as header and body, you need to use the utf8 converter, because js defaults to utf16.
// params::string
// return uint8Array[]
function encodeUTF8(params) {
var encoder = new TextEncoder('utf-8')
var buf = encoder.encode(params)
return buf
}
// params:: 由go服务端产生的utf8编码的uint8Array[]
// return string
func decodeUTF8UintArray(params) {
var decoder = new TextDecoder('utf-8')
var jsonStr = decoder.decode(params)
return jsonStr
}
2.4 json serialization
// 将{}转化为json string
var jsonobj = {
'name': 'ft'}
var jsonstr = JSON.stringify(jsonobj)
// 将json string 转 json obj
var obj = JSON.parse(jsonstr)
2.5 uint8Array stitching and reading
// 拼接
// xulieLength 和 messageId均为 Uint8Array
var concatArray = new Uint8Array([...xulieLength,...messageId])
// 截取
// buffer为ArrayBuffer类型,从流中读取出来的默认类型
// 该句表示,头长度记录在该段序列的第八位后4个长度
var headerLengthBytes = new Uint8Array(buffer, 8, 4);
3. js-api
Address: http://www.baidu.com , to be updated
3.1 Coding
3.1.1 pack(messageID, header, body)
- Encode all, use example:
pack(0, {
Router-Type:'URL_PATTERN',
URL-Pattern-Value: '/user-pool/kf-user-info/login/',
}, {
chanel: 'web,
})
3.1.2 packURL(urlPattern, body)
- Equivalent to
pack(0, {
Router-Type:'URL_PATTERN',
URL-Pattern-Value: urlPattern,
}, body)
Example:
packURL('/user-pool/kf-user-info/login/', {
chanel: 'web'})
3.1.3 packSerialURL(urlPattern, body)
- The sent message will be processed serially, and subsequent messages will wait before the message is processed.
- Generally used in login, authentication, verification, etc.
- Equivalent to
pack(2020, {
Router-Type:'URL_PATTERN',
URL-Pattern-Value: urlPattern,
}, body)
- Example:
packSerialURL('/user-pool/kf-user-info/login/', {
chanel: 'web'})
3.1.4 packPipeURL(params)
- This string of messages will be executed synchronously and sequentially, and the message sets in params are serial to each other and parallel to other messages.
params格式为:
[
{
urlPattern: '/user-pool/kf-user-info/login/',
body: {
chanel: 'web'}
},
{
urlPattern: '/user-pool/kf-user-info/login/',
body: {
chanel: 'web'}
},
]
3.2 Decoding
3.2.1 lengthOf(stream)
• Get the length of the #1 packet protocol, which is 4 less than the length of a packet protocol
• stream is of type uint8Array, which is a packet unit
• return uint32
var l = lengthOf(stream)
3.2.2 messageIDOf(stream)
• Get the message number in the packet protocol
• stream is of type uint8Array, which is a packet unit
• return uint32
var messageID = messageIDOf(stream)
3.2.3 headerLengthOf(stream)
• Get the length of the header in the packet protocol
• stream is of type uint8Array, which is a packet unit
• return uint32
var headerLength = headerLengthOf(stream)
3.2.4 headerOf(stream)
• Get the header in the packet protocol
• stream is of type uint8Array, which is a packet unit
• return object {}
var header = headerOf(stream)
3.2.5 urlPatternOf(stream)
• Get the length of the header in the packet protocol
• stream is of type uint8Array, which is a packet unit
• return string
var urlPattern = urlPatternOf(stream)
3.2.6 bodyLengthOf(stream)
• Get the body length in the packet protocol
• stream is of type uint8Array, which is a packet unit
• return uint32
var bodyLength = bodyLengthOf(stream)
3.2.7 bodyOf(stream)
• Get the body in the packet protocol
• stream is of type uint8Array, which is a packet unit
• return object {}
var body = bodyOf(stream)