About PHP Websocket error: "stream_select (): You must reco_php tutorial

Source: Internet
Author: User
Tags php source code php websocket
Recently, when using Ratchet (a PHP websocket framework) to transform a PHP Web site, an error occurred:

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

--ENABLE-FD-SETSIZE=2048 is recommended, and may want to set it
To equal the maximum number of open files supported by your system "

After several twists and turns, the reason is that the PHP source code set Fd_size to 1024, once more than 1024 websocket connection, will report this error. On-line solution, basically is to modify the PHP source code, recompile PHP. But personally think that the way to modify PHP source code is risky, so I put forward a new idea, and successfully used in the project (Welcome to shoot bricks).

The rationale is to open several WebSocket service processes in the background with different port numbers for each process. And the foreground JS randomly connected backstage open websocket service process port number. So each websocket service process has 1024 of the number of connections available, depending on the number of peak connections to the site to calculate the number of WebSocket service processes required, of course, to leave some margin. This does not need to recompile the PHP source code, also can easily break through PHP websocket 1024 connections limit.

The implementation of the background


Write a push-server.php as a websocket service, push-server implementation can refer to the example of ratchet, but need to be slightly modified, that is, you can enter the port as the command line parameters.

$port = $argv [1];if ($port = = "") {$port = 40003; The default port, if you do not write parameters when starting push-server.php, use 40003 port}//.... Omit other code ...//to pass $port as a listening port to the $websock->listen ($port, ' 0.0.0.0 ');

  

Depending on your needs, you can start multiple websocket processes, such as:

PHP push-server 40003

PHP push-server 40004

PHP push-server 40005

This allows the backend to allow 1024 * 3 = 3,072 websocket connected.

The implementation of the foreground

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

< span="">< span="">< span="">Serverports = [' 40003 ', ' 40004 ', ' 40005 '  < span="">    < span="">  server = ' ws://youhost '< span=""> /c11>  < span="">  randomportindex = Math.floor (Math.random () *< span="">+ = ': ' +< span="">< span="">< span="">

Of course you can also according to your own needs, specify when to connect a websocket service, no longer repeat.

http://www.bkjia.com/PHPjc/729840.html www.bkjia.com true http://www.bkjia.com/PHPjc/729840.html techarticle recently, when using Ratchet (a PHP websocket framework) to transform a PHP Web site, there was an error: "It is set to 1024x768, but you had descriptors numbered at least as high as 1266 ....

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.