mqtt的简单使用

Posted 申小贺

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mqtt的简单使用相关的知识,希望对你有一定的参考价值。

服务器与浏览器通信,服务器端需要建立mosca服务和http服务,浏览器端连接到http服务端口

服务器端:

方式一:

var mosca = require(‘mosca‘);  
var MqttServer = new mosca.Server({  
    port: 2000,
    http: {
        port: 3006,
        bundle: true,
        static: ‘./‘
        } 
}); 

  
MqttServer.on(‘clientConnected‘, function(client){  
    console.log(‘client connected‘, client.id);  
});  
  
/** 
 * 监听MQTT主题消息 
 **/  
MqttServer.on(‘published‘, function(packet, client) {  
    var topic = packet.topic;  
    console.log(‘message-arrived--->‘,‘topic =‘+topic+‘,message = ‘+ packet.payload.toString());  
   //MQTT转发主题消息
   //MqttServer.publish({topic: ‘test‘, payload: ‘sssss‘});
});  
  
MqttServer.on(‘ready‘, function(){  
    console.log(‘mqtt is running...‘);  
    //MqttServer.authenticate = authenticate;  
});  

  方式二:

var http     = require(‘http‘)
  , httpServ = http.createServer()
  , mosca    = require(‘mosca‘)
  , mqttServ = new mosca.Server({port: 2000});

mqttServ.attachHttpServer(httpServ);

httpServ.listen(3006);
MqttServer.on(‘clientConnected‘, function(client){  
    console.log(‘client connected‘, client.id);  
}); 

  浏览器客户端://端口为http端口 mqtt://localhost:3006 和 ws://localhost:3006一样, 

                        web页面没有其他要求,没有跨域问题

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        <!--[if lt IE 7]>
            <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
        <![endif]-->
        <div>hello mqtt</div>
        <script src="../public/js/jquery-1.9.1.min.js"></script>
    
        <script src="../public/mqtt.min.js"></script>
        <script>
            ////端口为http端口 mqtt://localhost:3006 和 ws://localhost:3006一样
           var client  = mqtt.connect(‘ws://localhost:3006‘,{  

            });  
 // you add a ws:// url here
  client.subscribe("mqtt/demo");
  client.on(‘connect‘, function () {  
    console.log(‘connected.....‘);  
    // client.subscribe(‘test/‘);  
    // client.publish(‘app2dev/‘, ‘Hello mqtt‘);  
});  
  client.on("message", function (topic, payload) {
    alert([topic, payload].join(": "));
    
  })
 
  client.publish("mqtt/demo", "hello world!");
  </script>
    </body>
</html>

  

以上是关于mqtt的简单使用的主要内容,如果未能解决你的问题,请参考以下文章

接口自动化支持插件扩展,提供MQTT插件,MeterSphere开源持续测试平台v1.13.0发布

MQTT---HiveMQ源码详解配置加载

M2Mqtt 库未发布消息,线程以代码 259 退出

基于MQTT协议实现Broker

mqtt的简单使用

如何在 Flask 项目中使用 MQTT