因工作內容需求 服務器端處理完 小程序二維碼後 後臺頁面需要實時進行更新展示
之前一直用 GatewayWorker 組件來處理後臺頁面長連接顯示
但是不用前端頁面 用後臺來觸發時就不行了 經過查找找到了 GatewayClient組件
關於GatewayClient
源碼:
https://github.com/walkor/GatewayClient
注意:
如果GatewayClient和GatewayWorker不是在同一臺服務器上,則需要先將start_gateway.php中的lanIp改成當前服務器的內網ip(如果不在一個內網可改成公網ip)。
如果GatewayClient和GatewayWorker在同一臺服務器上運行,則不用做任何更改,直接按照示例使用GatewayClient即可。
通過GatewayClient發送的數據不會經過Event.php,而是直接經由Gateway進程轉發給客戶端。
GatewayClient無法接收客戶端發來的數據。
如何加入tp5中呢
首先按照 GatewayWorker 的版本來下載對應的 GatewayClient
如何查看 GatewayWorker版本 在
如何知道GatewayWorker版本
打開GatewayWorker/Gateway.php,在Gateway類內部VERSION常量標記了當前GatewayWorker的版本,例如下面GatewayWorker版本號爲2.0.2。
如果沒有找到VERSION常量,說明是1.0版本。
GatewayWorker/Gateway.php
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace GatewayWorker;
use Workerman\Connection\TcpConnection;
use \Workerman\Worker;
use \Workerman\Lib\Timer;
use \Workerman\Autoloader;
use \Workerman\Connection\AsyncTcpConnection;
use \GatewayWorker\Protocols\GatewayProtocol;
/**
*
* Gateway,基於Worker開發
* 用於轉發客戶端的數據給Worker處理,以及轉發Worker的數據給客戶端
*
* @author walkor<[email protected]>
*
*/
class Gateway extends Worker
{
/**
* ######版本######
* @var string
*/
const VERSION = '2.0.2';
知道自己的版本後 下載對應的 GatewayClient
把下載好的 GatewayClient 放在 vendor 下
在需要的類中引入
use \GatewayClient\Gateway;
require_once '/www/wwwroot/******/vendor/GatewayClient/Gateway.php';
require_once要引入你服務器的絕對路徑
調用時 只需要
function ceshi(){
//你設置好的 服務器IP
Gateway::$registerAddress = '127.0.0.1:1238';
//要給全部人員發送的消息
Gateway::sendToAll(11111);
}
這樣就可以進行簡單使用了
客戶端使用示例
require_once '/your/path/GatewayClient/Gateway.php';
/**
* gatewayClient 3.0.0及以上版本加了命名空間
* 而3.0.0以下版本不需要use GatewayClient\Gateway;
**/
use GatewayClient\Gateway;
/**
*====這個步驟是必須的====
*這裏填寫Register服務的ip(通常是運行GatewayWorker的服務器ip)和端口
*注意Register服務端口在start_register.php中可以找到(chat默認是1236)
*這裏假設GatewayClient和Register服務都在一臺服務器上,ip填寫127.0.0.1
**/
Gateway::$registerAddress = '127.0.0.1:1236';
// 以下是調用示例,接口與GatewayWorker環境的接口一致
// 接口具體使用方法見《Lib\Gateway類提供的接口》一章
// 注意除了不支持sendToCurrentClient和closeCurrentClient方法
// 其它方法都支持
Gateway::sendToAll($data);
Gateway::sendToClient($client_id, $data);
Gateway::closeClient($client_id);
Gateway::isOnline($client_id);
Gateway::bindUid($client_id, $uid);
Gateway::isUidOnline($uid);
Gateway::getClientIdByUid($uid);
Gateway::unbindUid($client_id, $uid);
Gateway::sendToUid($uid, $data);
Gateway::joinGroup($client_id, $group);
Gateway::sendToGroup($group, $data);
Gateway::leaveGroup($client_id, $group);
Gateway::getClientCountByGroup($group);
Gateway::getClientSessionsByGroup($group);
Gateway::getAllClientCount();
Gateway::getAllClientSessions();
Gateway::setSession($client_id, $session);
Gateway::updateSession($client_id, $session);
Gateway::getSession($client_id);