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\n"; 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}\n"; 26 $serv->send($fd,$data); 27 28 } 29 30 public function onClose($serv,$fd,$from_id){ 31 echo "client {$fd} close connection \n"; 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}]\n"; 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}\n"; 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}]\n"; 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}\n"; 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