server端:
/**
* Created by PhpStorm.
* User: aaa
* Date: 2019/6/4
* Time: 17:29
*/
class Server
{
private $serv;
//构造函数中完成基本设置,启动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 设置功能监听与对应回调函数
/*
* 必须给swoole_server绑定两个回调函数:onTask和onFinish。这两个回调函数分别用于执行Task任务和处理Task任务的返回结果
*/
$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之后,会调用onConnect方法
public function onConnect($cli) {
//执行发送
$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";
//收到服务器推送消息后,关闭当前链路
$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;
}
}
//发送模拟请求100次
for($i=0;$i<100;$i++){
$client = new Client();
$client->setMsgId($i);
$client->connect();
}