使用JavaScript MQTT Websockets客戶端-(分步示例)



Web瀏覽器可以使用http和websockets協議,但不能使用MQTT。 為了能夠使用瀏覽器發布和訂閱MQTT代理,您將需要通過websockets JavaSript客戶端使用MQTT。 這是涵蓋MQTT Javascript客戶端的簡短視頻系列的第一部分,其中我們將使用非常簡單的腳本通過MQTT和websocket發布和接收消息。 我將逐步引導您完成此腳本,並解釋腳本的重要元素。 該腳本從Internet上通過Websockets腳本引入了MQTT,但是您也可以下載該腳本並將其安裝在本地。 我們從發布數據的簡單腳本開始,然後擴展此腳本以通過MQTT訂閱和接收數據。 我們檢查經紀人方並觀察客戶的連接,訂閱和發布。 消息已記錄到控制台,我將向您展示如何使用devrloper工具在Firefox中查看這些消息。 有關在腳本中使用document.wite()函數嘗試並顯示消息的警告。 它不起作用,所以不要這樣做。 網站上的教程http://www.steves-internet-guide.com/using-javascript-mqtt-client-websockets/其他視頻通過Websockets上的MQTT解釋了https://youtu.be/EvUI4vRhF88使用JavaScript MQTT客戶端-Example2 https ://youtu.be/_6_5ubPfm8U您可以在此處下載腳本http://www.steves-internet-guide.com/downloads/#examples有問題請使用注釋,或者如果需要幫助,請使用http:// www .steves-internet-guide.com / ask-steve /如果您發現這些視頻有用,那麼您可能要考慮向我購買咖啡https://www.paypal.me/StepenCope。

14 comments
  1. I know it's been two years since you published that. I'm a software engineer and as a sign of appreciation for this video I'm offering to help you with web technologies (html, Javascript, css, etc.) so you can build better examples for your audience.

    No catch, just a thank you,
    Jack

  2. Hello.
    getting this error:
    WebSocket connection to 'ws://192.168.0.105:1883/mqtt' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET
    that's my laptop IPv4 address checked in cmd (ip config) and that's my Mosquitto broker port, have no idea how to fix it, please help

  3. Hi Steve, thank you very much for these tutorials. A year ago, I used to watch your videos when I was learning Node-Red and MQTT, now because I have to stay at home I'm learning html and JS and I fall in your channel again xD. So thank you again.

  4. Excellent tutorial! I was able to publish and read the posts. BUT I ran into a problem. Example: I have a value of 0 and 1 in the topic. I would like to see when entering the page what value is in the topic and shown me on the page. I think I have to use the retain function but I didn't know how to implement in the code you passed. Would you help me ? Thanks for the help and sorry my english!

Comments are closed.