Swoole first recognized, swoole first recognized

Source: Internet
Author: User
Tags php server

Swoole first recognized, swoole first recognized

Official definition:

Swoole: redefine PHP

PHP's asynchronous, parallel, and high-performance network communication engine, written in pure C language, provides PHP-language asynchronous multi-thread servers, asynchronous TCP/UDP network clients, asynchronous MySQL, asynchronous Redis, database Connection Pool, AsyncTask, message queue, millisecond timer, asynchronous file read/write, asynchronous DNS query. Swoole has built-in Http/WebSocket server/client and Http2.0 server.
Swoole can be widely used in the Internet, mobile communication, enterprise software, cloud computing, online games, Iot, IOV, Smart Home, and other fields. Using PHP + Swoole as the network communication framework can greatly improve the efficiency of enterprise it r & D teams and focus more on developing innovative products.

 

Swoole extension installation and case study Source: http://wiki.swoole.com/wiki/page/6.html

 

Simple case:

<? Phpclass Server {private $ serv; public function _ construct () {$ this-> serv = new swoole_server ("0.0.0.0", 9501 ); $ this-> serv-> set (array ('worker _ num' => 8, 'daemonize '=> false, 'max _ request' => 10000, 'dispatch _ mode' => 2, 'debug _ mode' => 1); $ this-> serv-> on ('start', array ($ this, 'onstart'); $ this-> serv-> on ('connect ', array ($ this, 'onconnect ')); $ this-> serv-> on ('receive ', array ($ this, 'onrecei Ve '); $ this-> serv-> on ('close', array ($ this, 'onclose ')); $ this-> serv-> start ();} public function onStart ($ serv) {echo "Start \ n";} public function onConnect ($ serv, $ fd, $ from_id) {$ serv-> send ($ fd, "Hello {$ fd }! ");} Public function onReceive (swoole_server $ serv, $ fd, $ from_id, $ data) {echo" Get Message From Client {$ fd }: {$ data} \ n ";} public function onClose ($ serv, $ fd, $ from_id) {echo" Client {$ fd} close connection \ n ";}} // start the server $ Server = new server ();

 

<? Phpclass Client {private $ client; public function _ construct () {$ this-> client = new swoole_client (SWOOLE_SOCK_TCP);} public function connect () {if (! $ This-> client-> connect ("127.0.0.1", 9501, 1) {echo "Error: {$ fp-> errMsg} [{$ fp-> errCode}] \ n ";}$ message = $ this-> client-> recv (); echo "Get Message From Server: {$ message} \ n"; fwrite (STDOUT, "Enter the Message:"); $ msg = trim (fgets (STDIN )); $ this-> client-> send ($ msg) ;}$ client = new Client (); $ client-> connect ();

 

Open two terminal inputs: php server. php client. php to see the effect!

 

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.