2015-09-28 174 views
0

我正在开发一个物联网项目。我将使用Mqtt来进行设备之间的通信,我开始使用NodeJs和mosca运行一个简单的例子,它在我的本地linux机器上运行。Mqtt Node Js部署在heroku崩溃上

当我开始在Heroku上部署时,遇到了崩溃问题。

这里是我的代码:

var mosca = require('mosca') 
var http = require('http'); 
var url = require('url'); 
var sys = require('sys'); 

var settings = { 
    port: 1883 || Number(process.env.PORT) 
}; 

//here we start mosca 
var server = new mosca.Server(settings); 
server.on('ready', setup); 

// fired when the mqtt server is ready 
function setup() { 
    console.log('Mosca server is up and running') 
} 

// fired whena client is connected 
server.on('clientConnected', function(client) { 
    console.log('client connected', client.id); 
}); 

// fired when a message is received 
server.on('published', function(packet, client) { 
    console.log('Published : ', packet.payload); 
}); 

// fired when a client subscribes to a topic 
server.on('subscribed', function(topic, client) { 
    console.log('subscribed : ', topic); 
}); 

// fired when a client subscribes to a topic 
server.on('unsubscribed', function(topic, client) { 
    console.log('unsubscribed : ', topic); 
}); 

// fired when a client is disconnecting 
server.on('clientDisconnecting', function(client) { 
    console.log('clientDisconnecting : ', client.id); 
}); 

// fired when a client is disconnected 
server.on('clientDisconnected', function(client) { 
    console.log('clientDisconnected : ', client.id); 
}); 

,这是Heroku的日志报告的崩溃:

State changed from crashed to starting 
2015-09-28T12:49:43.209288+00:00 heroku[web.1]: Starting process with  command `node index.js` 
2015-09-28T12:49:48.669918+00:00 app[web.1]: (node) sys is deprecated. Use util instead. 
2015-09-28T12:49:48.718428+00:00 app[web.1]: Mosca server is up and running 
2015-09-28T12:50:43.468112+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch 
2015-09-28T12:50:43.468112+00:00 heroku[web.1]: Stopping process with SIGKILL 
2015-09-28T12:50:44.373307+00:00 heroku[web.1]: State changed from starting to crashed 
2015-09-28T12:50:44.352908+00:00 heroku[web.1]: Process exited with status 137 
2015-09-28T12:53:30.899810+00:00 heroku[slug-compiler]: Slug compilation started 
2015-09-28T12:53:30.899837+00:00 heroku[slug-compiler]: Slug compilation finished 
2015-09-28T12:53:30.830161+00:00 heroku[api]: Deploy c237cb9 by [email protected] 
2015-09-28T12:53:30.830200+00:00 heroku[api]: Release v20 created by [email protected] 
2015-09-28T12:53:30.926174+00:00 heroku[web.1]: State changed from crashed to starting 
2015-09-28T12:53:33.726073+00:00 heroku[web.1]: Starting process with command `node index.js` 
2015-09-28T12:53:36.600467+00:00 app[web.1]: (node) sys is deprecated. Use util instead. 
2015-09-28T12:53:36.643097+00:00 app[web.1]: Mosca server is up and running 
2015-09-28T12:54:33.858449+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch 
2015-09-28T12:54:33.858449+00:00 heroku[web.1]: Stopping process with SIGKILL 
2015-09-28T12:54:34.801910+00:00 heroku[web.1]: State changed from starting to crashed 
2015-09-28T12:54:34.781887+00:00 heroku[web.1]: Process exited with status 137 
2015-09-28T12:54:36.720413+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=iot-mqtt-glinty-tutorial.herokuapp.com request_id=b74f96e4-0a38-405d-a3c6-38fd41c601b0 fwd="196.221.206.12" dyno= connect= service= status=503 bytes= 
2015-09-28T12:54:37.904668+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=iot-mqtt-glinty-tutorial.herokuapp.com request_id=19d27d3a-a478-47a0-8e3f-532d741d15ac fwd="196.221.206.12" dyno= connect= service= status=503 bytes= 

任何解决这个请???

回答

0

这是失败的,因为它期望一个web进程绑定到process.env.PORT,但没有这样的过程。您包含http模块,但从不使用它。默认情况下,Heroku预计会有某种Web服务在运行。

如果应用程序没有网络的过程(不应该监听端口),那么你应该创建一个Procfile,并指定您希望如何启动它,比如:

server: node server.js 

更多信息在Procfiles:

+0

看来,Procfile起着Heroku上有很大的作用,但问题是,Heroku的始终在索引文件中搜索默认的http/webservice –

+0

Heroku并不认为'索引'是任何特殊的。唯一特别的是在没有Procfile的情况下假设'web'服务。在没有向Heroku指出如何开始你想要开始的过程的情况下,Heroku还会做什么来启动你的应用程序?即,如果您不告诉Heroku如何启动您的应用程序,您希望应用程序启动系统执行什么操作? – hunterloftis