server端:
/**
* Created by PhpStorm.
* User: aaa
* Date: 2019/6/4
* Time: 17:29
*/
class Server
{
private $serv;
? ? //構(gòu)造函數(shù)中完成基本設(shè)置,啟動Service
? ? public function __construct()
{
$this->serv = new swoole_server('0.0.0.0', 9001);
? ? ? ? $this->serv->set(array(
'worker_num'? => 8,
? ? ? ? ? ? ? ? 'daemonize'? => false, //是否作為守護進程,此配置一般配合log_file使用
? ? ? ? ? ? ? ? 'max_request' => 1000,
? ? ? ? ? ? ? ? 'log_file'? ? => './swoole.log',
? ? ? ? ? ? ? ? 'task_worker_num' => 8
? ? ? ? ));
? ? ? ? //Add lisene 設(shè)置功能監(jiān)聽與對應(yīng)回調(diào)函數(shù)
/*
* 必須給swoole_server綁定兩個回調(diào)函數(shù):onTask和onFinish芦瘾。這兩個回調(diào)函數(shù)分別用于執(zhí)行Task任務(wù)和處理Task任務(wù)的返回結(jié)果
*/
? ? ? ? $this->serv->on('Start', array($this, 'onStart'));
? ? ? ? $this->serv->on('Connect', array($this, 'onConnect'));
? ? ? ? $this->serv->on("Receive", array($this, 'onReceive'));
? ? ? ? $this->serv->on("Close", array($this, 'onClose'));
? ? ? ? $this->serv->on("Task", array($this, 'onTask'));
? ? ? ? $this->serv->on("Finish", array($this, 'onFinish'));
? ? ? ? //開啟
? ? ? ? $this->serv->start();
? ? }
public function onStart($serv) {
echo SWOOLE_VERSION . " onStart\n";
? ? }
public function onConnect($serv, $fd) {
echo $fd."Client Connect.\n";
? ? }
public function onReceive($serv, $fd, $from_id, $data) {
echo "Get Message From Client {$fd}:{$data}\n";
? ? ? ? // send a task to task worker.
? ? ? ? $param = array(
'fd' => $fd,
? ? ? ? ? ? 'sData'=> $data,
? ? ? ? );
? ? ? ? // start a task
? ? ? ? $result = $serv->task(json_encode($param));
? ? ? ? return $result;
? ? }
public function onClose($serv, $fd) {
echo "Client Close.\n";
? ? }
public function onTask($serv, $task_id, $from_id, $data) {
echo "This Task {$task_id} from Worker {$from_id}\n";
? ? ? ? echo "Data: {$data}\n";
? ? ? ? $proId = uniqid();
? ? ? ? $randomSec = random_int(1,2);
? ? ? ? sleep($randomSec);
? ? ? ? $fd = json_decode($data, true);
? ? ? ? $sData = $fd['sData'];
? ? ? ? $serv->send($fd['fd'], "Pro:{$proId} Data in Task {$task_id}? From Id is: {$from_id}? STRANSACTION: {$sData}? ? SEND COMP OK");
? ? ? ? return "\n=====Task {$task_id}'s result? SEND COMPLETED =====";
? ? }
public function onFinish($serv,$task_id, $data) {
$fd = json_decode($data, true);
? ? ? ? $serv->send($fd['fd'], "Task {$task_id}? Send Finish");
? ? ? ? echo "\nOK Task {$task_id} finish\n";
? ? ? ? echo "\n Ok Result: {$data}\n";
? ? }
}
$server = new server();
客戶端:
/**
* Created by PhpStorm.
* User: aaa
* Date: 2019/6/4
* Time: 22:43
*/
class Client
{
private $client;
? ? private $msgId;
? ? 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() {
if(!$fp = $this->client->connect("127.0.0.1", 9001 , 1)) {
echo "Error: {$fp->errMsg}[{$fp->errCode}]\n";
? ? ? ? ? ? return;
? ? ? ? }
}
//connect之后,會調(diào)用onConnect方法
? ? public function onConnect($cli) {
//執(zhí)行發(fā)送
? ? ? ? $result =? $this->send("Client Sent Msg ID: ".$this->msgId);
? ? ? ? echo "\nClient got Server Response is ".$result;
? ? }
public function onClose($cli) {
echo "Client close connection\n";
? ? }
public function onError() {
}
public function onReceive($cli, $data) {
echo "\n Received From Server : ".$data."\n";
? ? ? ? //收到服務(wù)器推送消息后真友,關(guān)閉當前鏈路
? ? ? ? $cli->close();
? ? }
public function send($data) {
$result =? $this->client->send($data);
? ? ? return $result;
? ? }
public function isConnected($cli) {
return $this->client->isConnected();
? ? }
public function setMsgId($msgId){
$this->msgId = $msgId;
? ? }
}
//發(fā)送模擬請求100次
for($i=0;$i<100;$i++){
$client = new Client();
? ? $client->setMsgId($i);
? ? $client->connect();
}