search
HomeBackend DevelopmentPHP TutorialPHP long connection, it is so simple_PHP tutorial

PHP long connection, it’s so simple to play

You are definitely familiar with long links, which are reusing a link to continuously interact with data. It is not like those one-night stand-like services that require frequent opening and closing of links. It is inefficient and also increases the number of links. the complexity of the business. Many Internet business scenarios require the support of long connections, such as games, chats, information push, etc. Today we will reveal step by step how to play long connections in PHP. I believe that the implementation of any technology is due to the needs of business scenarios, so this time we also talk about chat rooms.

0x00 First try

I remember that when writing chat rooms in PHP, I still used polling. There is no doubt that when polling is mentioned, some people will definitely say long polling, that’s right! Long polling is also very good, but it is a bit laborious to play this on nginx fpm. After all, one request requires one php process (even if you use apache php_mod, one request requires one thread), so it is okay if a few people just play it. Once there are more people online, it will basically be useless. Therefore, we still use the polling method, which will not block the process, and a request can be responded to immediately. However, the new problem is that we need to keep sending requests to the server, and the longer the interval, the greater the message delay. big.

0x01 Gorgeous transformation

After experiencing the above scene where one second is a small calorie, three seconds is a big calorie! I couldn't stand it anymore, so I decided to transform into a real man. Oh no, it should be a real long-term connection. Fuck polling, fuck long polling, fuck webserver, step aside and let flash socket (or websocket) rule the world! The journey of long-term connection in the true sense began. To play with long connections, it is always necessary to deal with sockets. As the best language in the world (no one), socket encapsulation is naturally indispensable. Just copy socket_*** and start working, so you have the following code, long connection, right? Delayed, right? socket, right? The soup and medicine cost, right? so easy....

