1

const express    = require('express');
const helmet     = require('helmet');
const bodyParser = require('body-parser');
const morgan     = require('morgan');
const path       = require('path');
const server     = require('./modules/server');
const dashboard  = require('./modules/dashboard');
const config = require('./config');
const routes = require('./routes');

const app  = express();

app.use(helmet()); // secure http communication middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(morgan('tiny')); // http request logger
app.use('/', routes);
app.use('/public', express.static(path.join(__dirname, '/public')));

// Serve the Parse API on the /parse URL prefix
const mountPath = process.env.PARSE_MOUNT || '/parse';
app.use(mountPath, server);
app.use('/dashboard', dashboard);

/**
 * Mosca Server Settings below
 */
var mosca = require('mosca');
var http = require('http');
var httpServer = http.createServer(app);

var moscaSettingss = {
  type: 'mongo',
  url: 'mongodb://***:31157/mqtt',
  pubsubCollection: 'pub_sub',
  mongo: {}
};

httpServer= new mosca.Server({
  backend: moscaSettingss,
  persistence: {
    factory: mosca.persistence.Mongo,
    url: 'mongodb://***:31157/mqtt'
  }
}, function() {
  httpServer.attachHttpServer(app);
});

httpServer.on('ready', function() {
  console.log('Mosca is running');
});

httpServer.on('clientConnected', function(client) {
  console.log('client connected', client.id);
});

httpServer.on('published', function(packet, client) {
  console.log('Published : ', packet.payload);
});

httpServer.on('subscribed', function(topic, client) {
  console.log('subscribed : ', topic);
});

httpServer.on('unsubscribed', function(topic, client) {
  console.log('unsubscribed : ', topic);
});

httpServer.on('clientDisconnecting', function(client) {
  console.log('clientDisconnecting : ', client.id);
});

httpServer.on('clientDisconnected', function(client) {
  console.log('clientDisconnected : ', client.id);
});

/**
 * End of Mosca Server Settings
 */

app.listen(config.server.port, () => {
  console.log(`Magic happens on port ${config.server.port}`);
});

module.exports = app;

我制作了一个快递应用程序,其中包含 mosca mqtt 代理。有些我无法连接到那个 mqtt。尝试了所有可能的网址。

在 express 应用中,我做了一个 mqtt 客户端,成功连接到 broker。但是在 Heroku 之外,我的设备无法连接。

等待回应。

4

2 回答 2

0

Heroku 不会打开 MQTT 所需的端口。这就是为什么 express 可以在本地访问它,但您的任何设备都不能。

于 2016-12-21T19:35:37.390 回答
0

我也尝试 ws://mevris-cloud.herokuapp.com:80。有用!

于 2017-01-17T07:05:46.127 回答