MQTT mosca服务端和客户端搭建

mosca node服务器端代码

1、创建index.js 安装依赖mosca

npm init 
npm install mosca --save
touch index.js
```

Index.js
```
var mosca = require("mosca");

var server = new mosca.Server({
  http: {
    port: 3000,
    bundle: true,
    static: './'
  }
});

server.on('ready', function(){
    console.log('mqtt server started');
});

server.on('published', function(packet, client){
    console.log('Published: ', packet.payload);
})

server.on('subscribed', function(topic, client){
     console.log('subscribed: ', topic);
});

server.on('unSubscribed', function(topic, client){
     console.log('unSubscribed: ', topic);
})

server.on('clientConnected', function(client){
    console.log('client connected: ', client.id);
});

server.on('clientDisConnected', function(client){
    console.log('client disConnected: ' + client.id + " userNumber:" + usermap.keys.length);
});
```

> mosca node客户端代码

```
var mqtt    = require('mqtt');
var client  = mqtt.connect('mqtt://127.0.0.1:1883');

client.on('connect', function () {
  client.subscribe('presence');
  client.publish('presence', 'Hello mqtt');
});

client.on('message', function (topic, message) {
  // message is Buffer
  console.log(message.toString());
  client.end();
});
```

> mosca html客户端代码

1、生成browserMqtt.js ,拷贝出来,放到和index.html目录下面
```
cd node_modules/mqtt
npm install .
 webpack mqtt.js ./browserMqtt.js --output-library mqtt
```
2、创建index.html
* 注意:html客户端的地址是ws://localhost:3000,这个ws开头表示是websocket的地址,端口号是3000,不是

```
<html>
  <head>
    <script src="./browserMqtt.js"></script>
  </head>
  <body>
    <script>
        console.log('hello 0 ');
      var client = mqtt.connect("ws://10.8.5.79:3000");
      console.log('hello 1');
      client.subscribe("mqtt/demo");
      console.log('hello 12');
      client.on("message", function(topic, payload) {
        alert([topic, payload].join(": "));
        client.end();
      });

      client.publish("mqtt/demo", "hello world!");
    </script>
  </body>
</html>
```

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容