123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use GatewayClient\Gateway;
- use think\Request;
- use Redis;
- //require_once VENDOR_PATH . 'workerman/gatewayclient/Gateway.php';
- /**
- * gateway绑定接口
- */
- class Gatewayworker extends Api
- {
- protected $noNeedLogin = ["userIsOnline","clearCharm","test"];
- protected $noNeedRight = ['*'];
- public function __construct(Request $request = null) {
- parent::__construct($request);
- $params_from = $this->request->request('params_from'); // 客户端
- if($params_from == "wxmin") {
- Gateway::$registerAddress = "127.0.0.1:1239";
- } else {
- Gateway::$registerAddress = "127.0.0.1:1238";
- }
- }
- public function test() {
- $client_id = $this->request->request('client_id'); // 客户端ID
- print_r(Gateway::getAllUidCount());exit;
- }
- /**
- * 绑定用户ID
- */
- public function bind() {
- $client_id = $this->request->request('client_id'); // 客户端ID
- if(!$client_id) {
- $this->error("请传入客户端ID");
- }
- $user_id = $this->auth->id;
- // 先判断当前用户是否在线
- // if(Gateway::isUidOnline($user_id)) {
- // // 发送一个踢下线的信息
- // $tcpArr = [];
- // $tcpArr["type"] = "outLogin";
- // $tcpArr['data'] = [
- // 'user_id' => $user_id
- // ];
- // $outLoginJson = json_encode($tcpArr);
- // Gateway::sendToUid($user_id,$outLoginJson);
- // }
- Gateway::bindUid($client_id, $user_id);
- // 切换用户在线状态
- \app\common\model\User::update(["is_online"=>1],["id"=>$user_id]);
- $this->success("绑定成功!");
- }
- //============================workerman 计时器==========================//
- /**
- * 服务器定时器:判断用户是否在线
- */
- public function userIsOnline() {
- // $userlist = \app\common\model\User::where(["is_online" =>1,"status"=>"normal"])->select();
- // if($userlist) {
- // foreach($userlist as $k =>$v){
- // if(!Gateway::isUidOnline($v["id"])) {
- // \app\common\model\User::update(["is_online"=>0],["id"=>$v["id"]]);
- // \app\common\model\User::update(["is_live"=>0],["id"=>$v["id"]]);
- // }
- // }
- // }
- }
- /**
- * 所有房间人数redis 魅力值清零
- */
- public function clearCharm() {
- // 获取所有房间ID信息
- // $party_id = \app\common\model\Party::where(["status"=>1])->column("id");
- // if($party_id) {
- // $redis = new Redis();
- // $redisconfig = config("redis");
- // $redis->connect($redisconfig["host"], $redisconfig["port"]);
- /*if ($redisconfig['redis_pwd']) {
- $redis->auth($redisconfig['redis_pwd']);
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select($redisconfig['redis_selectdb']);
- }*/
- // foreach($party_id as $v) {
- // $redis->del("hourCharm_".$v);
- // }
- // }
- // // 清零之后发送消息
- // $tcpArr = [];
- // $tcpArr['type'] = "clearAllCharm";
- // $tcpArr['data'] = [
- // 'clear' => 1
- // ];
- // $tcpJson = json_encode($tcpArr);
- // Gateway::sendToAll($tcpJson);
- }
- // 注意除了不支持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);
- }
|