TCP connection on Node.js Heroku app

2019-08-10 02:37发布

I've got a simple TCP chat application below running locally using Node.js. I would like to deploy this to Heroku. I'm quite new to both Heroku and Node.js and instantly ran into issues where I could not telnet into any specific port to access this chat client. The ideal situation (which works locally with forman start) is that I would telnet into the server and start posting data that can be read out via the terminal logs for Heroku. If the app is running locally, I can connect using telnet http://tcp-chat.herokuapp.com 5000 when starting via foreman start or telnet http://tcp-chat.herokuapp.com 3000 when starting via node index.js. On Heroku, I am unable to connect using any of these ports. Every example I've seen for Heroku uses Express, which I don't want to use for my learning purposes. Would anyone be able to explain if this can be corrected and why this is happening?

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');
});

Procfile

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"
}

Error logs:

    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

1条回答
Viruses.
2楼-- · 2019-08-10 03:12

Heroku only allows HTTP traffic incoming to your server. You'll need to find a different host.

查看更多
登录 后发表回答