個人測試環境:html
Linux: CentOS7web
EMQX:V3.2.3安全
題外話:socket
這裏主要介紹Websocket的支持問題。工具
對ws的支持比較正常,可是對wss的支持,調了較長的時間,沒有成功。測試
JavaScript支持mqtt的工具備paho.mqtt.js和mqtt.js. 我這裏爲了研究wss,因此,重點採用的是mqtt.js.基於這兩種工具包,ws的通訊,我這裏都很順利的調通了,通訊沒有問題。ui
看代碼:this
由於paho.mqtt.js沒有看到有支持SSL的地方(至少從庫裏面的配置參數上,沒有看到。。。),基於mqtt.js的wss配置,是有參數能夠支持的,因此重點研究的是mqtt.js的使用。下面是個人wss代碼。spa
<html> <head> <title>Ws mqtt.js</title> <script src="./js/mqtt-3.0.0.min.js"></script> </head> <body> <script> var myca = "-----BEGIN CERTIFICATE-----"+ "MIIDhDCCAmygAwIBAgIGAW2+wl6XMA0GCSqGSIb3DQEBCwUAMFcxFDASBgNVBAMTC0lPVFBMQVRG"+ "T1JNMQswCQYDVQQLEwJJVDELMAkGA1UEChMCVEsxCzAJBgNVBAcTAldIMQswCQYDVQQIEwJIQjEL"+ "MAkGA1UEBhMCQ04wHhcNMTkxMDEyMDY1NzUyWhcNNDkxMDEyMDY1NzUyWjBXMRQwEgYDVQQDEwtJ"+ "T1RQTEFURk9STTELMAkGA1UECxMCSVQxCzAJBgNVBAoTAlRLMQswCQYDVQQHEwJXSDELMAkGA1UE"+ "CBMCSEIxCzAJBgNVBAYTAkNOMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoBcnvo/P"+ "XT9keD5NC6nviy12hFQ7W+YCTMZJiGgi2lGI9uytYivzom7+WshLMqrUwE3E2BCTiLmGGGHv+GTd"+ "kkGbpF0wsZPpefTBw0czQNOQgMIKOw1t11GijakHvuzJKgRofjyfew2YqF+cBxOM/f0xhExfjft8"+ "qLUUJ80w9VEJDeBhmE20esH9if0KYfjHOJiw5h1p4qG/W6xIZhSm22bgsBGd75zE7HmYWxO2bSwb"+ "nt7boqk9Vyyeg/6XCujH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nZUxkPW8YXFHyU17UhW4G28va"+ "qGf6lT1vbqY3yCzyUJpEifLN18u7xwS7lATHwtU0uVBMRTSt6B0sVOIz+QKBgHYBykyiu2rwvqd9"+ "oN/ekZ1EEmjGdRMHfJ94y/IUfab4CMZFS6ktNVUs1MW5ZQLLvB98e+/SPZ06hU9JgAupepJ8Ezqk"+ "RZjqMi8IHvWz6QG3cE78U8/JExIO5WKboJaABet0MWci3H+f9xDMDbE1dGGJ5KLh+KZ2u7SrWGNT"+ "yoXHAoGBAKGwZM4/ZUOVnl1nMDg+m96A9+VaGT/d8mxImTDdDmEuarjmGuiqqygWMMJirgQDovsD"+ "BTZP3/a5N3TmqVt4RP+5Sd5T2svlC536etHOIltuyXeJSsYSrk0ifvdG94ePhlGBrWLt8wxKEaG6"+ "BLCty7md7EUHxNS19Oa4BV7JVN9t"+ "-----END PRIVATE KEY-----"; var options = { protocolId: 'MQTT', protocolVersion: 4, clean: true, reconnectPeriod: 1000, connectTimeout: 30 * 1000, clientId: 'emqx_mqtt_js001', username: 'shihuc', password: 'shihuc', keepalive: 50, rejectUnauthorized: true, //若不是wss,這個參數改成false,而後下面的三個參數都不須要出如今options裏面 key: mykey, cert: mycert, // The CA list will be used to determine if server is authorized ca: myca } const sendTopic = "kkk/send"; const recvTopic = "kkk/recv"; const client = mqtt.connect('wss://10.95.200.13:8084/mqtt', options); //ws模式下,將protocol改成ws,端口改成8083 // 鏈接創建後,能夠在此處設置應用邏輯做 client.on('connect', function () { client.subscribe(recvTopic, {qos: 0}); client.publish(sendTopic, "connection is successful!", {qos: 0, retain: false }) ; }) // 在這裏能夠添加業務邏輯,正常接收消息 client.on("message", function (topic, payload) { console.log([topic, payload].join(": ")); }) // 重連成功,在這裏設置重連的業務邏輯 client.on('reconnect', (error) => { console.log('正在重連:', error); }) // 這裏顯示鏈接錯誤的信息,能夠添加業務邏輯 client.on('error', (error) => { console.log('鏈接失敗:', error); client.end(); }) function send() { var s = document.getElementById("msg").value; if (s) { s = "{time:" + new Date().Format("yyyy-MM-dd hh:mm:ss") + ", content:" + (s) + ", from: web console based on mqtt.js}"; client.publish(sendTopic, s); document.getElementById("msg").value = ""; } } var count = 0; function start() { window.tester = window.setInterval(function () { if (client.connected) { var s = "{time:" + new Date().Format("yyyy-MM-dd hh:mm:ss") + ", content:" + (count++) + ", from: web console based on mqtt.js}"; client.publish(sendTopic, s); } }, 1000); } function stop() { window.clearInterval(window.tester); } Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小時 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } </script> <input type="text" id="msg" /> <input type="button" value="Send" onclick="send()" /> <input type="button" value="Start" onclick="start()" /> <input type="button" value="Stop" onclick="stop()" /> </body> </html>
一直不成功,雙向驗證的時候,老是報服務端證書不對。prototype
這個問題,讓我想起來,以前研究EMQX的CoAP協議在DTLS安全通訊模式下不工做的問題了。是EMQX目前不支持麼?
有知道的看官,能夠留言支招,若你有任何想法。。。