我正在尝试在托管服务“dotcloud”上托管一个 nodejs 应用程序。我的 nodejs 使用包“websocket”来处理通信。IE。npm 安装 websocket
我的应用程序在我的笔记本电脑上的 localhost 上运行时效果很好。但是当我在 dotcloud 上部署应用程序时,它无法正常工作。
这是发生了什么:您将浏览器指向 dotcloud 上的 url:pirate-captainlonate.dotcloud.com
然后,express 使用 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)
好吧,我真的很想让这个工作。我一直在这。让我知道你们是否需要帮助我回答我的问题。
谢谢,
——内森
附录:这是服务器发送 html 文件的方式。
xpress.get('/', function(req, res) {
res.sendfile(__dirname + '/public/game.html');
});