我正在开展一个物联网项目。我将使用Mqtt进行设备之间的通信,我开始使用NodeJs和mosca运行简单的示例,并且它在我的本地linux机器上运行。
当我开始在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 mstfkhattab@gmail.com
2015-09-28T12:53:30.830200+00:00 heroku[api]: Release v20 created by mstfkhattab@gmail.com
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 :(得分:0)
它失败了,因为它期望Web进程绑定到process.env.PORT
,但是没有这样的进程。您包含http
模块但从不使用它。默认情况下,Heroku期望运行某种Web服务。
如果此应用程序没有网络进程(不应该监听端口),那么您应该创建一个Procfile
并指定您希望如何启动它,例如:
server: node server.js
有关Procfiles的更多信息: