瞎折腾气到肺炸 mqtt

Nodejs cyanprobe 7年前 (2017-05-04) 5510次浏览 已收录 3个评论

前言:

mqtt(Message Queuing Telemetry Transport),pomelo不是支持了么,闲来没事看到有中文文档想撸一发,没想到活活被气死。。。

正文:

传送门 https://mcxiaoke.gitbooks.io/mqtt-cn/content/mqtt/0301-CONNECT.html

const net = require('net');
let server = net.createServer();
server.on('connection', function(sock) {
sock.on('data', function(data) {
    console.log(data.toString())
    let index=0
    let frame={
        packetType: (data[index]>>4)&0xF,
        flags:(data[index++]&0x2<<4)&0xF,
        remaining:getRemain(),
        variableHeader:{
            protocolName:(data.slice(index+2,index+6).toString()),
            protocolLevel:(data[index+7]),
            connectFlags:data[index+8]&&0xff,
            keepAlive:data.slice(index+9,index+13)
    }
    }
    function getRemain() {
        let byteLength=1;
        let value=0;
        do{
            value+=byteLength*(data[index]&0x7f)
            index++
            byteLength *= 128
        } while ((data[index]&0x80)==0x80)
        if(byteLength>Math.pow(128,3)){
            throw Error("剩余长度字段异常")
        }
        return value
    }
console.log(frame,index)
    const array=[];
    array.push('0X20','0X2')
    const buf=Buffer.from(array)
});
});
server.listen(1883);
//打印
 { protocolName: 'MQTT',
 protocolLevel: 194,
 connectFlags: 0,
 keepAlive: <Buffer 3c 00 10 61> } }

protocolLevel,和connectFlags匹配不上,直接炸了。协议版本匹配不上最奇葩,已经疯了找不到bug,准备自杀。


CyanProbe , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:瞎折腾气到肺炸 mqtt
喜欢 (1)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(3)个小伙伴在吐槽
  1. 自杀前请先找找你的项目经理以及这个语言的开发者。。
    姜辰2017-05-05 10:43 回复
  2. 博主可以直接用3.0CN,有法律效益了的
    神隐者2017-05-09 23:51 回复