首页 > 编程语言 >thinkphp结合workerman和gateway实现数据同步

thinkphp结合workerman和gateway实现数据同步

时间:2023-05-31 15:04:00浏览次数:42  
标签:function gateway thinkphp post workerman Gateway con

thinkphp结合workerman和gateway实现数据同步

0x10安装扩展

composer require topthink/think-worker=2.0.*
composer require workerman/gatewayclient

此安装仅针对5.1,其他版本根据官方要求安装。client仅针对3.0.8以上,具体看GitHub

0x20服务端Gateway

0x21config/worker_gateway.php

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <[email protected]>
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | Workerman设置 仅对 php think worker:gateway 指令有效
// +----------------------------------------------------------------------
return [
    // BusinsessWorker配置
    'businessWorker'        => [
        'name'         => 'BusinessWorker',
        'count'        => 1,
        'eventHandler' => '\app\http\Gateway',
    ],

];

0x22 application/http/Gateway.php

<?php

namespace app\http;

use GatewayWorker\Lib\Gateway as Way;

class GateWay
{
    public static function onWebSocketConnect($id, $data)
    {
        dump('设备连接,ID:' . $id);
        Way::bindUid($id, '1');
    }

    public static function onMessage($client_id, $data)
    {
        Way::sendToClient($client_id, '服务端消息');
    }
}

0x23 向客户端发送消息

//use GatewayClient\Gateway;
$post = request()->post();
Gateway::$registerAddress = '127.0.0.1:1236';
// 向任意uid的网站页面发送数据
Gateway::sendToUid('1', json_encode($post));
dump($post);

0x30客户端代码

客户端需要用到command,即thinkphp命令行

protected function execute(Input $input, Output $output)
    {
		//忽略....
        $a = new Worker();
        $a->onWorkerStart = function ($work) {
            $con = new AsyncTcpConnection('ws://192.168.25.149:2346');//服务端地址
            $con->onConnect = function (AsyncTcpConnection $con) {
                // 连接成功的事件
            };

            $con->onMessage = function (AsyncTcpConnection $con, $data) {
                dump($data);//来自服务端的消息
            };

            $con->connect();
        };
        Worker::runAll();
    }

0x40效果图

您的网络不支持ipv6

您的网络不支持ipv6

您的网络不支持ipv6

您的网络不支持ipv6

您的网络不支持ipv6

标签:function,gateway,thinkphp,post,workerman,Gateway,con
From: https://www.cnblogs.com/xfstu/p/17446125.html

相关文章

  • thinkphp6数据库相关操作
    一、多表联合查询$list=UserModel::where(function(Query$query){$data=$this->request->param();$query->where('user_type',2);。。。。})->haswhere('UserParams',function(Query$query){......
  • Sentinel整合Spring Cloud Gateway、Zuul详解
    Sentinel支持对SpringCloudGateway、Zuul等主流的APIGateway进行限流。编辑切换为居中添加图片注释,不超过140字(可选)Sentinel1.6.0引入了SentinelAPIGatewayAdapterCommon模块,此模块中包含网关限流的规则和自定义API的实体和管理逻辑:GatewayFlowRule:网关限流规......
  • thinkphp正确开发系列:使用全局中间件统计当天活跃用户
    背景:pv、uv大家应该了解,不懂得搜索一下就知道了,现在有这么一个需求:统计当天有哪些用户登录了系统,也即:同一个用户,在一天之内第一次登录系统,记为1次,一天之内的后续登录(在登录状态刷新页面)忽略,项目使用的是thinkphp6,前后端分离架构。 实现方案:我们知道,php的开发框架,如:laravel、t......
  • nginx,gateway配置
    反向代理:监听192.168.10.129:9001如果url中有edu就把请求转发给http://ip1:port1,如果url中有vod就把请求转发给http://ip2:port2nginx配置:server{listen9001;server_name192.168.10.129;location~/edu/{proxy_passhttp://ip1:port1}location~/vod/{proxy_passhtt......
  • 七牛云上传,网关超时502 Bad Gateway
    本地可以正常上传七牛云,服务器上网站正常访问,七牛云无法上传,报错502 BadGateway原因:域名解析节点出错。域名解析节点出现问题可能会导致网络请求无法正常访问到对应的服务器,因而会影响上传七牛云图片这类网络操作。例如,如果您的浏览器无法将域名正确解析为IP地址,那么就无法......
  • 7.Gateway
    8.Gateway8.1.介绍8.1.1.网关介绍网关是微服务最边缘的服务,直接暴露给用户,用来做用户和微服务的桥梁没有网关:客户端直接访问我们的微服务,会需要在客户端配置很多的ip:port,如果user-service并发比较大,则无法完成负载均衡。有网关:客户端访问网关,网关来访问微服务,(网关可以和注......
  • SpringGateway不用注册中心实现负载均衡
    1、pom<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/PO......
  • 关于AWS中VPC下的IGW-internet gateway的创建与说明
    关于AWS中VPC下有一个资源叫做 Internetgateways,也就是我们常说的IGW关于IGW,我们可以参考官网文档 AmazonVPC/ UserGuide中有如下说明:Aninternetgatewayisahorizontallyscaled,redundant,andhighlyavailableVPCcomponentthatallowscommunicationbetw......
  • php语言开发宝塔部署thinkphp考试平台管理系统
    技术架构PHP7.2+Thinkphp6+React+UmiJs+nginx+mysql5.7cnetos7以上+宝塔面板搭建教程1.下载源码,宝塔添加一个站点,PHP版本选择7.2。2.压缩codes后台代码目录,上传后台代码到宝塔的网站根目录当中解压缩,关闭防跨站,运行目录指向public,伪静态选择thinkphp,设置为:locat......
  • 博学谷学习记录 自我总结 用心分享 | Alibaba- GateWay
    SpringCloudNetflix项目进入维护模式,SpringCloudNetflix将不再开发新的组件,我们知道SpringCloud版本迭代算是比较快的,因而出现了很多中岛的ISSUE都来不及Fix就又推另一个Release了。进入维护模式意思就是目前已知以后一段时间SpringCloudNetflix提供的服......