简体   繁体   English

来自同一浏览器的PHP同时TCP连接

[英]PHP simultaneous TCP connections from same browser

I have a PHP script which opens a socket connection(using fsockopen) and takes about 15secs to complete/return the result to the browser. 我有一个PHP脚本,可以打开一个套接字连接(使用fsockopen),大约需要15秒才能完成/将结果返回给浏览器。 Mean while, if the browser sends a second request it is serialized. 同时,如果浏览器发送第二个请求,它将被序列化。 This is giving a bad user experience because if the user clicks 3 times, then the third request which gets sent after 30sec is the one that gets the response -- the first 2 requests from browser prespective are getting lost. 这给用户带来不好的体验,因为如果用户单击3次,则在30秒后发送的第三个请求就是得到响应的请求-预期来自浏览器的前两个请求会丢失。

I do not have any session in my script, but tried putting session_write_close() at the beginning of my script which didnt help. 我的脚本中没有任何会话,但是尝试将session_write_close()放在脚本的开头没有帮助。

Also session.auto_start in the php.ini = 0. 此外,php.ini中的session.auto_start = 0。

Any ideas as to how to make the client requests from the same browser parallel?? 关于如何使来自同一浏览器的客户端请求并行的任何想法?

Thanks Gary 谢谢加里

1) Download and install Firefox 1)下载并安装Firefox

2) Download and install Firebug 2)下载并安装Firebug

3) Add a sleep(10) to your PHP script so that it pauses for a few seconds before returning its response 3)在您的PHP脚本中添加一个sleep(10) ,以便在返回响应之前暂停几秒钟

4) Open up your webpage, and watch the outbound connections with Firebug. 4)打开您的网页,并查看与Firebug的出站连接。 You should see several that are open and do not yet have a response. 您应该看到几个打开的但还没有响应。 They should all return at about the same time, when each one finishes the 10 second delay. 他们每个人都应该大约在同一时间返回,每个人都完成10秒的延迟。

If you do not see multiple connections open at the same time, and return at approximately the same time, then you need to look at your front end code. 如果看不到同时打开多个连接并大约同时返回多个连接,则需要查看前端代码。 AJAX requests are asynchronous and can run in parallel. AJAX请求是异步的,可以并行运行。 If you are seeing them run serially instead, then it means you need to fix your JavaScript code, not anything on the server. 如果看到它们是串行运行的,则意味着您需要修复JavaScript代码,而不需要修复服务器上的任何内容。

Parallel asynchronous Ajax requests using jQuery 使用jQuery的并行异步Ajax请求

You should if at all possible install(*nix) redis . 您应该尽可能安装install(* nix) redis

To install just do simple 安装只需简单

make

With lpush/brpop you can handle this stuff asynchronously and keep order intact. 使用lpush / brpop,您可以异步处理这些内容并保持订单完整。 If you spawn couple of worker threads you could even handle multiple requests simultaneous. 如果产生几个工作线程,您甚至可以同时处理多个请求。 the predis client library is pretty solid predis客户端库非常可靠

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

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