PHP使用swoole編寫簡(jiǎn)單的echo服務(wù)器示例
本文實(shí)例講述了PHP使用swoole編寫簡(jiǎn)單的echo服務(wù)器。分享給大家供大家參考,具體如下:
server.php代碼如下:
<?php class EchoServer { protected $serv = null; public function __construct() { $this->serv = new swoole_server('0.0.0.0', 8888); //配置參數(shù) $this->serv->set(array( 'worker_num' => 4, 'daemonize' => 0, )); //注冊(cè)回調(diào)函數(shù) $this->serv->on('start', array($this, 'start')); $this->serv->on('connect', array($this, 'connect')); $this->serv->on('receive', array($this, 'receive')); $this->serv->on('close', array($this, 'close')); //啟動(dòng)服務(wù) $this->serv->start(); } public function start($serv) { echo "start \n"; } //有客戶端連接時(shí) public function connect($serv, $fd) { echo "connect \n"; $serv->send($fd, "hello \n"); } public function close($serv, $fd) { echo "close \n"; } public function receive($serv, $fd, $from_id, $data) { echo "get message {$fd} : {$data} \n"; //向客戶端發(fā)送信息 $serv->send($fd, $data . "\n"); } } $serv = new EchoServer();
client.php代碼如下:
<?php class EchoClient { protected $client = null; public function __construct() { //注意這里需設(shè)置為異步,不然下面無(wú)法設(shè)置事件回調(diào)函數(shù) $this->client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $this->client->on('connect', array($this, 'connect')); $this->client->on('receive', array($this, 'receive')); $this->client->on('close', array($this, 'close')); $this->client->on('error', array($this, 'error')); //連接服務(wù)端 $this->client->connect('0.0.0.0', 8888); } public function connect($client) { echo "connect \n"; } public function receive($client, $data) { echo "server send: {$data}"; //向標(biāo)準(zhǔn)輸出寫入數(shù)據(jù) fwrite(STDOUT, "請(qǐng)輸入消息:"); //獲取標(biāo)準(zhǔn)輸入數(shù)據(jù) $msg = trim(fgets(STDIN)); //向服務(wù)端發(fā)送數(shù)據(jù) $client->send($msg); } public function close($client) { echo "close \n"; } public function error($client) { echo "error \n"; } } $cli = new EchoClient();
然后分別運(yùn)行這兩個(gè)腳本
> /data/php56/bin/php server.php > /data/php56/bin/php client.php
運(yùn)行結(jié)果如下:
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php socket用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》及《php程序設(shè)計(jì)算法總結(jié)》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP5.4起內(nèi)置web服務(wù)器使用方法
- phpstorm遠(yuǎn)程連接服務(wù)器并實(shí)時(shí)更新發(fā)布代碼(thinkphp6.0.7)
- PhpStorm連接服務(wù)器并實(shí)現(xiàn)自動(dòng)上傳功能
- php實(shí)現(xiàn)的簡(jiǎn)單多進(jìn)程服務(wù)器類完整示例
- 在phpstudy集成環(huán)境下的nginx服務(wù)器下配置url重寫
- php服務(wù)器的系統(tǒng)詳解
- Windows服務(wù)器中PHP如何安裝redis擴(kuò)展
- PHP如何將圖片文件上傳到另外一臺(tái)服務(wù)器上
- 微信小程序上傳圖片到php服務(wù)器的方法
- PHP 內(nèi)置WEB服務(wù)器的簡(jiǎn)單使用
相關(guān)文章
php+mysql+jquery實(shí)現(xiàn)日歷簽到功能
本文主要介紹了php+mysql+jquery實(shí)現(xiàn)日歷簽到功能的過(guò)程與步驟,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02PHP實(shí)現(xiàn)redis限制單ip、單用戶的訪問(wèn)次數(shù)功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)redis限制單ip、單用戶的訪問(wèn)次數(shù)功能,結(jié)合實(shí)例形式分析了php連接redis及獲取、記錄客戶端信息,并限制客戶訪問(wèn)次數(shù)等操作技巧,需要的朋友可以參考下2018-06-06PHP中Session ID的實(shí)現(xiàn)原理實(shí)例分析
這篇文章主要介紹了PHP中Session ID的實(shí)現(xiàn)原理,結(jié)合實(shí)例形式分析了PHP中session的工作機(jī)制、調(diào)用原理、配置方法及使用技巧,需要的朋友可以參考下2019-08-08PHP實(shí)現(xiàn)Snowflake生成分布式唯一ID的方法示例
這篇文章主要給大家介紹了關(guān)于PHP實(shí)現(xiàn)Snowflake生成分布式唯一ID的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08php基于表單密碼驗(yàn)證與HTTP驗(yàn)證用法實(shí)例
這篇文章主要介紹了php基于表單密碼驗(yàn)證與HTTP驗(yàn)證用法,以實(shí)例形式較為詳細(xì)的分析了表單密碼驗(yàn)證與HTTP驗(yàn)證的原理與相關(guān)注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01php實(shí)現(xiàn)倒計(jì)時(shí)效果
這篇文章主要介紹了php實(shí)現(xiàn)倒計(jì)時(shí)效果,寫了一個(gè)考試系統(tǒng)剩余時(shí)間倒計(jì)時(shí)的顯示代碼和大家一起探討,需要的朋友可以參考下2015-12-12