我正嘗試在託管服務「dotcloud」上託管nodejs應用程序。 我的nodejs使用包「websocket」來處理通信。 即。 npm install websocket在dotcloud上託管nodejs服務器
我的應用程序在筆記本電腦上運行本地主機時效果很好。但是,當我在dotcloud上部署應用程序時,它無法正常工作。
這裏是正在發生的事情:你的瀏覽器指向的URL dotcloud: pirate-captainlonate.dotcloud.com
然後,快速處理與express.get( '/' GET請求.. .....){} express按照您的預期將.html頁面提供給客戶端。 .html文件 反過來嘗試建立與服務器的websocket連接。再次,我可以得到 這在我的本地機器上工作得很好。但是,沒有建立連接。 具體來說,dotcloud肯定爲我提供了.html文件,但.html文件並未建立與服務器的websocket連接。但是connection.onerror也沒有被調用。有點奇怪。
下面是一些代碼來幫助你理解我在做什麼:
客戶端:
this.connection = new WebSocket('ws://pirate-captainlonate.dotcloud.com:1337');
this.connection.onerror = function (error) {
console.log("ERROR with the connection *sadface*");
};
**** Note that I note the onerror function here to show that I do indeed have it set up, but it's not being called. It would seem that no error is being thrown.
服務器端:
var webSocketServer = require('websocket').server; // websocket
var server = require('http').createServer();
var expr = require("express"); // load the express module
var xpress = expr(); // xpress now holds the server object
// Helps Node serve the game.html page upon a get request
xpress.configure(function() {
xpress.use(expr.static(__dirname + "/public"));
xpress.set("view options", {layout: false});
});
// All requests to root serve the game.html page
xpress.get('/', function(req, res) {
res.sendfile(__dirname + '/public/game.html');
});
// What ports to listen on
var webSocketsServerPort = 1337;
xpress.listen(8080);
server.listen(webSocketsServerPort, function() {
console.log((new Date()) + " Server is listening on port " + webSocketsServerPort);
});
// WebSocket Server
var wsServer = new webSocketServer({
httpServer: server
});
這應該是足夠代碼向你們展示它是如何工作的。現在你們中的一個人可能會問,什麼是「>> dotcloud logs」顯示?
[www.0] ==> /var/log/supervisor/app.log <==
[www.0] Sat Feb 16 2013 02:57:59 GMT+0000 (UTC) Server is listening on port 1337
[www.0] ==> /var/log/supervisor/supervisord.log <==
[www.0] 2013-02-16 02:57:57,946 WARN Included extra file "/home/dotcloud/current/supervisord.conf" during parsing
[www.0] 2013-02-16 02:57:58,033 INFO RPC interface 'supervisor' initialized
[www.0] 2013-02-16 02:57:58,033 WARN cElementTree not installed, using slower XML parser for XML-RPC
[www.0] 2013-02-16 02:57:58,033 CRIT Server 'unix_http_server' running without any HTTP authentication checking
[www.0] 2013-02-16 02:57:58,038 INFO daemonizing the supervisord process
[www.0] 2013-02-16 02:57:58,039 INFO supervisord started with pid 140
[www.0] 2013-02-16 02:57:59,048 INFO spawned: 'app' with pid 154
[www.0] 2013-02-16 02:58:00,290 INFO success: app entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
[db.0] ==> /var/log/mongodb/mongodb.log <==
[db.0] Sat Feb 16 01:45:02 [conn4] end connection 127.0.0.1:51326 (0 connections now open)
好吧,好吧,我真的很想得到這個工作。我一直在這個永遠。讓我知道如果你還有什麼需要幫助我回答我的問題。
感謝,
--Nathan
附錄:這是服務器如何發送HTML文件。
xpress.get('/', function(req, res) {
res.sendfile(__dirname + '/public/game.html');
});
偉大的問題,大量的信息,很容易找到你的問題。基本上你需要第二個http端口。我在下面的答案中解釋如何做到這一點。 – 2013-02-16 12:24:44