page contents

swoole 第5次课-依葫芦画瓢:加速tp6,实现swoole聊天室、队列消息和服务通信3

加速tp swoole聊天室 swoole队列 swoole服务通信


接上

4.案例

功能:机器识别人 =》把信息传递给服务端 =》 服务端接收到这个信息之后又会返回给机器

要求:实现这个通信过程

提示:机器人是一个服务 ,后端也是个服务;机器利用客户端向后端发送信息,后端会回复 -》也可以以客户端的方式回复

           利用tcpserver与client

如下是图示

attachments-2020-12-z3BAIWut5fd00efd91a4a.png

案例分析:

attachments-2020-12-RhkdhJPw5fd00fe226608.png

实现思路及过程:

attachments-2020-12-1sTj95CB5fd00fea9c132.png

知识补充--多端口监听:

https://wiki.swoole.com/#/server/port

具体实现源码:

客户端:class\05\machine\tcpClient.php

<?php
$client = new Swoole\Client(SWOOLE_SOCK_TCP);

if (!$client->connect('192.168.204.168', 9501, -1)) {
exit("connect failed. Error: {$client->errCode}\n");
}
//需要注意这两是要作为一组,否则recv处于等待状态
$data = [
//'method' => 'machineStop',
'method' => 'machineInfo',
'msg' => "shineyork",
'data' => "lll",
'code' => 9,
//'code' => 10
];
$client->send(json_encode($data));
echo $client->recv()."\n";
$client->close();

后台服务:class\05\machine\server\server.php

<?php

use \Swoole\Server;

class AdminServer
{

private $port = 9501;

private $host = "0.0.0.0";

/**
* @var Server
*/
private $server;

function __construct()
{
// var_dump(swoole_get_local_ip());
echo swoole_get_local_ip()['ens33'].":".$this->port."\n";
$this->server = new Server($this->host, $this->port);
$this->onEvnet();
}

public function connect(Server $server, $fd)
{
echo "Client: Connect \n";
}
public function receive(Server $server, $fd, $fromid, $data)
{
echo "接收到机器人的信息";
var_dump($data);

/**
* [
* 'method' = 'machineInfo'|'machineStop',
* 'code' = 200 | 404,
* 'msg' = "信息",
* 'data' = $data
* ]
*/
$data = json_decode($data, true);
$this->{$data['method']}($server, $fd, $fromid, $data);
}
public function machineInfo($server, $fd, $framid, $data)
{
var_dump("machineInfo");
$server->send($fd, json_encode(["code" => 200, "msg" => "ok"]));
}
public function machineStop($server, $fd, $framid, $data)
{
var_dump("去停止机器");
$return = $this->send('127.0.0.1', 9555, json_encode($data));
var_dump("停止机器");
$server->send($fd, json_encode(["code" => 200, "msg" => "ok"]));
}

public function close(Server $server, $fd)
{
echo "Client: Close.\n";
}

public function send($ip, $port, $data)
{
$client = new Swoole\Client(SWOOLE_SOCK_TCP);
if (!$client->connect($ip, $port, -1)) {
exit("connect failed. Error: {$client->errCode}\n");
}
$client->send($data);
$return = $client->recv();
$client->close();
return $return;
}


/**
* 注册swoole事件
* @method onEvnet
* 六星教育 @shineyork老师
* @return [type] [description]
*/
protected function onEvnet()
{
$this->server->on('connect', [$this, 'connect']);
$this->server->on('receive', [$this, 'receive']);
$this->server->on('close' , [$this, 'close']);
}

public function start()
{
$this->server->start();
}
}


(new AdminServer)->start();


机器人服务--端口监听:class\05\machine\client\listen.php

<?php

use \Swoole\Server;

class Listen
{
private $port = 9555;

private $host = "127.0.0.1";

private $listen;

/**
* @var Server
*/
private $server;

function __construct($server)
{
$this->server = $server;
$this->listen = $server->listen("127.0.0.1", 9555, SWOOLE_SOCK_TCP);
$this->onEvnet();
echo "listen : 127.0.0.1:9555\n";
}

public function connect(Server $server, $fd)
{
echo "Client: Connect \n";
}
public function receive(Server $server, $fd, $fromid, $data)
{
var_dump("接收到信息");
$data = json_decode($data, true);
if ($data['code'] == 9) {
var_dump("stop server");
$this->server->shutdown();
} else {
$server->send($fd, '经由server转发,通过监控服务listen返回的信息');//$server->send($fd, $return); 经测试,这行注释及前面的无法发送给客户端
}
}
public function close(Server $server, $fd)
{
echo "Client: Close.\n";
}

/**
* 注册swoole事件
* @method onEvnet
* 六星教育@shineyork老师
* @return [type] [description]
*/
protected function onEvnet()
{
$this->listen->on('connect', [$this, 'connect']);
$this->listen->on('receive', [$this, 'receive']);
$this->listen->on('close' , [$this, 'close']);
}
}

机器人服务:class\05\machine\client\machine.php

<?php
require 'listen.php';

use \Swoole\Server;

class Machine
{
private $port = 9505;

private $host = "0.0.0.0";

// private $listen;

/**
* @var Server
*/
private $server;

function __construct()
{
$this->server = new Server($this->host, $this->port);
echo swoole_get_local_ip()['ens33'].":".$this->port."\n";
$this->onEvnet();

//开启监听
(new Listen($this->server));
}

public function connect(Server $server, $fd)
{
echo "Client: Connect \n";
}
public function receive(Server $server, $fd, $fromid, $data)
{
$return = $this->send('192.168.204.168', 9501, $data);
//$return = $this->send('192.168.204.168', 9505, $data);
echo "获取数据\n";
var_dump($return);
$server->send($fd, $return);
}
public function close(Server $server, $fd)
{
echo "Client: Close.\n";
}

public function send($ip, $port, $data)
{
$client = new Swoole\Client(SWOOLE_SOCK_TCP);
if (!$client->connect($ip, $port, -1)) {
exit("connect failed. Error: {$client->errCode}\n");
}
$client->send($data);
$return = $client->recv();
$client->close();
return $return;
}

/**
* 注册swoole事件
* @method onEvnet
* 六星教育 @shineyork老师
* @return [type] [description]
*/
protected function onEvnet()
{
$this->server->on('connect', [$this, 'connect']);
$this->server->on('receive', [$this, 'receive']);
$this->server->on('close' , [$this, 'close']);
}

public function start()
{
$this->server->start();
}
}
(new Machine)->start();


运行1:客户端给后台服务器发送一般消息

attachments-2020-12-i2wfecgA5fd0b6f1edafb.png


运行2:客户端给后台服务器发送停止机器人服务

attachments-2020-12-y4NzXdZe5fd0b73d47b63.png

运行3

attachments-2020-12-XaO0G59m5fd49caf267f2.png

  1. 注意: listen中的receive方法中,code==9的else部分
if ($data['code'] == 9) {
var_dump("stop server");
$this->server->shutdown();
} else {
//$server->send($fd, $return);
$server->send($fd, '经由server转发,通过监控服务listen返回的信息');//没收到
}

这里通过控制服务,发给客户端的信息没收到。原因不明 

待查原因


  • 发表于 2020-12-09 07:35
  • 阅读 ( 784 )

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
吉洪叶
吉洪叶

21 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1320 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章