gpt4 book ai didi

javascript - 在 dotcloud 上托管 nodejs 服务器

转载 作者:搜寻专家 更新时间:2023-10-31 23:55:36 25 4
gpt4 key购买 nike

我正在尝试在托管服务“dotcloud”上托管一个 nodejs 应用程序。我的 nodejs 使用包“websocket”来处理通信。IE。 npm 安装 websocket

我的应用程序在笔记本电脑的本地主机上运行时运行良好。但是当我在 dotcloud 上部署应用程序时,它无法正常工作。

事情是这样的:您将浏览器指向 dotcloud 上的 url:海盗船长.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');
});

最佳答案

看起来您正在尝试为您的服务使用 2 个 http 端口,而 dotCloud 仅支持开箱即用的 1 个,因此您需要通过在 dotcloud.yml < 中添加一个小片段让他们知道您想要另一个端口/p>

这是一个 dotcloud.yml 的例子,它要求第二个名为 server

的 tcp 端口
app:
type: nodejs
ports:
server: tcp
config:
node_version: v0.8.x

添加并推送后,您的服务器将获得可用于服务器的第二个 TCP 端口,您只需要通过从环境文件中获取值来找出是哪个端口。

这是一个从 ENV 获取端口的代码片段,如果不存在,它将默认为 4242,因此您仍然可以在本地运行。

var webSocketsServerPort = process.env['PORT_SERVER'] || 4242;

如果您想知道我是如何获得 ENV 变量名的,那很简单。它将是 PORT_ 然后是 dotcloud.yml 中名称的大写字符串。因为我在上面使用了 server 它变成了 PORT_SERVER,如果我使用 node 的话它会是 PORT_NODE,所以放你想要的,但要确保这些值匹配。

客户:

要找出您需要连接到客户端上的哪个端口,您需要再次返回到您的环境变量。这次您正在寻找一个类似于 DOTCLOUD_APP_SERVER_PORT 的变量。 重要:您的变量名称可能不同

我是如何获得环境变量名称的?

变量名看起来像这样 DOTCLOUD_{{app_name}}_{{port_name}}_PORT 全部大写。将 {{variable}} 替换为以下信息。

{{app_name}} = 来自 dotcloud.yml 的应用名称,在上面的示例中为 app {{port_name}} = 端口名称,上面 dotcloud.yml 示例中的 server

要找到它,您可以从您的应用程序 environment.jsonenvironment.yml 文件、shell ENV 变量中获取它,或者登录到 dotCloud 仪表板,单击您的应用程序,然后单击“环境”选项卡以查看您的应用程序变量列表。

如果您进行了这三项更改,您的问题应该会消失。

如果您需要更多代码示例,请查看此 github 存储库,它执行的操作与您尝试执行的操作类似。

https://github.com/3on/node-tcp-on-dotcloud

关于javascript - 在 dotcloud 上托管 nodejs 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14906632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com