zoukankan      html  css  js  c++  java
  • 使用swoole编写简单的echo服务器

    server.php代码如下:

    <?php
    class EchoServer {
        protected $serv = null;
    
        public function __construct() {
            $this->serv = new swoole_server('0.0.0.0', 8888);
            //配置参数
            $this->serv->set(array(
                'worker_num' => 4,
                'daemonize' => 0,
            ));
            //注册回调函数
            $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'));
            //启动服务
            $this->serv->start();
        }
    
        public function start($serv) {
            echo "start 
    ";
        }
    
        //有客户端连接时
        public function connect($serv, $fd) {
            echo "connect 
    ";
            $serv->send($fd, "hello 
    ");
        }
    
        public function close($serv, $fd) {
            echo "close 
    ";
        }
    
        public function receive($serv, $fd, $from_id, $data) {
            echo "get message {$fd} : {$data} 
    ";
            //向客户端发送信息
            $serv->send($fd, $data . "
    ");
        }
    }
    
    $serv = new EchoServer();

    client.php代码如下:

    <?php
    class EchoClient {
        protected $client = null;
    
        public function __construct() {
            //注意这里需设置为异步,不然下面无法设置事件回调函数
            $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'));
            //连接服务端
            $this->client->connect('0.0.0.0', 8888);
        }
    
        public function connect($client) {
            echo "connect 
    ";
        }
    
        public function receive($client, $data) {
            echo "server send: {$data}";
    
            //向标准输出写入数据
            fwrite(STDOUT, "请输入消息:");
            //获取标准输入数据
            $msg = trim(fgets(STDIN));
            //向服务端发送数据
            $client->send($msg);
        }
    
        public function close($client) {
            echo "close 
    ";
        }
    
        public function error($client) {
            echo "error 
    ";
        }
    }
    
    $cli = new EchoClient();

    然后分别运行这两个脚本

    > /data/php56/bin/php server.php
    > /data/php56/bin/php client.php  

    运行结果如下:

  • 相关阅读:
    Mysql如何进行分组,并且让每一组的结果按照某个字段排序,并且获取每一组的第一个字段
    Mysql报错:Packet for query is too large (1121604 > 1048576).You can change this value on the server by setting the max_allowed_packet variable
    JavaScript判断对象有没有定义
    本地设置VirtualBox虚拟机
    Mysql关于时间排序的问题
    PHP实现页面静态化
    301重定向的两种实现方法
    判断浏览器类型
    javascript DOM事件总结
    装饰器模式
  • 原文地址:https://www.cnblogs.com/jkko123/p/6524280.html
Copyright © 2011-2022 走看看