• MQTT之https页面请求问题


    网站开启了https,开始总会遇到各种问题,用户登入认证失败,视频请求失败,mqtt连接失败等问题。是不是很不爽,来看看,教你怎么解决这些问题。
    1.网站开启https,mqtt连接失败
    解决过程,开启activemq的ssl加密传输,前端直接请求wss://192.168.1.10:6165,注意6165是我开启了mqtt的ssl端口

            <!--
                The transport connectors expose ActiveMQ over a given protocol to
                clients and other brokers. For more information, see:
    
                http://activemq.apache.org/configuring-transports.html
            -->
            <transportConnectors>
                <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
                <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
                <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
                <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
                <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
                <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
           <!--     <transportConnector name="wss" uri="wss://0.0.0.0:61615?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>  -->
            </transportConnectors>
    
            <!-- destroy the spring context on shutdown to stop jetty -->
            <shutdownHooks>
                <bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
            </shutdownHooks>
    
       <!--  
           <sslContext>
              <sslContext keyStore="file:${activemq.conf}/broker1.ks"
                    keyStorePassword="adminadmin" trustStore="file:${activemq.conf}/broker1.ts"
                    trustStorePassword="adminadmin"
              />
           </sslContext>
       -->
    
        </broker>
    
    

    此方案最后失败了。

    解决方案
    借鉴用户认证思想,使用nginx反向代理到没有开启ssl的mqtt服务器
    网站前端mqtt请求:wss://192.168.1.10/mqtt/

    <template>
      <div class="hello">
        <p>MQTT:{{msg}}</p>
        <button @click="handleclick">发布</button>
      </div>
    </template>
    
    <script>
    import mqtt from 'mqtt'
    
    export default {
      name: 'HelloWorld',
     data() {
        return {
          mtopic: "mqtt_topic",
          msg: "test-test-test!",
          client: {}
        };
      },
      mounted() {
        this.client = mqtt.connect('wss://192.168.1.10/mqtt/', {
          username: "88888888",
          password: "88888888"
        });
        this.client.on("connect", () =>{
          console.log("连接成功");
          this.client.subscribe(this.mtopic, (err)=> {
            if (!err) {
              console.log("订阅成功:" + this.mtopic);
            }
          });
        });
        this.client.on("message", (topic, message) => {
          this.msg = message
        });
      },
      methods: {
        handleclick: function() {
          this.client.publish(this.mtopic, this.msg);
        }
      }
    }
    </script>
    <style scoped>
    </style>
    

    nginx反向代理配置

              location /mqtt/ {
                    proxy_pass http://192.168.1.10:61614/;
                    proxy_set_header   Host             $host;
                    proxy_set_header   X-Real-IP        $remote_addr;
                    proxy_set_header   X-Forwarded-For  $remote_addr;
                    proxy_set_header   X-Forwarded-Proto http;
    
                    proxy_http_version 1.1;
                    proxy_set_header Upgrade $http_upgrade;
                    proxy_set_header Connection "upgrade";
            }
    
    
  • 相关阅读:
    Unity《ATD》塔防RPG类3D游戏架构设计(一)
    计算机网络基础笔记 运输层协议UDP/TCP
    空间划分的数据结构(四叉树/八叉树/BVH树/BSP树/k-d树)
    游戏设计模式——内存池管理
    Unity 用ml-agents机器学习造个游戏AI吧(2) (深度强化学习入门DEMO)
    博客部署设计和构建
    教你如何把浏览器变为浏览“神器”
    珍藏多年的学习资料300G+,赶紧免费领取,从此离大神更进一步(文末有彩蛋)
    图解一致性哈希算法,全网(小区局域网)最通俗易懂
    SpringBoot整合Mail发送邮件&发送模板邮件
  • 原文地址:https://www.cnblogs.com/InternetJava/p/15731312.html
Copyright © 2020-2023  润新知