Gatewayworker.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use GatewayClient\Gateway;
  5. use think\Request;
  6. use Redis;
  7. //require_once VENDOR_PATH . 'workerman/gatewayclient/Gateway.php';
  8. /**
  9. * gateway绑定接口
  10. */
  11. class Gatewayworker extends Api
  12. {
  13. protected $noNeedLogin = ["userIsOnline","clearCharm","test"];
  14. protected $noNeedRight = ['*'];
  15. public function __construct(Request $request = null) {
  16. parent::__construct($request);
  17. $params_from = $this->request->request('params_from'); // 客户端
  18. if($params_from == "wxmin") {
  19. Gateway::$registerAddress = "127.0.0.1:1239";
  20. } else {
  21. Gateway::$registerAddress = "127.0.0.1:1238";
  22. }
  23. }
  24. public function test() {
  25. $client_id = $this->request->request('client_id'); // 客户端ID
  26. print_r(Gateway::getAllUidCount());exit;
  27. }
  28. /**
  29. * 绑定用户ID
  30. */
  31. public function bind() {
  32. $client_id = $this->request->request('client_id'); // 客户端ID
  33. if(!$client_id) {
  34. $this->error("请传入客户端ID");
  35. }
  36. $user_id = $this->auth->id;
  37. // 先判断当前用户是否在线
  38. // if(Gateway::isUidOnline($user_id)) {
  39. // // 发送一个踢下线的信息
  40. // $tcpArr = [];
  41. // $tcpArr["type"] = "outLogin";
  42. // $tcpArr['data'] = [
  43. // 'user_id' => $user_id
  44. // ];
  45. // $outLoginJson = json_encode($tcpArr);
  46. // Gateway::sendToUid($user_id,$outLoginJson);
  47. // }
  48. Gateway::bindUid($client_id, $user_id);
  49. // 切换用户在线状态
  50. \app\common\model\User::update(["is_online"=>1],["id"=>$user_id]);
  51. $this->success("绑定成功!");
  52. }
  53. //============================workerman 计时器==========================//
  54. /**
  55. * 服务器定时器:判断用户是否在线
  56. */
  57. public function userIsOnline() {
  58. // $userlist = \app\common\model\User::where(["is_online" =>1,"status"=>"normal"])->select();
  59. // if($userlist) {
  60. // foreach($userlist as $k =>$v){
  61. // if(!Gateway::isUidOnline($v["id"])) {
  62. // \app\common\model\User::update(["is_online"=>0],["id"=>$v["id"]]);
  63. // \app\common\model\User::update(["is_live"=>0],["id"=>$v["id"]]);
  64. // }
  65. // }
  66. // }
  67. }
  68. /**
  69. * 所有房间人数redis 魅力值清零
  70. */
  71. public function clearCharm() {
  72. // 获取所有房间ID信息
  73. // $party_id = \app\common\model\Party::where(["status"=>1])->column("id");
  74. // if($party_id) {
  75. // $redis = new Redis();
  76. // $redisconfig = config("redis");
  77. // $redis->connect($redisconfig["host"], $redisconfig["port"]);
  78. /*if ($redisconfig['redis_pwd']) {
  79. $redis->auth($redisconfig['redis_pwd']);
  80. }
  81. if($redisconfig['redis_selectdb'] > 0){
  82. $redis->select($redisconfig['redis_selectdb']);
  83. }*/
  84. // foreach($party_id as $v) {
  85. // $redis->del("hourCharm_".$v);
  86. // }
  87. // }
  88. // // 清零之后发送消息
  89. // $tcpArr = [];
  90. // $tcpArr['type'] = "clearAllCharm";
  91. // $tcpArr['data'] = [
  92. // 'clear' => 1
  93. // ];
  94. // $tcpJson = json_encode($tcpArr);
  95. // Gateway::sendToAll($tcpJson);
  96. }
  97. // 注意除了不支持sendToCurrentClient和closeCurrentClient方法
  98. // Gateway::sendToAll($data);
  99. // Gateway::sendToClient($client_id, $data);
  100. // Gateway::closeClient($client_id);
  101. // Gateway::isOnline($client_id);
  102. // Gateway::bindUid($client_id, $uid);
  103. // Gateway::isUidOnline($uid);
  104. // Gateway::getClientIdByUid($uid);
  105. // Gateway::unbindUid($client_id, $uid);
  106. // Gateway::sendToUid($uid, $data);
  107. // Gateway::joinGroup($client_id, $group);
  108. // Gateway::sendToGroup($group, $data);
  109. // Gateway::leaveGroup($client_id, $group);
  110. // Gateway::getClientCountByGroup($group);
  111. // Gateway::getClientSessionsByGroup($group);
  112. // Gateway::getAllClientCount();
  113. // Gateway::getAllClientSessions();
  114. // Gateway::setSession($client_id, $session);
  115. // Gateway::updateSession($client_id, $session);
  116. // Gateway::getSession($client_id);
  117. }