简体   繁体   English

在浏览器中尝试连接 mqtt

[英]Trying connection mqtt in browser

I'm trying to get a mqtt connection on my browser with JS我正在尝试使用 JS 在我的浏览器上建立 mqtt 连接

I'm following this tutorial: https://emqx.medium.com/use-websocket-to-connect-to-mqtt-broker-9e7baf1aa773我正在关注本教程: https : //emqx.medium.com/use-websocket-to-connect-to-mqtt-broker-9e7baf1aa773

So I've got this:所以我有这个:

<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript"></script>
<script>
    // Globally initializes an mqtt variable    
    const clientId = 'mqttjs_' + Math.random().toString(16).substr(2, 8)
    
    
    const host = 'ws://broker.***.***.com:9883'
    
    const options = {
        keepalive: 60,
        clientId: clientId,
        username: '***',
        password: '***',
        protocolId: 'MQTT',
        protocolVersion: 4,
        clean: true,
        reconnectPeriod: 1000,
        connectTimeout: 30 * 1000,
        
        will: {
            topic: 'WillMsg',
            payload: 'Connection Closed abnormally..!',
            qos: 0,
            retain: false
        },
    }
    
    console.log('Connecting mqtt client')
    const client = mqtt.connect(host, options)
    
    client.on('connect', () => {
        console.log('Client connected:' + clientId)
        // Subscribe
    })
</script>

And in my browser I've got this error:在我的浏览器中,我收到了这个错误: 在此处输入图片说明

After some research, some people say that need to use certificate: https://github.com/eclipse/paho.mqtt.javascript/issues/187经过一番研究,有人说需要使用证书: https : //github.com/eclipse/paho.mqtt.javascript/issues/187

So, I've got this :所以,我有这个:

<script src="../browserMqtt.js"></script>
<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>

<script>
            var options = {
                keyPath: '../credentials/client-key.pem',
                certPath: '../credentials/client-cert.pem',
                rejectUnauthorized : false, 
                ca: ['../credentials/a-cert.pem'],
                protocolId: 'MQTT',
                username: '***',
                password: '***',
                clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8)
            };

            var client = mqtt.connect('ws://broker.***.***.com:9883',options);

            client.on('connect', function(){
                console.log('Connected');
            });    
                    
</script>

I've got the same error in browser ...我在浏览器中遇到了同样的错误...

The broker conguration for mosquitto, it's like this : mosquitto 的 broker 配置是这样的:

allow_anonymous false
password_file /mosquitto/config/passwd

#TCP
listener 1883
socket_domain ipv4

#SSL
listener 8883
socket_domain ipv4
cafile /mosquitto/config/tls/ca/ca-cert.pem
certfile /mosquitto/config/tls/server/server-cert.pem
keyfile /mosquitto/config/tls/server/server-key.pem
tls_version tlsv1.2
socket_domain ipv4

#WSS
listener 9883
socket_domain ipv4
protocol websockets
cafile /mosquitto/config/tls/ca/ca-cert.pem
certfile /mosquitto/config/tls/server/server-cert.pem
keyfile /mosquitto/config/tls/server/server-key.pem
tls_version tlsv1.2

persistence true
persistence_location /mosquitto/data/

log_dest file /mosquitto/log/mosquitto.log
log_timestamp_format %Y-%m-%dT%H:%M:%S
log_type all

I can't understand how can I solve it ?我不明白我该如何解决? Thanks for your help谢谢你的帮助

You can't use client side certs in the browser to authenticate the client (unless you load them into the browsers keystore, but even then I'm not convinced it will work unless there is only one cert/key for the browser to pick as javascript code won't normally prompt the user to pick the right one).您不能在浏览器中使用客户端证书对客户端进行身份验证(除非您将它们加载到浏览器密钥库中,但即使如此,我也不相信它会起作用,除非浏览器只有一个证书/密钥可供选择) javascript 代码通常不会提示用户选择正确的)。

Also loading client certs over http from the server totally defeats the point of using a client cert as anybody can download them.此外,从服务器通过 http 加载客户端证书完全违背了使用客户端证书的意义,因为任何人都可以下载它们。

You need to remove all of the following from the options您需要从options删除以下所有内容

keyPath: '../credentials/client-key.pem',
certPath: '../credentials/client-cert.pem',
rejectUnauthorized : false, 
ca: ['../credentials/a-cert.pem'],
protocolId: 'MQTT',

Because the paths are meaningless in the browser (and for the reasons I mentioned earlier)因为路径在浏览器中没有意义(以及我之前提到的原因)

You should also be starting your broker URL with wss:// to make it clear you are trying to connect over secure WebSockets.您还应该使用wss://开始您的代理 URL,以表明您正在尝试通过安全的 WebSockets 进行连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM