前言:
使用GatewayWorker与thinkphp6结合时不需要添加其他的依赖,上一篇已经介绍了怎样运行一个简单的GatewayWorker,不知道的可以看前一篇文章。我的使用目的是GatewayWorker进行数据通信在thinkphp中构建业务逻辑处理。
一、对GatewayWorker进行简单的改造
1、将下载的GatewayWorker解压然后放置到thinkphp中的extend目录下
2、在GatewayWorker 中新建一个php文件并命名为SendMessage.php,在文件内容中输入如下内容:
<?php
namespace GatewayWorker;
require_once 'GatewayClient/Gateway.php';
use GatewayClient\Gateway;
class SendMessage
{
public function send_message($send_ip)
{
Gateway::$registerAddress=$send_ip;
Gateway::sendToAll('jijim');
}
}
3、下载安装GatewayClient,你可以依据手册选择合适你的GatewayWorker版本,然后将GatewayClient解压后放置在extend/GatewayWorker目录下,GatewayClient文件夹命名为GatewayClient如下:
前期对GatewayWorker的改造已经完成。
二、使用thinkphp中的方法进行消息推送
在thinkphp6的index控制器中的内容替换为如下代码:
<?php
namespace app\controller;
use app\BaseController;
use GatewayWorker\SendMessage;
class Index extends BaseController
{
protected $send_ip="127.0.0.1:1238";
public function index()
{
return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">16载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
}
public function hello()
{
$send_ip='127.0.0.1:1238';
$gat=new SendMessage();
$res=$gat->send_message($send_ip);
dump($res);
}
}
然后启动GatewayWorker,然后打开前端检查然后访问hello方法,就会收到后端推送来的内容“jijim”,至此GatewayWorker基本搭建完毕,如果有不解的可以通过QQ群:553668309与我取得联系。