1.安裝swoole :pecl install swoole,而後修改php.ini 開啓swoole擴展:extension=swoole.sophp
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.phpswoole
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.phptcp