本文介绍了如何保持Php stream_socket存活?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用stream_socket_client()的Php应用程序,可通过tcp从后端服务器获取数据,但想保持连接在池中的活动状态甚至更好,或者避免在头上进行连接/断开连接.

I have a Php application using stream_socket_client(), to get data through tcp from a back-end server, but would like to keep the connections alive or even better in a pool or something to avoid the connect/disconnect over head.

但是我不是Php专家,所以我不知道该怎么做.尽管我可能会在几个小时内弄清楚,但我的时间可能最好花在每个人的脑海上,所以有什么建议吗?

But I'm not a Php guru, so I have no idea how to do this. And although I could probably figure it out in a few hours, my time will be probably better spent picking on everyone's brains, so any advice?

推荐答案

在创建流时设置STREAM_CLIENT_PERSISTENT标志可防止连接空闲.在内部,该标志使stream_socket_client()调用pfsockopen()( doc )而不是fsockopen()(文档).

Setting the STREAM_CLIENT_PERSISTENT flag upon stream creation prevents the connection from idling out. Internally, the flag makes stream_socket_client() call pfsockopen() (doc) instead of fsockopen() (doc).

连接持久性仅限于在其上打开连接的服务器进程.当脚本结束并再次调用时,可能无法保证由同一进程处理您的请求-在这种情况下,将打开另一个连接.将连接放入$_SESSION进行共享将无法正常工作.

The connection persistence is limited to the server process the connection was opened on. When your script ends, and you call it again, there may be no guarantee that the same process handles your request - another connection will be opened in this case. Putting the connection into $_SESSION to share it will not work.

这篇关于如何保持Php stream_socket存活?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:58