【问题标题】:How to use Secured Web Sockets with mosquitto and JS?如何在 mosquitto 和 JS 中使用安全 Web 套接字?
【发布时间】:2018-04-11 12:20:00
【问题描述】:

Paho JavaScript Client 的帮助下,我有一个 JS 前端连接到 MQTT 服务器 (mosquitto)。这很好用,我可以发布和收听主题。

我现在想将 Web Sockets 连接提升为安全连接。

为此,我在/etc/mosquitto/mosquitto.conf 中添加了certfilekeyfile 的条目:

pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
listener 1883
listener 1884
protocol websockets
# above is the working, non-wss configuration
certfile /etc/mosquitto/wildcard.crt
keyfile /etc/mosquitto/wildcard.key

如果这是正确的配置,如何在JS中创建一个考虑安全连接的客户端?对于现有的,我有

client = new Paho.MQTT.Client("10.10.10.10", 1884, Math.random().toString(16).substring(7))

并且没有看到任何其他要传递的配置?

【问题讨论】:

    标签: javascript websocket mqtt mosquitto paho


    【解决方案1】:

    来自docs

    您有多种选择:

    1. 在声明新客户端时,host 可以是 URI 而不仅仅是 ip/主机名

      client = new Paho.MQTT.Client("wss://10.10.10.10:1884", Math.random().toString(16).substring(7));
      
    2. 您可以在client.connect(options) 选项对象中使用useSSL 标志。

      client.connect({
        useSSL: true
       });
      

    【讨论】:

    • 我尝试了第一个选项,但最终被用作ws://wss://10.10.10.10:1844/mqtt,这是不正确的。我错过了 doczs 中的第二个选项 - 谢谢!
    • 那是个bug,你应该提出问题(或检查你是否有最新版本)
    猜你喜欢
    • 1970-01-01
    • 2018-12-02
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 2021-09-21
    • 2021-06-26
    相关资源
    最近更新 更多