zoukankan      html  css  js  c++  java
  • swoole1--搭建echo服务器

    1.安装swoole :pecl install swoole,然后修改php.ini 开启swoole扩展:extension=swoole.so

    2.写一个服务器Server.php

      1 <?php
      2 
      3 class Server{
      4     private $serv;
      5 
      6     public function __construct(){
      7         $this->serv = new swoole_server("0.0.0.0",9501);
      8         $this->serv->set(['worker_num'=>8,'daemonize'=>false]);
           //注册服务器回调时间
    9 $this->serv->on('Start',[$this,'onStart']); 10 $this->serv->on('Connect',[$this,'onConnect']); 11 $this->serv->on('Receive',[$this,'onReceive']); 12 $this->serv->on('Close',[$this,'onClose']); 13 $this->serv->start(); 14 } 15 16 public function onStart($serv){ 17 echo "Start "; 18 } 19 20 public function onConnect($serv,$fd,$from_id){ 21 $serv->send($fd,"hello,$fd,欢迎连接"); 22 } 23 24 public function onReceive($serv,$fd,$from_id,$data){ 25 echo "get message from client {$fd},{$data} "; 26 $serv->send($fd,$data); 27 28 } 29 30 public function onClose($serv,$fd,$from_id){ 31 echo "client {$fd} close connection "; 32 } 33 34 } 35 36 @$server = new Server();//开了xDebug,这里不用@会弹出警告信息

    3.写一个客户端Client.php

     1 <?php
     2 class Client
     3 {
     4     private $client;
     5 
     6     public function __construct() {
     7         $this->client = new swoole_client(SWOOLE_SOCK_TCP);//同步tcp客户端
     8     }
     9     
    10     public function connect() {
    11         if( !$this->client->connect("127.0.0.1", 9501 , 1) ) {
    12             echo "Error: {$this->client->errMsg}[{$this->client->errCode}]
    ";
    13         }
    14         
    15         fwrite(STDOUT, "请输入消息 Please input msg:"); //STDOUT,STDIN : php的伪协议,标准输入(php://stdin)输出(php://stdout)流,这里是将提示信息输出到命令行,类似echo
    16         $msg = trim(fgets(STDIN));//在这里fgets会读取命令行输入的内容,直到读取到换行符或者、EOF或者最大数据量的时候停止读取
    17         $this->client->send( $msg );
    18 
    19         $message = $this->client->recv();
    20         echo "Get Message From Server:{$message}
    ";
    21     }
    22 }
    23 
    24 $client = new Client();
    25 $client->connect();//这里只能收发一次消息后关闭

    4.异步客户端

      1 <?php
      2 
      3 class Client{
      4     private $client;
      5 
      6     public function __construct(){
      7         $this->client = new swoole_client(SWOOLE_SOCK_TCP,SWOOLE_SOCK_ASYNC);//加了异步之后要注册回调,不然报错
      8         $this->client->on('connect',[$this,'onConnect']);
      9         $this->client->on('receive',[$this,'onReceive']);
     10         $this->client->on('error',[$this,'onError']);
     11         $this->client->on('close',[$this,'onClose']);
     12     }
     13 
     14     public function connect(){
     15         if(!$this->client->connect("127.0.0.1",9501,1)){
     16             echo "Error :{$this->client->errMsg}[{$this->client->errCode}]
    ";
     17         }
     18     //客户端异步之后就不能再同步发消息
     19     //  fwrite(STDOUT,"请输入消息(please input msg):");
     20     //  $msg = trim(fgets(STDIN));
     21     //  $this->client->send($msg);
     22 
     23     //  $message = $this->client->recv();
     24     //  echo "get message from Server:{$message}
    ";
     25     }
     26 
     27     public function onConnect($cli){
     28         $cli->send("hello server ,I'm connect".PHP_EOL);
     29     }
     30 
     31     public function onReceive($cli,$data){
     32         echo "receive message from server:".$data.PHP_EOL;
     33         fwrite(STDOUT,"回复:");
     34         $msg = trim(fgets(STDIN));
     35         $cli->send($msg);
     36     }
     37 
     38     public function onError($cli){
     39         echo "Connect faild".PHP_EOL;
     40     }
     41 
     42     public function onClose($cli){
     43         echo "client connect close".PHP_EOL;
     44     }
     45 
     46 }
     47 
     48 $client = new Client();
     49 $client->connect();

    5.启动Server.php,,Client.php

         

  • 相关阅读:
    [Oracle工程师手记]如何收集 RMAN 工作时的 10046 trace
    [Linux]Linux环境,如何查找一个目录下所有包含特定字符串的文件
    [Oracle工程师手记]如何找到 RMAN 的所有 session
    PowerShell提交HTTP Request Post请求
    PowerShell计算字符串MD5 Hash值
    《异类》笔记
    《哈弗商学院 判断与决策心理学课》笔记
    《影响力》笔记
    《金字塔原理》笔记
    三月十九日,偶感
  • 原文地址:https://www.cnblogs.com/jint-php7/p/11644062.html
Copyright © 2011-2022 走看看