微信小程序真机调试问题

docker部署的

version: '3'

services:
  emqx1:
    image: emqx:5.1.3
    container_name: emqx1
    environment:
      - "EMQX_NODE_NAME=emqx@node1.emqx.io"
      - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
      - "EMQX_CLUSTER__STATIC__SEEDS=[emqx@node1.emqx.io,emqx@node2.emqx.io]"
    healthcheck:
      test: [ "CMD", "/opt/emqx/bin/emqx ctl", "status" ]
      interval: 5s
      timeout: 25s
      retries: 5
    networks:
      emqx-bridge:
        aliases:
          - node1.emqx.io
    restart: always
    ports:
      - 1883:1883
      - 8083:8083
      - 8084:8084
      - 8883:8883
      - 8003:18083
    volumes:
      - "$PWD/emqx.conf:/opt/emqx/etc/emqx.conf"
      #- $PWD/emqx1_data:/opt/emqx/data
  emqx2:
    image: emqx:5.1.3
    container_name: emqx2
    environment:
      - "EMQX_NODE_NAME=emqx@node2.emqx.io"
      - "EMQX_CLUSTER__DISCOVERY_STRATEGY=static"
      - "EMQX_CLUSTER__STATIC__SEEDS=[emqx@node1.emqx.io,emqx@node2.emqx.io]"
    healthcheck:
      test: [ "CMD", "/opt/emqx/bin/emqx ctl", "status" ]
      interval: 5s
      timeout: 25s
      retries: 5
    networks:
      emqx-bridge:
        aliases:
          - node2.emqx.io
    restart: always
    volumes:
      - "$PWD/emqx.conf:/opt/emqx/etc/emqx.conf"
    #   - $PWD/emqx2_data:/opt/emqx/data

networks:
  emqx-bridge:
    driver: bridge

修改了对应的配置文件

## NOTE:
## This config file overrides data/configs/cluster.hocon,
## and is merged with environment variables which start with 'EMQX_' prefix.
##
## Config changes made from EMQX dashboard UI, management HTTP API, or CLI
## are stored in data/configs/cluster.hocon.
## To avoid confusion, please do not store the same configs in both files.
##
## See https://www.emqx.io/docs/en/v5.0/configuration/configuration.html for more details.
## Configuration full example can be found in etc/examples

node {
  name = "emqx@127.0.0.1"
  cookie = "emqxsecretcookie"
  data_dir = "data"
}

cluster {
  name = emqxcl
  discovery_strategy = manual
}

dashboard {
    listeners.http {
        bind = 18083
    }
}

# 看官网配置手册上说微信小程序需要关闭这个,就把配置文件复制出来,然后单独添加了这个,其余配置项未作修改
ws_opts {
    fail_if_no_subprotocol = false
}

nginx做了反向代理

        # 将 mqtt 转发到 mqtt服务器
        location /mqtt {
                proxy_pass http://127.0.0.1:8083;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                # client_max_body_size 35m; 
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
        }

微信小程序连接代码

      // 建立 mqtt 链接 
      connectMqtt() {
            try {
                  const options = {
                        reconnectPeriod: 1000,
                        connectTimeout: 4000, // 超时时间
                        clientId: 'wx_' + parseInt(Math.random() * 100 + 800, 10),
                  }

                  const mqttUrl = `wxs://${app.globalData.mqttUrl}/mqtt`

                  const mqttClient = mqtt.connect(mqttUrl, options);
                  
                  mqttClient.on('error', function (error) {
                        console.log(error)
                        console.log("mqtt 连接 失败", error)
                  })

                  mqttClient.on('connect', function (connack) {
                        console.log('Connected')
                        console.log("mqtt 连接 成功")
                  })


                  this.setData({
                        mqttClient: mqttClient
                  })

            } catch (error) {
                  console.log("mqtt err: ", error)
            }


      },

微信开发者工具中连接正常

image

尝试操作

官网上说连接不上尝试切换mqtt版本,现在试过:

  • 4.2.2
  • 4.2.1
  • 4.2.0
  • 2.18.9 - 2.18.0

真机调试中打印mqttClient 连接状态是false,现在是一脸懵逼,实在是没有办法了,有没有大佬可以指点一下,或者教我怎么打印出来对应的哪里有报错也可以

emqx dashboard打开日志,微信小程序连接,看一下日志中是否连接