MQTT.js是一个支持多种网络协议的mqtt库,支持Node.js 和 browser,在此基础支持我又新增了egret和layabox两种协议
目前MQTT.js支持库种类: tcp tls(ssl) ws (cocos2d,H5) wss wx (微信小程序/小游戏) wxs egret egrets laya layas
引用库
https://github.com/liangdas/MQTT.js/blob/master/browserMqtt.js 游戏开发的中可以直接在html中引用这个已编译好的库
测试示例
https://github.com/liangdas/MQTT.js/blob/master/examples/mqtt.html
cocos 微信小程序
cocos 和 微信小程序可以用umd库的方式引用mqtt库 https://github.com/liangdas/MQTT.js/blob/master/umdMqtt.js
注意事项
//不能直接传json,mqtt.js库没有自动转义 // client.publish(‘Login/HD_Login/1’, { // userName: ‘mqtt’, // passWord: ‘Hello,anyone!’ // });
egret中如何使用
var client = mqtt.connect(“egret://127.0.0.1:3653”,{ protocolId: ‘MQIsdp’, protocolVersion: 3, clientId:‘mqttjs_’ + Math.random().toString(16).substr(2, 8), reconnectPeriod:0, //不自动重连 }) // you add a ws:// url here