Swoole
Swoole
Also provides some interfaces for direct manipulation of the underlying epoll/kqueue
event loop, which can be socket
stream/socket
socket
added to the swoole in addition to other extensions created by extensions created in PHP code EventLoop
.
Document: Https://wiki.swoole.com/wiki/page/242.html
Here I also briefly introduce.
Basic use
swoole_tcp_server.php
<?php/** * Created by Phpstorm. * User: Public number: Feihong Shadow's Blog (fhyblog) * Date:2018/6/30 */use swoole\event; $socket = Stream_socket_server ("tcp://0.0.0.0:9201", $e Rrno, $errstr), if (false = = = $socket) {echo "$errstr ($errno) \ n"; Exit ();} if (! $socket) Die ($errstr. " --". $errno);//stream_set_blocking ($socket, 0); Can be removed, does not involve read this Socketecho "waiting client...\n";//accept event callback function, the parameters are $fdfunction ev_accept ($socket) {global $ Master $connection = Stream_socket_accept ($socket); The setting of the parameter will affect functions such as fgets () and fread () to read data from the resource stream. In nonblocking mode, the call fgets () always returns immediately, while in blocking mode it waits until the data is fetched from the resource stream to return. Stream_set_blocking ($connection, 0);//If not set, subsequent reads will block $id = (int) $connection; echo "New Client $id \ n"; Event::add ($connection, ' ev_read ', null, swoole_event_read); }//read Event callback function, parameter is fdfunction ev_read ($buffer) {$receive = '; Loop Read and parse client message while (1) {$read = @fread ($buffer, 2); Client abnormally disconnects if ($read = = = "| | $read = = = False) {break; }$pos = Strpos ($read, "\ n"); if ($pos = = = False) {$receive. = $read; echo "Received:". $read. "; Not all package,continue recdiveing\n "; }else{$receive. = Trim (substr ($read, 0, $pos + 1)); $read = substr ($read, $pos + 1); Switch ($receive) {case "quit": echo "client close conn\n"; Close Client Connection event::d El ($buffer);//Delete event fclose ($buffer);//Disconnect Client Connection Break Default://echo "All package:\n"; echo $receive. " \ n "; Break } $receive = '; }}}event::add ($socket, ' ev_accept ', null, swoole_event_read); echo "Start run...\n";//Enter Event loop event::wait (); PHP5.4 or later does not need to add this function//the following sentence is not executed by echo "This code would not be executed.\n";
Event::add()
Such methods also have corresponding functions, for example swoole_event_add()
.
Timer
The Swoole provides two timers:
swoole_timer_tick
Periodic timers, object-oriented notation:Timer::tick
swoole_timer_after
Disposable timers, Object-oriented notation:Timer::after
The accuracy of the swoole timer is milliseconds.
Example:
<?php /** * Created by PhpStorm. * User: 公众号: 飞鸿影的博客(fhyblog) * Date: 2018/6/30 */use Swoole\Timer;Timer::after(1000, function(){ echo time(). " hello\n"; Timer::tick(1000, function($timer_id, $params){ static $c = 0; echo time(). " hello $params $c\n"; $c++; if($c > 5){ Timer::clear($timer_id); } }, 'this is param');});
Description: Timer::after
The callback function has no parameters, and Timer::tick
the callback function parameter is the timer ID and additional parameters.
Summarize
I have consulted the Workerman
source code, see the author of the Common Event (System comes with SELECT, Libevet, Event, Ev, Swoole) for a package, external exposure to the unified interface (add, Del, loop), we are interested to see.
Reference
1. PHP libevent Extension Simple use case-nickbai-Blog Park
Https://www.cnblogs.com/nickbai/articles/6197689.html
2. PHP uses PCNTL and libevent to implement the timer function | Blog Water Wood
Http://www.4u4v.net/php-uses-pcntl-and-libevent-achieve-timer-function.html
3. Socket Service model and implementation (4) – Single process IO multiplexing libevent
Http://www.xtgxiso.com/socket%e6%9c%8d%e5%8a%a1%e7%9a%84%e6%a8%a1%e5%9e%8b%e4%bb%a5%e5%8f%8a%e5%ae%9e%e7%8e% b04-%e5%8d%95%e8%bf%9b%e7%a8%8bio%e5%a4%8d%e7%94%a8libevent/
4, libevent in the bufferevent principle-nengm-Blog Park
Https://www.cnblogs.com/nengm1988/p/8203784.html
5. Eventloop-swoole-swoole Document Center
Https://wiki.swoole.com/wiki/page/242.html
PHP's high-performance I/O framework: Libevent (iii)