Node.js Heroku 应用程序上的 TCP 连接

Posted

技术标签:

【中文标题】Node.js Heroku 应用程序上的 TCP 连接【英文标题】:TCP connection on Node.js Heroku app 【发布时间】:2014-07-09 04:12:07 【问题描述】:

我有一个使用 Node.js 在本地运行的简单 TCP 聊天应用程序。我想将它部署到 Heroku。我对 Heroku 和 Node.js 都很陌生,并且立即遇到了无法通过 telnet 登录到任何特定端口来访问此聊天客户端的问题。理想的情况(使用 forman start 在本地工作)是我将远程登录到服务器并开始发布可以通过 Heroku 的终端日志读取的数据。如果应用程序在本地运行,我可以在通过工头启动时使用 telnet http://tcp-chat.herokuapp.com5000 进行连接,或者在通过节点 index.js 启动时使用 telnet http://tcp-chat.herokuapp.com3000 进行连接。在 Heroku 上,我无法使用这些端口中的任何一个进行连接。我为 Heroku 看到的每个示例都使用 Express,我不想将其用于学习目的。谁能解释这是否可以纠正以及为什么会发生这种情况?

index.js

    var net = require('net');
//Keep track of connections
var count = 0;

var server = net.createServer(function (connection) 
    connection.setEncoding('utf8');
    connection.write(
        '\n > welcome to \033[92mnode-chat\033[39m!' +
        '\n > ' +count+ ' other people are connected at this time.' +
        '\n > please write your name and press enter: '
    );
    count++;
    connection.on('data', function (data) 
       console.log(data);
    );

    connection.on('close', function (error) 
        console.log('Error: ' + error);
        count--;
    );
);

var port = process.env.PORT || 3000;

server.listen(port, function () 
    console.log('\033[90m   server listening on *:' + port + '\033[39m');
);

过程文件

web: node index.js

package.json


"name": "tcp-chat",
"description": "TCP server",
"version": "0.0.1",
"engines": 
    "node": "v0.10.28",
    "npm": "1.4.x"

错误日志:

    2014-05-20T18:16:08.555522+00:00 app[web.1]: accept-language: en-us
