本文实例讲述了php使用swoole编写简单的echo服务器。分享给大家供大家参考,具体如下:
rver.php代码如下:
<?phpclass echorver { protected $rv = null; public function _关于动物的诗_construct() { $this->rv = new swoole_rver('0.0.0.0', 8888); //配置参数 $this->rv->t(array( 'worker_num' => 4, 'daemonize' => 0, )); //注册回调函数 $this->rv->on('start', array($this, 'start')); $this->rv->on('connect',旧三民主义 array($this, 'connect')); $this->rv->on('receive', array($this, 'receive')); $this->rv->on('clo', array($this, 'clo')); //启动服务 $this->rv->start(); } public function start($rv) { echo "start \n"; } //有客户端连接时 public function connect($rv, $fd) { echo "connect \n"; $rv->nd($fd, "hello \n"); } public function cl2019考研时间o($rv, $fd) { echo "clo \n"; } public function receive($rv, $fd, $from_id, $data) { echo "get message {$fd} : {$data} \n"; //向客户端发送信息 $rv->nd($fd, $data . "\n"); }} $rv = new echorver();
client.php代码如下:
<?phpclass echoclient { protected $client = null; public function __construct() { //注意这里需设置为异步,不然下面无法设置事件回调函数 $this->client = new swoole_client(swoole_sock_tcp, swoole骆驼祥子读书笔记300字_sock_async); $this->client->on('connect', array($this, 'connect')); $this->client->on('receive', array($this, 'receive')); $this->client->on('clo', array($this, 'clo')); $this->client->on('error', array($this, 'error')); //连接服务端 $this->client->connect('0.0.0.0', 8888); } public function connect($client) { echo "connect \n"; } public function receive($client, $data) { echo "rver nd: {$data}"; //向标准输出写入数据 fwrite(stdout, "请输入消息:"); //获取标准输入数据 $msg = trim(fgets(stdin)); //向服务端发送数据 $client->nd($msg); } public function clo($client) { echo "clo \n"; } publi如何做市场调查c function error($client) { echo "error \n"; }} $cli = new echoclient();
然后分别运行这两个脚本
> /data/php56/bin/php rver.php> /data/php56/bin/php client.php
运行结果如下:
本文发布于:2023-04-08 19:34:50,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/6cc0f4618f91d77e353ffe9edc11f713.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:PHP使用swoole编写简单的echo服务器示例.doc
本文 PDF 下载地址:PHP使用swoole编写简单的echo服务器示例.pdf
留言与评论(共有 0 条评论) |