<ol class="dp-c"><li class="alt"><span><span class="vars">$sfd</span><span> = socket_create(AF_INET, SOCK_STREAM, 0); </span></span></li><li><span> </span></li><li class="alt"><span>socket_bind(<span class="vars">$sfd</span><span>, </span><span class="string">"0.0.0.0"</span><span>, 1234); </span></span></li><li><span> </span></li><li class="alt"><span>socket_listen(<span class="vars">$sfd</span><span>, 511); </span></span></li><li><span> </span></li><li class="alt"><span>socket_set_option(<span class="vars">$sfd</span><span>, SOL_SOCKET, SO_REUSEADDR, 1); </span></span></li><li><span> </span></li><li class="alt"><span>socket_set_nonblock(<span class="vars">$sfd</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$rfds</span><span> = </span><span class="keyword">array</span><span>(</span><span class="vars">$sfd</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$wfds</span><span> = </span><span class="keyword">array</span><span>(); </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">do</span><span>{ </span></span></li><li><span> </span></li><li class="alt"><span>    <span class="vars">$rs</span><span> = </span><span class="vars">$rfds</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span>    <span class="vars">$ws</span><span> = </span><span class="vars">$wfds</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span>    <span class="vars">$es</span><span> = </span><span class="keyword">array</span><span>(); </span></span></li><li><span> </span></li><li class="alt"><span>    <span class="vars">$ret</span><span> = socket_select(</span><span class="vars">$rs</span><span>, </span><span class="vars">$ws</span><span>, </span><span class="vars">$es</span><span>, 3); </span></span></li><li><span> </span></li><li class="alt"><span>     </span></li><li><span> </span></li><li class="alt"><span>    <span class="comment">//read event</span><span> </span></span></li><li><span> </span></li><li class="alt"><span>    <span class="keyword">foreach</span><span>(</span><span class="vars">$rs</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$fd</span><span>){ </span></span></li><li><span> </span></li><li class="alt"><span>        <span class="keyword">if</span><span>(</span><span class="vars">$fd</span><span> == </span><span class="vars">$sfd</span><span>){ </span></span></li><li><span> </span></li><li class="alt"><span>            <span class="vars">$cfd</span><span> = socket_accept(</span><span class="vars">$sfd</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span>            socket_set_nonblock(<span class="vars">$cfd</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span>            <span class="vars">$rfds</span><span>[] = </span><span class="vars">$cfd</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span>            <span class="func">echo</span><span> </span><span class="string">"new client coming, fd=$cfd\n"</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span>        }<span class="keyword">else</span><span>{ </span></span></li><li><span> </span></li><li class="alt"><span>            <span class="vars">$msg</span><span> = socket_read(</span><span class="vars">$fd</span><span>, 1024); </span></span></li><li><span> </span></li><li class="alt"><span>            <span class="keyword">if</span><span>(</span><span class="vars">$msg</span><span> <= 0){ </span></span></li><li><span> </span></li><li class="alt"><span>                <span class="comment">//close</span><span> </span></span></li><li><span> </span></li><li class="alt"><span>            }<span class="keyword">else</span><span>{ </span></span></li><li><span> </span></li><li class="alt"><span>                <span class="comment">//recv msg</span><span> </span></span></li><li><span> </span></li><li class="alt"><span>                <span class="func">echo</span><span> </span><span class="string">"on message, fd=$fd data=$msg\n"</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span>            } </span></li><li><span> </span></li><li class="alt"><span>        } </span></li><li><span> </span></li><li class="alt"><span>    } </span></li><li><span> </span></li><li class="alt"><span>     </span></li><li><span> </span></li><li class="alt"><span>    <span class="comment">//write event</span><span> </span></span></li><li><span> </span></li><li class="alt"><span>    <span class="keyword">foreach</span><span>(</span><span class="vars">$ws</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$fd</span><span>){ </span></span></li><li><span> </span></li><li class="alt"><span>        socket_write(<span class="vars">$fd</span><span>, ........); </span></span></li><li><span> </span></li><li class="alt"><span>    } </span></li><li><span> </span></li><li class="alt"><span>     </span></li><li><span> </span></li><li class="alt"><span>}<span class="keyword">while</span><span>(true); </span></span></li></ol>

0x02 Reach the pinnacle

From the day I started playing with sockets, Google told me softly: Don’t use select under high concurrency because it is inefficient. Use iocp for win, epoll for linux, blablablabla... oh! Well, since Google has said so, I can't argue with him. I decided again (why do I say so?) to listen to Google and start epoll, but I can't write it myself? A lazy person like me might as well expand the whole thing, libevent is gone! After crazy coding (py), the masterpiece is finally out. How efficient can it be and how much concurrency can it support? If I don’t build it, it’s useless to choose anyway. I’m a dick!

<ol class="dp-c"><li class="alt"><span><span class="vars">$sfd</span><span> = stream_socket_server (</span><span class="string">'tcp://0.0.0.0:1234'</span><span>, </span><span class="vars">$errno</span><span>, </span><span class="vars">$errstr</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span>stream_set_blocking(<span class="vars">$sfd</span><span>, 0); </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$base</span><span> = event_base_new(); </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$event</span><span> = event_new(); </span></span></li><li><span> </span></li><li class="alt"><span>event_set(<span class="vars">$event</span><span>, </span><span class="vars">$sfd</span><span>, EV_READ | EV_PERSIST, </span><span class="string">'ev_accept'</span><span>, </span><span class="vars">$base</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span>event_base_set(<span class="vars">$event</span><span>, </span><span class="vars">$base</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span>event_add(<span class="vars">$event</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span>event_base_loop(<span class="vars">$base</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">function</span><span> ev_accept(</span><span class="vars">$socket</span><span>, </span><span class="vars">$flag</span><span>, </span><span class="vars">$base</span><span>) </span></span></li><li><span> </span></li><li class="alt"><span>{ </span></li><li><span> </span></li><li class="alt"><span>    <span class="vars">$connection</span><span> = stream_socket_accept(</span><span class="vars">$socket</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span>    stream_set_blocking(<span class="vars">$connection</span><span>, 0); </span></span></li><li><span> </span></li><li class="alt"><span>    <span class="vars">$buffer</span><span> = event_buffer_new(</span><span class="vars">$connection</span><span>, </span><span class="string">'ev_read'</span><span>, NULL, </span><span class="string">'ev_error'</span><span>,  </span><span class="vars">$connection</span><span>);     </span></span></li><li><span> </span></li><li class="alt"><span>    event_buffer_base_set(<span class="vars">$buffer</span><span>, </span><span class="vars">$base</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span>    event_buffer_timeout_set(<span class="vars">$buffer</span><span>, 30, 30); </span></span></li><li><span> </span></li><li class="alt"><span>    event_buffer_watermark_set(<span class="vars">$buffer</span><span>, EV_READ, 0, 0xffffff); </span></span></li><li><span> </span></li><li class="alt"><span>    event_buffer_priority_set(<span class="vars">$buffer</span><span>, 10); </span></span></li><li><span> </span></li><li class="alt"><span>    event_buffer_enable(<span class="vars">$buffer</span><span>, EV_READ | EV_PERSIST); </span></span></li><li><span> </span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span><span class="keyword">function</span><span> ev_error(</span><span class="vars">$buffer</span><span>, </span><span class="vars">$error</span><span>, </span><span class="vars">$connection</span><span>) </span></span></li><li><span> </span></li><li class="alt"><span>{ </span></li><li><span> </span></li><li class="alt"><span>    event_buffer_disable(<span class="vars">$buffer</span><span>, EV_READ | EV_WRITE);                 </span></span></li><li><span> </span></li><li class="alt"><span>    event_buffer_free(<span class="vars">$buffer</span><span>);                 </span></span></li><li><span> </span></li><li class="alt"><span>    fclose(<span class="vars">$connection</span><span>);                 </span></span></li><li><span> </span></li><li class="alt"><span>} </span></li><li><span> </span></li><li class="alt"><span><span class="keyword">function</span><span> ev_read(</span><span class="vars">$buffer</span><span>, </span><span class="vars">$connection</span><span>) </span></span></li><li><span> </span></li><li class="alt"><span>{ </span></li><li><span> </span></li><li class="alt"><span>    <span class="vars">$read</span><span> = event_buffer_read(</span><span class="vars">$buffer</span><span>, 256); </span></span></li><li><span> </span></li><li class="alt"><span>    <span class="comment">//do something....</span><span> </span></span></li><li><span> </span></li><li class="alt"><span>} </span></li></ol>

0x03 Survival from desperate situation

As the number of people increases and concurrency increases, a single process can no longer meet the demand. Tian Boguang’s story tells us that we can’t beat a group of P’s in single combat. What should we do? As the saying goes, make big things small, make small things small, stop! ! Don't melt it, or it will be gone if you melt it again. Let's split it up and split the single process into multiple processes. But after splitting it, we will face new problems, such as inter-process communication, load balancing, session uniqueness, etc. Since such a question has been raised, there must be a solution. There are ready-made extensions and libraries to solve this problem, such as: swoole, workerman, etc.? In comparison, swoole is more cool, with sex and functionality, eh! It seems that this abbreviation is not very elegant. Well, the performance and functions are better (Brother Tong, please forgive me for being boring~). . . . Wait a moment! ! ! However, when we use PHP to develop the web, we do not use webserver-related libraries for development, right? We just do a simple echo. These complicated things are all handed over to nginx or apache, and they take the lead without hesitation, so that we can concentrate on writing logic. To write web, we only need to simply configure nginx and fpm. What about writing socket service? Why can't we simply configure it like nginx fpm? ? Of course it can, it must be possible. . . . . Watching this plot, I am afraid that an advertisement is coming. . .

0x04 Unexpected

Writing socket services is no more advanced than writing web services. They are both coding and completing requirements. The communication layer is fixed, but one is completed by nginx and the other is completed by yourself. . But now you don’t need to complete it yourself. A solution similar to nginx fpm, fooking fpm=php long connection, gateway is used to carry the connection, router is used to forward messages, inter-process communication? Load balancing? Session only? so easy..

<ol class="dp-c"><li class="alt"><span><span class="vars">$sid</span><span> = </span><span class="vars">$_SERVER</span><span>[</span><span class="string">'SESSIONID'</span><span>];</span><span class="comment">//这是sessionid</span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$data</span><span> = </span><span class="func">file_get_contents</span><span>(</span><span class="string">"php://input"</span><span>);//这样就能拿到请求内容了 </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//想要返回消息只需要两步</span><span> </span></span></li><li><span> </span></li><li class="alt"><span>header(<span class="string">'Content-Length: 11'</span><span>);</span><span class="comment">//返回给客户端字节数</span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">"hello world"</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//想要给别的用户发消息</span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="keyword">include</span><span> </span><span class="string">'api.php'</span><span>; </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$router</span><span> = </span><span class="keyword">new</span><span> RouterClient(</span><span class="string">'router host'</span><span>, </span><span class="string">'router port'</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$router</span><span>->sendMsg(用户sessionid, </span><span class="string">"fuck you"</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//想要给所有人要消息</span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$router</span><span>->sendAllMsg(</span><span class="string">"fuck all"</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">//想给指定组发消息(类似redis的pub/sub)</span><span> </span></span></li><li><span> </span></li><li class="alt"><span><span class="vars">$router</span><span>->publish(</span><span class="string">"channel name"</span><span>, </span><span class="string">"fuck all"</span><span>); </span></span></li></ol>

Project address: http://git.oschina.net/scgywx/fooking

Document address (updated from time to time): http://my.oschina.net/scgywx/blog/465186

PHP long connection, it is so simple_PHP tutorial



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1015229.htmlTechArticlePHP long connection is so simple. When it comes to long links, everyone is definitely familiar with it. It means reusing a link continuously. For data interaction, it is not like those one-night stand-like services that require frequent...
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

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 Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

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.