首页 > 解决方案 > 如何使用 Vue 连接到 TLS?

问题描述

我想使用 Vue 连接到 MQTT 服务器。我尝试了类似于我在谷歌上找到的东西,但失败了。

var mqtt = require('mqtt');
var fs = require('fs');

var options = {
  host: '127.0.0.1',
  port: 8883,
  protocol: 'mqtts',
  protocolId: 'MQIsdp',
  ca: [fs.readFileSync('<path>/ca.crt')],
  key: fs.readFileSync('<path>/client.key'),
  cert: fs.readFileSync('<path>/client.crt'),
  passphrase: 'CLIENT KEY PASSPHRASE',
  secureProtocol: 'TLSv1_method',
  protocolVersion: 3
};


var client = mqtt.connect(options);
const option = {
      port: 48883,
      ca: [],
      key: '',
      cert: '',
      clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8),
      protocolId: 'MQTT',
      useSSL: true,
      passphrase: 'CLIENT KEY PASSPHRASE',
      secureProtocol: 'TLSv1_method',
      protocolVersion: 4,
      rejectUnauthorized: false,
    }

    axios.get('/' + 'ca.crt').then(response => {
     option.ca = [response.data]
    })

    axios.get('/' + 'client.key').then(response => {
     option.key = response.data
    })

    axios.get('/' + 'client.crt').then(response => {
     option.cert = response.data
    })
    
    
    const client = mqtt.connect('wss://url', option)
failed: WebSocket is closed before the connection is established.
WebSocket connection to 'wss://url' failed: 

标签: javascriptsslmqtttls1.2

解决方案


推荐阅读