简体   繁体   English

Node.JS套接字服务器和同一服务器上的连接速度变慢

[英]Node.JS Socket server and connection on same server slow down

I may be doing something wrong here, but essentially I have a service layer that intercepts post requests to a certain url. 我在这里可能做错了什么,但是从本质上讲,我有一个服务层来拦截对某个URL的发布请求。 It's important this is never down so I isolated it from the main server. 重要的是,这个功能永远都不会失败,因此我将其与主服务器隔离了。 I figured it'd be easiest to have the service layer take the http requests and send the resulting XML packet over any streams connected. 我认为让服务层接受http请求并通过任何连接的流发送生成的XML数据包是最简单的。 Well, it's technically working, but the problem I'm having is that the server and client are both on the same machine and it seems as if stream.write is filling up the kernel buffer and slowing down the amount of time between when it hits the service layer to when it actually gets to the client. 好吧,这在技术上是可行的,但是我遇到的问题是服务器和客户端都在同一台机器上,并且似乎stream.write正在填充内核缓冲区并减慢了两次命中之间的时间服务层到实际到达客户端的时间。

On the Server Side 在服务器端

  • On connect I push the stream into an array of connections, I also set an interval to make sure that I have received a message within the past 12 seconds (heartbeat). 在连接时,我将流推送到连接数组中,我还设置了一个时间间隔以确保在过去的12秒(心跳)中收到了一条消息。

  • On receive I iterate over the stream array, make sure it's writable and write the xml packet to it. 接收时,我遍历流数组,确保其可写并将xml数据包写入其中。

On the Client Side 在客户端

  • Write "Heartbeat" every 8 seconds to ensure I don't get disconnected 每8秒钟写一次“ Heartbeat”,以确保我不会断开连接

  • On incoming message I parse the XML into a JSON object and deal w/ it within the server. 在收到消息时,我将XML解析为JSON对象,并在服务器内进行处理。

Nothing about this seems outrageous, but I may be missing something. 关于这一点,似乎没有什么可笑的,但是我可能会遗漏一些东西。

I figured it'd be easiest to have the service layer take the http requests and send the resulting XML packet over any streams connected. 我认为让服务层接受http请求并通过任何连接的流发送生成的XML数据包是最简单的。

One thing to remember is that node buffers things sent over streams. 要记住的一件事是节点缓冲通过流发送的内容。 Like you noted, "stream.write is filling up the kernel buffer", this is intended behavior, as usually one does not use streams to communicate in this way. 就像您提到的那样,“ stream.write正在填充内核缓冲区”,这是预期的行为,因为通常不会使用流以这种方式进行通信。 You might try something different like sending requests over sockets to your other process? 您可能会尝试其他操作,例如通过套接字向其他进程发送请求?

Hopefully this helps a bit? 希望这会有所帮助吗?

Stop into #node.js and ask us questions anytime! 随时进入#node.js并向我们​​提问!

Edit: on second thought, are you already using sockets? 编辑:再三考虑,您已经在使用套接字吗? If so, my answer is really unhelpful, apologies. 如果是这样,我的回答真的无济于事,很抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM