简单的TCP套接字聊天服务器在heroku上不起作用(simple TCP socket chat server doesn't work on heroku)

编程入门 行业动态 更新时间:2024-10-22 07:16:43
简单的TCP套接字聊天服务器在heroku上不起作用(simple TCP socket chat server doesn't work on heroku)

我想在heroku上构建一个node.js聊天服务器,聊天客户端将成为iOS应用程序。

我的第一次尝试是基于socket.io构建服务器。 它可以工作,但我不喜欢iOS应用程序使用长轮询而不是websocket或tcp套接字与服务器通信的事实。

所以我的第二次尝试是使用真正的TCP套接字构建服务器,代码如下:

var net = require('net'); var sockets = []; var server = net.createServer(function(socket){ sockets.push(socket); socket.on('data', function (data) { for(var i=0; i< sockets.length; i++) { sockets[i].write(data); } }); socket.on('end', function() { var i = sockets.indexOf(socket); sockets.splice(i, 1); }) }); var port = process.env.PORT || 5000; console.log("server listening to port " + port); server.listen(port);

如果我在localhost中部署此node.js服务器代码,我可以使用Terminal作为测试客户端成功发送和接收聊天到TCP服务器

nc localhost 5000

但是,如果我在heroku中部署此服务器代码,我将无法再与服务器建立tcp连接。

我的理解是服务器将在process.env.PORT上监听。

所以我首先找出了服务器正在监听的端口,例如30134。

然后,如果我尝试以这种方式连接:

nc http://my-heroku-app.herokuapp.com 30134

什么都没发生。

我想我在某处有一些根本的误解。 谁能指导我? 是否可以在heroku服务器上进行Real TCP套接字聊天?

I wanted to build a node.js chat server on heroku, the chat clients are going to be iOS apps.

My first attempt is to build a server based on socket.io. It works, but I don't like the fact the the iOS app is talking to the server using long polling instead of websocket or tcp socket.

So my second attempt is to build the server using real TCP socket, code as below:

var net = require('net'); var sockets = []; var server = net.createServer(function(socket){ sockets.push(socket); socket.on('data', function (data) { for(var i=0; i< sockets.length; i++) { sockets[i].write(data); } }); socket.on('end', function() { var i = sockets.indexOf(socket); sockets.splice(i, 1); }) }); var port = process.env.PORT || 5000; console.log("server listening to port " + port); server.listen(port);

If I deploy this node.js server code in localhost, i can successfully send and receives chat to the TCP server using Terminal as test client

nc localhost 5000

BUT, if I deploy this server code in heroku, I couldn't establish a tcp connection with the server anymore.

My understanding is server will be listening at the process.env.PORT.

So I first traced out this port that the server is listening, e.g. 30134.

Then If I attempt to connect in this way:

nc http://my-heroku-app.herokuapp.com 30134

Nothing happens.

I think I have some fundamental misunderstanding somewhere. Can anyone guide me? Is Real TCP socket chat possible on heroku server?

最满意答案

Heroku不支持websocket。 这就是为什么,例如, socket.io需要被迫使用xhr-polling 。 (请参阅https://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku )。

您可以使用类似socket.io东西来支持多个传输,而不是直接使用套接字。

这是因为他们使用的是旧版本的nginx ,它没有完整的Web套接字支持。

此外,您的应用程序正在侦听的端口, eg 30134由nginx从您的端口​​80上的heroku主机名代理到heroku内部网络上的应用程序端口。

Heroku does not support websocket. This is why, for example, socket.io needs to be forced to use xhr-polling. (see https://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku).

Instead of using sockets directly, you could use something like socket.io which supports multiple transports.

This is because they use and older version of nginx, which did not have full web socket support.

Plus, the port your app is listening on, e.g. 30134 is proxied by nginx from your heroku hostname on port 80 to your app port on heroku's internal network.

更多推荐

本文发布于:2023-08-04 23:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424524.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上不   简单   服务器   heroku   work

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!