2014-05-20T18:16:08.555524+00:00 app[web.1]: cache-control: max-age=0
2014-05-20T18:16:08.555525+00:00 app[web.1]: x-request-id: 859a605f-e831-4156-9822-e465cbf84fc9
2014-05-20T18:16:08.555527+00:00 app[web.1]: x-forwarded-for: 97.68.23.226
2014-05-20T18:16:08.555531+00:00 app[web.1]: via: vegur
2014-05-20T18:16:08.555516+00:00 app[web.1]: connection: close
2014-05-20T18:16:08.555538+00:00 app[web.1]: 
2014-05-20T18:16:08.555517+00:00 app[web.1]: accept-encoding: gzip, deflate
2014-05-20T18:16:08.555540+00:00 app[web.1]: 
2014-05-20T18:16:08.555508+00:00 app[web.1]: GET / HTTP/1.1
2014-05-20T18:16:08.555535+00:00 app[web.1]: total-route-time: 2
2014-05-20T18:16:36.919076+00:00 app[web.1]: Error: false
2014-05-20T18:16:38.553267+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path=/ host=tcp-chat.herokuapp.com request_id=859a605f-e831-4156-9822-e465cbf84fc9 fwd="97.68.23.226" dyno=web.1 connect=1 service=30002 status=503 bytes=586
2014-05-20T18:16:38.556467+00:00 app[web.1]: Error: false
2014-05-20T18:17:53.178665+00:00 heroku[api]: Starting process with command `node` by myemail@gmail.com
2014-05-20T18:17:56.227317+00:00 heroku[run.3002]: Awaiting client
2014-05-20T18:17:56.314939+00:00 heroku[run.3002]: Starting process with command `node`
2014-05-20T18:17:55.770835+00:00 heroku[run.3002]: State changed from starting to up
2014-05-20T18:18:16.111574+00:00 heroku[run.3002]: State changed from up to complete
2014-05-20T18:18:16.098800+00:00 heroku[run.3002]: Process exited with status 0
2014-05-20T18:18:27.754574+00:00 heroku[run.6097]: State changed from starting to up
2014-05-20T18:18:28.255725+00:00 heroku[run.6097]: Starting process with command `index.js`
2014-05-20T18:18:28.208735+00:00 heroku[run.6097]: Awaiting client
2014-05-20T18:18:29.912858+00:00 heroku[run.6097]: State changed from up to complete
2014-05-20T18:18:29.900907+00:00 heroku[run.6097]: Process exited with status 127
2014-05-20T18:18:47.944230+00:00 app[web.1]: Error: false
2014-05-20T18:20:59.105119+00:00 app[web.1]: Error: false
2014-05-20T18:22:12.266662+00:00 app[web.1]: connection: close
2014-05-20T18:22:12.266663+00:00 app[web.1]: accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
2014-05-20T18:22:12.266653+00:00 app[web.1]: GET / HTTP/1.1
2014-05-20T18:22:12.266660+00:00 app[web.1]: Host: tcp-chat.herokuapp.com
2014-05-20T18:22:12.266674+00:00 app[web.1]: x-forwarded-port: 80
2014-05-20T18:22:12.266665+00:00 app[web.1]: accept-language: en-us
2014-05-20T18:22:12.266666+00:00 app[web.1]: accept-encoding: gzip, deflate
2014-05-20T18:22:12.266668+00:00 app[web.1]: user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14
2014-05-20T18:22:12.266670+00:00 app[web.1]: x-request-id: 139f06a6-54d4-48e3-8802-0339d543e5f2
2014-05-20T18:22:12.266672+00:00 app[web.1]: x-forwarded-for: 97.68.23.226
2014-05-20T18:22:12.266673+00:00 app[web.1]: x-forwarded-proto: http
2014-05-20T18:22:12.266681+00:00 app[web.1]: Content-Length: 0
2014-05-20T18:22:12.266680+00:00 app[web.1]: total-route-time: 2
2014-05-20T18:22:12.266683+00:00 app[web.1]: 
2014-05-20T18:22:12.266684+00:00 app[web.1]: 
2014-05-20T18:22:12.266677+00:00 app[web.1]: connect-time: 6
2014-05-20T18:22:12.266676+00:00 app[web.1]: via: vegur
2014-05-20T18:22:12.266679+00:00 app[web.1]: x-request-start: 1400610132253
2014-05-20T18:22:42.268959+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path=/ host=tcp-chat.herokuapp.com request_id=139f06a6-54d4-48e3-8802-0339d543e5f2 fwd="97.68.23.226" dyno=web.1 connect=6 service=30003 status=503 bytes=560
2014-05-20T18:22:42.275641+00:00 app[web.1]: Error: false
2014-05-20T18:25:21.010472+00:00 app[web.1]: Error: false
2014-05-20T18:27:32.160554+00:00 app[web.1]: Error: false
2014-05-20T18:29:43.123184+00:00 app[web.1]: Error: false
2014-05-20T18:31:53.885286+00:00 app[web.1]: Error: false
2014-05-20T18:34:05.211129+00:00 app[web.1]: Error: false
2014-05-20T18:36:16.125340+00:00 app[web.1]: Error: false
2014-05-20T18:38:26.963611+00:00 app[web.1]: Error: false
2014-05-20T18:40:37.921057+00:00 app[web.1]: Error: false
2014-05-20T18:42:49.113478+00:00 app[web.1]: Error: false
2014-05-20T18:45:00.165273+00:00 app[web.1]: Error: false
2014-05-20T18:46:03.690586+00:00 app[web.1]: accept-language: zh-CN
2014-05-20T18:46:03.690578+00:00 app[web.1]: connection: close
2014-05-20T18:46:03.690579+00:00 app[web.1]: accept-encoding: deflate, gzip
2014-05-20T18:46:03.690588+00:00 app[web.1]: user-agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0); 360Spider
2014-05-20T18:46:03.690589+00:00 app[web.1]: x-request-id: 42d45a65-4f73-4541-845e-f69d42438df2
2014-05-20T18:46:03.690591+00:00 app[web.1]: x-forwarded-for: 182.118.21.127
2014-05-20T18:46:03.690592+00:00 app[web.1]: x-forwarded-proto: http
2014-05-20T18:46:03.690594+00:00 app[web.1]: x-forwarded-port: 80
2014-05-20T18:46:03.690595+00:00 app[web.1]: via: vegur
2014-05-20T18:46:03.690596+00:00 app[web.1]: connect-time: 5
2014-05-20T18:46:03.690598+00:00 app[web.1]: x-request-start: 1400611563680
2014-05-20T18:46:03.690599+00:00 app[web.1]: total-route-time: 3
2014-05-20T18:46:03.690601+00:00 app[web.1]: Content-Length: 0
2014-05-20T18:46:03.690602+00:00 app[web.1]: 
2014-05-20T18:46:03.690603+00:00 app[web.1]: 
2014-05-20T18:46:03.690571+00:00 app[web.1]: GET / HTTP/1.1
2014-05-20T18:46:03.690576+00:00 app[web.1]: Host: tcp-chat.herokuapp.com
2014-05-20T18:46:03.690581+00:00 app[web.1]: referer: http://tcp-chat.herokuapp.com/
2014-05-20T18:46:03.690583+00:00 app[web.1]: cookie: 
2014-05-20T18:46:03.690584+00:00 app[web.1]: accept: */*
2014-05-20T18:46:33.693226+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path=/ host=tcp-chat.herokuapp.com request_id=42d45a65-4f73-4541-845e-f69d42438df2 fwd="182.118.21.127" dyno=web.1 connect=5 service=30001 status=503 bytes=508
2014-05-20T18:46:33.695059+00:00 app[web.1]: Error: false
2014-05-20T18:47:10.967631+00:00 app[web.1]: Error: false
2014-05-20T18:51:33.145102+00:00 app[web.1]: Error: false
2014-05-20T18:53:44.055065+00:00 app[web.1]: Error: false
2014-05-20T18:55:21.484397+00:00 heroku[api]: Scale to web=1 by emailaddress@gmail.com
2014-05-20T18:55:55.043915+00:00 app[web.1]: Error: false
2014-05-20T18:58:06.131452+00:00 app[web.1]:               ^
2014-05-20T18:58:06.130680+00:00 app[web.1]: 
2014-05-20T18:58:06.131441+00:00 app[web.1]:         throw er; // Unhandled 'error' event
2014-05-20T18:58:06.132980+00:00 app[web.1]: Error: read ECONNRESET
2014-05-20T18:58:06.132982+00:00 app[web.1]:     at errnoException (net.js:904:11)
2014-05-20T18:58:06.132984+00:00 app[web.1]:     at TCP.onread (net.js:558:19)
2014-05-20T18:58:06.131075+00:00 app[web.1]: events.js:72
2014-05-20T18:58:07.668771+00:00 heroku[web.1]: State changed from up to crashed
2014-05-20T18:58:07.669405+00:00 heroku[web.1]: State changed from crashed to starting
2014-05-20T18:58:07.654801+00:00 heroku[web.1]: Process exited with status 8
2014-05-20T18:58:12.041400+00:00 app[web.1]:    server listening on *:49725
2014-05-20T18:58:12.498182+00:00 heroku[web.1]: State changed from starting to up
2014-05-20T18:58:12.472297+00:00 app[web.1]: Error: false
2014-05-20T18:58:12.492325+00:00 app[web.1]: Error: false
2014-05-20T18:58:09.855133+00:00 heroku[web.1]: Starting process with command `node index.js`
2014-05-20T19:00:19.044255+00:00 app[web.1]: Error: false
2014-05-20T19:02:31.268586+00:00 app[web.1]: Error: false

【问题讨论】:

您的测功机放大了吗? heroku ps:scale web=1(那个总能吸引我) 是的,根据他们的文档,通过:heroku ps:scale web=1 Scaling dynos... 完成,现在以 1:1X 运行 web。 我不确定是否可以指定一个端口来连接到 Heroku。每次似乎我都被拒绝了。如果我不指定端口,它只会尝试连接。 我已将它们附在上面 【参考方案1】:

Heroku 只允许 HTTP 流量传入您的服务器。您需要找到其他主机。

【讨论】:

这就是我想知道的。所以我必须改用 AWS 或 RackSpace 之类的东西吗? 。 那些可以,但价格相当昂贵。 DigitalOcean 的 5 美元 VPS 应该可以满足您的需求。 @ZachMertes 还是这样吗?

以上是关于Node.js Heroku 应用程序上的 TCP 连接的主要内容,如果未能解决你的问题,请参考以下文章

从 Heroku 上的 Node.js 连接到 CloudSQL

Heroku 上的 Socket.io 聊天应用程序与 node.js

Heroku 上的 socket.io + node.js

Heroku 上的 Node JS 支持版本

Heroku上的Node.js Web Socket H15空闲连接超时

处理或防止错误使我在 Heroku 上的整个 node.js 应用程序崩溃