头像

swoole 服务端heartbeat_check_interval心跳检测 客户端 swoole_timer_tick 发送心跳包 这里只是个demo参考

发布时间:2018-09-13 11:31:46

发布作者:admin

4997

服务端:

<?php
class TimerServer
{
        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 ,
            'heartbeat_check_interval' => 10
        ));
        $this->serv->on('WorkerStart', array($this, 'onWorkerStart'));
        $this->serv->on('Connect', array($this, 'onConnect'));
        $this->serv->on('Receive', array($this, 'onReceive'));
        $this->serv->on('Close', array($this, 'onClose'));
        $this->serv->start();
        }
        public function onWorkerStart( $serv , $worker_id) {
                // 在Worker进程开启时绑定定时器
        echo "onWorkerStart\n";
        // 只有当worker_id为0时才添加定时器,避免重复添加
        if( $worker_id == 0 ) {
                //$this->serv->addtick(5000);
 
                swoole_timer_tick(5000,function($id){
 
 
                        echo "time\n";
 
                });
 
        }
    }
    public function onConnect( $serv, $fd, $from_id ) {
        echo "Client {$fd} connect\n";
    }
    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";
    }
}
new TimerServer();
客户端:

<?php
class Client
{
        private $client;
 
        public function __construct() {
    $this->client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
    $this->client->on('Connect', array($this, 'onConnect'));
    $this->client->on('Receive', array($this, 'onReceive'));
    $this->client->on('Close', array($this, 'onClose'));
    $this->client->on('Error', array($this, 'onError'));
        }
 
        public function connect() {
                $fp = $this->client->connect("127.0.0.1", 9501 , 1);
                if( !$fp ) {
                        echo "Error: {$fp->errMsg}[{$fp->errCode}]\n";
                        return;
                }
        }
        public function onReceive( $cli, $data ) {
    echo "Get Message From Server: {$data}\n";
  }
  public function onConnect( $cli) {
    fwrite(STDOUT, "Enter Msg:");
    swoole_event_add(STDIN, function($fp){
                global $cli;
        fwrite(STDOUT, "Enter Msg:");
        $msg = trim(fgets(STDIN));
            $cli->send( $msg );
    });
 
swoole_timer_tick(5000, function () use ($cli) {
      $cli->send('beatheart');
});
 
  }
  public function onClose( $cli) {
      echo "Client close connection\n";
  }
  public function onError() {
  }
  public function send($data) {
        $this->client->send( $data );
  }
  public function isConnected() {
        return $this->client->isConnected();
  }
}
$cli = new Client();
$cli->connect();


相关文章:
  1. Mysql千万级数据量批量快速迁移
  2. php排列组合函数(无重复)
  3. Linux下编译安装MariaDB
  4. apache和nodejs共用80端口