javascript - 如何使用 Vue 连接到 TLS?
问题描述
我想使用 Vue 连接到 MQTT 服务器。我尝试了类似于我在谷歌上找到的东西,但失败了。
- node.js 示例
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:
解决方案
推荐阅读
- python - 使用 Python 进行混合流水车间调度的遗传算法
- java - 如何从 Spring Integration 的网关元素中的 default-reply-timeout 属性更改空值?
- android - 在android 7.0及以上版本中从URI获取路径
- javascript - 两国手机号码正则表达式怎么写?
- functional-programming - 有没有一种函子有一个功能(monad ,function A -> monad ) -> Monad
- java - 哪个版本的 eclipse IDE 与 java 10 兼容?
- android - aosp 存储库 android-7.1.2_r36 下载后不完整 42GB
- postgresql - 没有新列的表继承
- python - 如何使用 python Flask RESTplus 填充模型
- java - Java 运行时环境检测到致命错误:SIGSEGV (0xb) at pc=0x00007f3457bc96ae, pid=1192, tid=0x00007f345e1ff700