PHPWebsocket error: & quot; stream

Source: Internet
Author: User
Tags php website php websocket
Recently an error occurred while using Ratchet (A PHPwebsocket framework) to transform a PHP Website: Itissetto1024, butyouhavedescriptorsnumberedatleastashighas1266. -- enable-fd-setsize2048isrecommended, butyoumaywanttosetittow.thema

Recently, when Ratchet (a PHP websocket framework) was used 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 ma

Recently, when Ratchet (a PHP websocket framework) was used 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, it is found that the source code of PHP has set FD_SIZE to 1024. Once more than 1024 websocket connections are established, this error is reported. The online solution is to modify the PHP source code and recompile PHP. however, I personally think there is a risk to modify the PHP source code, so I put forward a new idea and applied it in the project (Welcome To make a decision ).

The basic principle is to open several websocket service processes in the background, and each process uses a different port number. The frontend js randomly connects to the websocket service process port opened in the backend. In this way, each websocket service process has 1024 available connections, and the number of websocket service processes to be opened is calculated based on the Peak connections of the website. Of course, you need to leave some margin. In this way, you do not need to re-compile the PHP source code, and can easily break through the limit of 1024 connections of PHP websocket.

Background implementation


Write a push-server.php as the websocket service, the implementation of the push-server can refer to the Ratchet example, but you need to modify a little, that is, you can enter the port as the command line parameter.

$ Port = $ argv [1]; if ($ port = "") {$ port = 40003; // default port, if you do not write a parameter when starting the push-server.php, use port 40003 }//..... omit other code ..... // use $ port as the listening port to input $ webSock-> listen ($ port, '0. 0.0.0 ');

  

You can start multiple websocket processes as needed, such:

Php push-server 40003

Php push-server 40004

Php push-server 40005

In this way, the backend can allow 1024*3 = 3072 websocket connections.

Foreground implementation

The code for randomly obtaining the connection service is as follows:

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

You can also specify when to connect to a websocket service as needed.

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.