Websocket.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Websocket 后台封装类,用于实现token 和 uid 的绑定关系
  4. * 前端连接 websocket 时,可以使用带上 token,workerman根据 token 查询出 uid,以实现 client_id 和 uid 的绑定关系
  5. */
  6. namespace app\common\logic;
  7. use \GatewayClient\Gateway;
  8. use think\Cache;
  9. use think\Config;
  10. class Websocket
  11. {
  12. const TOKEN_LIMIT = 10; // 同一用户 token 上限
  13. private $auth_duration; // 登录token 有效期
  14. private $_redis = null;
  15. public function __construct()
  16. {
  17. $this->auth_duration = Config::get('session')['expire']; //设置成和session相同的过期时间
  18. $this->_redis = Cache::init()->handler(); // redis 句柄
  19. }
  20. /**
  21. * 登录完成后,调用该方法,在 websocket 服务端注册一个有效token,并产生 token 和 uid 的对应关系记录
  22. * @param $token
  23. * @param $adminId
  24. * @return void
  25. */
  26. public function registerToken($token, $adminId)
  27. {
  28. // uid 对应的 token列表, 同一个账号,可能存在多个人同时登录的情况,在目前的业务场景下,并未禁止
  29. // 所以要能够实现多个登录终端的对应
  30. $tokenCacheKey = "ayam:push:uid:{$adminId}:token";
  31. $tokenList = $this->_redis->get($tokenCacheKey);
  32. $tokenList = json_decode($tokenList, true);
  33. empty($tokenList) && $tokenList = array();
  34. $tokenList[$token] = time() + $this->auth_duration;
  35. // 限制 token 的个数,超出预设值,剔除最先登录的
  36. if (count($tokenList) > self::TOKEN_LIMIT) { // 限制 TOKEN 数
  37. $tokenList = array_slice($tokenList, -self::TOKEN_LIMIT);
  38. }
  39. $this->_redis->setex($tokenCacheKey, $this->auth_duration, json_encode($tokenList));
  40. // token 对应的 uid,这样 workerman 那边可以通过 token 查询对应的 uid
  41. $cacheKey = "ayam:push:token:{$token}:uid";
  42. $this->_redis->setex($cacheKey, $this->auth_duration, $adminId);
  43. }
  44. /**
  45. * 重置登录token有效
  46. *
  47. * @param $token string 查询的token
  48. * @param $adminId string 所属用户id
  49. *
  50. * @return void
  51. */
  52. public function resettingToken($token, $adminId)
  53. {
  54. // uid 对应的 token列表, 同一个账号,可能存在多个人同时登录的情况,在目前的业务场景下,并未禁止
  55. // 所以要能够实现多个登录终端的对应
  56. $tokenCacheKey = "ayam:push:uid:{$adminId}:token";
  57. $cacheKey = "ayam:push:token:{$token}:uid";
  58. if (!$this->_redis->exists($cacheKey)) {
  59. $tokenList = $this->_redis->get($tokenCacheKey);
  60. $tokenList = json_decode($tokenList, true);
  61. empty($tokenList) && $tokenList = array();
  62. $tokenList[$token] = time() + $this->auth_duration;
  63. // 限制 token 的个数,超出预设值,剔除最先登录的
  64. if (count($tokenList) > self::TOKEN_LIMIT) { // 限制 TOKEN 数
  65. $tokenList = array_slice($tokenList, -self::TOKEN_LIMIT);
  66. }
  67. $this->_redis->setex($tokenCacheKey, $this->auth_duration, json_encode($tokenList));
  68. // token 对应的 uid,这样 workerman 那边可以通过 token 查询对应的 uid
  69. $this->_redis->setex($cacheKey, $this->auth_duration, $adminId);
  70. }
  71. }
  72. /**
  73. * 注销连接 token
  74. * @param $token
  75. * @param $adminId
  76. * @return void
  77. */
  78. public function destroyToken($token, $adminId)
  79. {
  80. $uidCacheKey = "ayam:push:token:{$token}:uid";
  81. $this->_redis->del($uidCacheKey);
  82. $tokenCacheKey = "ayam:push:uid:{$adminId}:token";
  83. $tokenList = $this->_redis->get($tokenCacheKey);
  84. if (empty($tokenList)) {
  85. $tokenList = array();
  86. } else {
  87. $tokenList = json_decode($tokenList, true);
  88. }
  89. if (isset($tokenList[$token])) { // 只清除指定的 token
  90. unset($tokenList[$token]);
  91. }
  92. // 若该uid还存在token,做更新,否则删除key,减少占用
  93. if (count($tokenList) > 0) {
  94. $this->_redis->set($tokenCacheKey, json_encode($tokenList));
  95. } else {
  96. $this->_redis->del($tokenCacheKey);
  97. }
  98. }
  99. /**
  100. * 发送消息给指定的用户
  101. * @param $uid int
  102. * @param $data string
  103. * @return void
  104. */
  105. public function sayToUid($uid, $data)
  106. {
  107. if (!is_string($data) || empty($data)) {
  108. exception('data 参数需为字符串类型');
  109. }
  110. empty($uid) && exception('请传入 uid参数');
  111. Gateway::$registerAddress = config('WEBSOCKET_REGISTER_ADDR');
  112. // 数据格式示例:{"error_code":0,"data":{"action":"authSuccess","data":{"code":0,"message":""}}}
  113. Gateway::sendToUid($uid, $data);
  114. }
  115. }