2015-09-28 200 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