search
HomeBackend DevelopmentPHP TutorialAbout PHP Websocket Error: 'stream_select(): You MUST reco_PHP Tutorial

When I recently used Ratchet (a PHP websocket framework) to transform a PHP website, an error occurred:

"It is set to 1024, but you have descriptors numbered at least as high as 1266.

--enable-fd-setsize=2048 is recommended, but you may want to set it
to equal the maximum number of open files supported by your system"

After several twists and turns, I found that the reason is that the PHP source code sets FD_SIZE to 1024. Once there are more than 1024 websocket connections, this error will be reported. The solution on the Internet is basically to modify the PHP source code and recompile PHP. However, I personally think that modifying the PHP source code is risky, so I came up with a new idea and successfully used it in the project (you are welcome to submit a copy).

The basic principle is to open several more websocket service processes in the background, and each process uses a different port number. The front-end js randomly connects to the websocket service process port number open in the background. In this way, each websocket service process has 1024 available connections. The number of websocket service processes required can be calculated based on the peak number of connections of the website. Of course, some margin needs to be left. In this way, there is no need to recompile the PHP source code, and it can easily break through the 1024 connection limit of PHP websocket.

Backend implementation


Write a push-server.php as a websocket service. The implementation of push-server can refer to the Ratchet example, but it needs a slight modification, that is, you can enter port as the command line parameter.

$port = $argv[1];
if ($port == ""){
	$port = 40003; // 默认端口,如果启动push-server.php时不写参数,则使用40003端口
}
// .....省略其他代码 .....

// 将$port作为监听端口传入
$webSock->listen($port, '0.0.0.0');

 

As needed, multiple websocket processes can be started, such as:

php push-server 40003

php push-server 40004

php push-server 40005

In this way, the background can tolerate 1024 * 3 = 3072 websocket connections.

Front-end implementation

The code to randomly obtain the connection service is as follows:

<span function</span><span  getWSServer() {
    </span><span var</span> serverPorts = ['40003', '40004', '40005'<span ];
    </span><span var</span> server = 'ws://youhost'<span ;
    </span><span var</span> randomPortIndex = Math.floor(Math.random() *<span  serverPorts.length);
    
    server </span>+= ':' +<span  serverPorts[randomPortIndex];
    
    </span><span return</span><span  server;
};</span>

Of course, you can also specify when to connect to a certain websocket service according to your own needs, so I won’t go into details.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/729840.htmlTechArticleWhen I recently used Ratchet (a PHP websocket framework) to transform a PHP website, an error occurred: "It is set to 1024, but you have descriptors numbered at least as high as 1266....
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Vue.js与ASP.NET的结合,实现Web应用的性能优化和扩展的技巧和建议Vue.js与ASP.NET的结合,实现Web应用的性能优化和扩展的技巧和建议Jul 29, 2023 pm 05:19 PM

Vue.js与ASP.NET的结合,实现Web应用的性能优化和扩展的技巧和建议随着Web应用的快速发展,性能优化成为开发者不可或缺的重要任务。Vue.js作为一款流行的前端框架,与ASP.NET的结合可以帮助我们实现更好的性能优化和扩展。本文将会介绍一些技巧和建议,并提供一些代码示例。一、减少HTTP请求HTTP请求的数量直接影响着Web应用的加载速度。通过

ASP.NET程序中的MySQL连接池使用及优化技巧ASP.NET程序中的MySQL连接池使用及优化技巧Jun 30, 2023 pm 11:54 PM

如何在ASP.NET程序中正确使用和优化MySQL连接池?引言:MySQL是一种广泛使用的数据库管理系统,它具有高性能、可靠性和易用性的特点。在ASP.NET开发中,使用MySQL数据库进行数据存储是常见的需求。为了提高数据库连接的效率和性能,我们需要正确地使用和优化MySQL连接池。本文将介绍在ASP.NET程序中如何正确使用和优化MySQL连接池的方法。

生成式AI将在十个方面改变软件开发生成式AI将在十个方面改变软件开发Mar 11, 2024 pm 12:10 PM

译者|陈峻审校|重楼上个世纪90年代,当人们提起软件编程时,通常意味着选择一个编辑器,将代码检入CVS或SVN代码库,然后将代码编译成可执行文件。与之对应的Eclipse和VisualStudio等集成开发环境(IDE)可以将编程、开发、文档、构建、测试、部署等步骤纳入到一个完整的软件开发生命周期(SDLC)中,从而提高了开发人员的工作效率。近年来,流行的云计算和DevSecOps自动化工具提升了开发者的综合能力,使得更多的企业能够更加轻松地开发、部署和维护软件应用。如今,生成式AI作为下一代开

如何在ASP.NET程序中重连MySQL连接?如何在ASP.NET程序中重连MySQL连接?Jun 29, 2023 pm 02:21 PM

如何在ASP.NET程序中重连MySQL连接?在ASP.NET开发中,使用MySQL数据库是非常常见的。然而,由于网络或数据库服务器的原因,有时会导致数据库连接中断或超时。在这种情况下,为了保证程序的稳定性和可靠性,我们需要在连接断开后重新建立连接。本文将介绍如何在ASP.NET程序中实现重连MySQL连接的方法。引用必要的命名空间首先,在代码文件的头部引用

Vue.js与ASP.NET的结合,实现企业级应用的开发和部署Vue.js与ASP.NET的结合,实现企业级应用的开发和部署Jul 29, 2023 pm 02:37 PM

Vue.js与ASP.NET的结合,实现企业级应用的开发和部署在当今快速发展的互联网技术领域,企业级应用的开发和部署变得越来越重要。Vue.js和ASP.NET是两个在前端和后端开发中广泛使用的技术,将它们结合起来可以为企业级应用的开发和部署带来诸多优势。本文将通过代码示例介绍如何使用Vue.js和ASP.NET进行企业级应用的开发和部署。首先,我们需要安装

如何在ASP.NET程序中正确配置和使用MySQL连接池?如何在ASP.NET程序中正确配置和使用MySQL连接池?Jun 29, 2023 pm 12:56 PM

如何在ASP.NET程序中正确配置和使用MySQL连接池?随着互联网的发展和数据量的增大,对数据库的访问和连接需求也在不断增加。为了提高数据库的性能和稳定性,连接池成为了一个必不可少的技术。本文主要介绍如何在ASP.NET程序中正确配置和使用MySQL连接池,以提高数据库的效率和响应速度。一、连接池的概念和作用连接池是一种重复使用数据库连接的技术,在程序初始

aspnet有哪些内置对象aspnet有哪些内置对象Nov 21, 2023 pm 02:59 PM

ASP.NET中的内置对象有“Request”、“Response”、“Session”、“Server”、“Application”、 “HttpContext”、“Cache”、“Trace”、“Cookie”和“Server.MapPath”:1、Request,表示客户端发出的HTTP请求;2、Response:表示Web服务器返回给客户端的HTTP响应等等。

在Linux上使用Visual Studio进行ASP.NET开发的推荐配置在Linux上使用Visual Studio进行ASP.NET开发的推荐配置Jul 06, 2023 pm 08:45 PM

在Linux上使用VisualStudio进行ASP.NET开发的推荐配置概述:随着开源软件的发展和Linux操作系统的普及,越来越多的开发者开始在Linux上进行ASP.NET开发。而作为一款功能强大的开发工具,VisualStudio在Windows平台上一直占据着主导地位。本文将介绍如何在Linux上配置VisualStudio来进行ASP.NE

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),