gpt4 book ai didi

How do I get a list of connected sockets in net.Server?(如何获取net.Server中已连接套接字的列表?)

转载 作者:bug小助手 更新时间:2023-10-24 19:38:48 24 4
gpt4 key购买 nike



net.server.getConnections((err, count) => {...}) only returns the count of the connection, I can't get each connection. I had thought I could use the following code for (let socket of server.connections) {...} inside the callback, but I got the error TypeError: server.connections is not iterable.

Net.server.getConnections((err,count)=>{...})仅返回连接的计数,我无法获取每个连接。我本以为可以在回调中使用下面的代码(let Socket of server.Connections){...},但是我得到了错误TypeError:server.Connections is not iterable。


I search the SO and found a similar question that was asked 10 years ago without an answer so I ask it again, Enumerate Node.js net.Server connections.

我搜索了SO,发现了一个与10年前类似的问题,但没有答案,所以我再次询问,枚举Node.js net.Server连接。


How do I get each connection? Do I have to store each socket myself on Event: 'connection' callback? That seems to be the only way to do that currently and I have raised a feature request to nodejs

我如何获得每个连接?我必须在事件:‘Connection’回调时自己存储每个套接字吗?这似乎是目前唯一的方法,我已经向NodeJS提出了功能请求


The similar question was also asked in Python several times, e,g. How can I keep a list of connected clients in a list in python? and they all said you have to store it yourself.

类似的问题在Python语言中也被问了好几次,例如,如何在Python语言中将连接的客户端列表保存在列表中?他们都说你必须自己储存它。


Just that Nodejs provides net.server.getConnections((err, count) => {...}), which makes me doubt the use case of it.

只是NodeJS提供了net.server.getConnections((err,count)=>{...}),这让我怀疑它的用例。


更多回答

You have to store them yourself. There is no reason for any API or framework to do so, and there isn't really any good reason for an application to do so either. Why do you think you need this?

你得自己把它们储存起来。任何API或框架都没有理由这样做,应用程序也没有任何很好的理由这样做。你觉得你为什么需要这个?

@user207421 thanks for the comments. But if there is no reason for API to do that, what does it provide net.server.getConnections, what is the use case for it ?

@user207421感谢您的评论。但是,如果API没有理由这样做,它提供了什么net.server.getConnections,它的用例是什么?

Nothing stopping the server sending messages to clients. That's what the accepted sockets are for. You should not expect every API to be like every other API.

不会阻止服务器向客户端发送消息。这就是公认的套接字的用途。您不应该期望每个API都与其他API一样。

I need to send a message to its own tcp connection, not broadcast. So I need to get all the connections first then find out which connection this message is for.

我需要将消息发送到它自己的TCP连接,而不是广播。因此,我需要首先获取所有连接,然后找出此消息针对的是哪个连接。

优秀答案推荐

The comments I got in my feature request makes me realize nodejs won't do it for couple of reasons so I have to store each socket myself, e.g.

我在功能请求中得到的评论让我意识到NodeJS不会这样做,因为几个原因,所以我必须自己存储每个套接字,例如



Constructing potentially large lists of resources generally feels like an anti-pattern in Node.js, and unless the application code is entirely synchronous, it is prone to race conditions.



The comment also mentioned the possible usage of net.server.getConnections((err, count) => {...}) ,

评论还提到了net.server.getConnections((err,count)=>{...})的可能用法,



I assume getConnections() to count connections is mostly used as a
metric in practice, e.g., with Prometheus or so.



更多回答

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