无法连接到远程服务器上的HiveMQ

时间:2020-02-26 09:36:56

标签: node.js nginx mqtt hivemq

我通过HiveMQ在远程服务器上运行了自己的MQTT Broker。我设置了Nginx,并且仪表板在http://104.251.210.224上可以正常工作。但是我无法通过发布者或订阅者连接到它。 在以某种类似mqtt://broker.hivemq.com:1883的方式连接到HiveMQ沙箱之前,但是我无法在自己的服务器上执行此操作。 我该怎么办?

HiveMQ日志

2020-02-26 07:29:07,819 INFO  - Extension "Allow All Extension" version 1.0.0 started successfully.
2020-02-26 07:29:09,952 INFO  - 5EQfP: no members discovered after 2001 ms: creating cluster as first member
2020-02-26 07:29:09,989 INFO  - No user for HiveMQ Control Center configured. Starting with default user
2020-02-26 07:29:09,990 INFO  - Starting HiveMQ Control Center on address 127.0.0.1 and port 8080
2020-02-26 07:29:10,217 INFO  - Control Center Audit Logging started.
2020-02-26 07:29:10,217 INFO  - Started HiveMQ Control Center in 227ms
2020-02-26 07:29:10,230 INFO  - Starting TCP listener on address 0.0.0.0 and port 1883
2020-02-26 07:29:10,269 INFO  - Started TCP Listener on address 0.0.0.0 and on port 1883
2020-02-26 07:29:10,269 INFO  - Started HiveMQ in 6383ms

Ngnix

server {
    listen      80;
    client_max_body_size 20M;
    server_name xxxxx.io www.xxxxx.io;
    error_log  /var/log/apache2/domains/xxxxx.io.error.log error;

    location / {
        proxy_pass      http://0.0.0.0:8080;
        location ~* ^.+\.(jpg|jpeg|gif|png|ico|svg|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|odt|ods|odp|odf|tar|wav|bmp|rtf|js|mp3|avi|mpeg|flv|html|htm)$ {
            root           /home/admin/web/mqtt/hivemq-4.3.1/;
            access_log     /var/log/apache2/domains/xxxxx.log combined;
            access_log     /var/log/apache2/domains/xxxxx.bytes bytes;
            expires        max;
            try_files      $uri @fallback;
        }
    }


    location @fallback {
        proxy_pass      http://0.0.0.0:8080;
    }

    location ~ /\.ht    {return 404;}
    location ~ /\.svn/  {return 404;}
    location ~ /\.git/  {return 404;}
    location ~ /\.hg/   {return 404;}
    location ~ /\.bzr/  {return 404;}

    include /home/admin/conf/web/nginx.xxxxxx.io.conf*;
}

订户

const mqtt = require('mqtt')
options = {
  port: 1883,
  host: 'mqtt://104.251.210.224',
  // clientId: "alireza"
}
const client = mqtt.connect('mqtt://104.251.210.224', options)

/**
* The state of the doorphone, defaults to closed
* Possible states : closed, opening, open, closing
*/
// console.log(client)

var state = 'closed'

client.on('connect', () => {
  console.log(client.connected)
  client.subscribe('doorphone/open')
  client.subscribe('doorphone/close')

  // Inform controllers that doorphone is connected
  client.publish('doorphone/connected', 'true')
  sendStateUpdate()
})

client.on('message', (topic, message) => {
    console.log('received message %s %s', topic, message)
    switch (topic) {
        case 'doorphone/open':
          return handleOpenRequest(message)
        case 'doorphone/close':
          return handleCloseRequest(message)
      }
})

function sendStateUpdate () {
    console.log('sending state %s', state)
    client.publish('doorphone/state', state)
}


function handleOpenRequest (message) {
    if (state !== 'open' && state !== 'opening') {
      console.log('opening doorphone door')
      state = 'opening'
      sendStateUpdate()

      // simulate door open after 5 seconds (would be listening to hardware)
      setTimeout(() => {
        state = 'open'
        sendStateUpdate()
      }, 5000)
    }
}


function handleCloseRequest (message) {
    if (state !== 'closed' && state !== 'closing') {
      state = 'closing'
      sendStateUpdate()

      // simulate door closed after 5 seconds (would be listening to hardware)
      setTimeout(() => {
        state = 'closed'
        sendStateUpdate()
      }, 5000)
    }
}



/**
 * Want to notify controller that doorphone is disconnected before shutting down
 */
function handleAppExit (options, err) {
    if (err) {
      console.log(err.stack)
    }

    if (options.cleanup) {
      client.publish('doorphone/connected', 'false')
    }

    if (options.exit) {
      process.exit()
    }
  }

  /**
   * Handle the different ways an application can shutdown
   */
  process.on('exit', handleAppExit.bind(null, {
    cleanup: true
  }))
  process.on('SIGINT', handleAppExit.bind(null, {
    exit: true
  }))
  process.on('uncaughtException', handleAppExit.bind(null, {
    exit: true
  }))

2 个答案:

答案 0 :(得分:4)

答案可以在HiveMQ Community Forum

中找到

不过,您的问题的简短答案:您的代理配置 缺少MQTT端口(1883)的配置,您仅配置了 仪表板端口(8080)。

您需要一个看起来像这样的设置:

upstream hivemq {
    server 127.0.0.1:1883;
}

server {
    listen 1883;
    proxy_pass hivemq;
}

答案 1 :(得分:0)

此解决方案解决了我的问题。这一点很重要,可以从外部了解和访问您的端口,这可以通过操作系统防火墙配置来完成,对于我来说,ubuntu具有名为ufw的防火墙配置工具。

$ sudo apt install ufw

$ sudo ufw enable

$ sudo ufw allow 1883/tcp

$ sudo ufw status verbose
相关问题