将Reactjs连接到Myqtthub [英] Connecting Reactjs to Myqtthub

查看:78
本文介绍了将Reactjs连接到Myqtthub的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我对所有这些物联网都是陌生的,我希望能够使用https://myqtthub.com 作为我们的经纪人.我使用以下代码进行连接:

Hello I'm new to all this IOT thing and I want to be able to send and receive data from an Arduino via mqtt using https://myqtthub.com as our broker. I used the following code in order to connect:

import React, { Component } from 'react';

var mqtt = require('mqtt');

var options={
    clientId:"mqttjs01",
    username:"user",
    password:"password",
    clean:true};
    var client = mqtt.connect("mqtt://node02.myqtthub.com",options)

client.on('connect', () => {
    alert('Connected!');
   
    console.log('Connected');
});




class Garage extends Component {
   
   
    render() {
        return (
            <div>
                <h1>HEllooo</h1>
            </div>
        );
    }
}

export default Garage;

我遇到了错误:与'ws://node02.myqtthub.com/'的WebSocket连接失败:WebSocket握手期间出错:意外的响应代码:301"

I'm having the error: "WebSocket connection to 'ws://node02.myqtthub.com/' failed: Error during WebSocket handshake: Unexpected response code: 301"

任何帮助将不胜感激.

推荐答案

假定这一切都在浏览器中运行(主要是因为MQTT.js强制通过ws://进行连接,因为该连接是ws://中的唯一选项)浏览器).

Assuming this is all running in the browser (mainly because MQTT.js is forcing the connection to be over ws:// as that is the ONLY option in the browser).

在查看myqtthub的文档时,他们没有要求Websockets支持MQTT(以及

Looking at the docs for myqtthub, they do NOT claim any support for MQTT over Websockets (and the support forum says they don't support it yet). This means your application will NOT work with this broker provider until they add Websockets support..

这篇关于将Reactjs连接到Myqtthub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