123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace App\Service;
- use App\Cache\SocketFdBindUser;
- use App\Cache\SocketUserBindFds;
- /**
- * websocket关系存储
- *
- * @package App\Service
- */
- class RedisService extends RedisInterface
- {
- protected $prefix_fn = 'chat_fn';
- protected $prefix_user = 'chat_user';
- /**
- * 绑定fd和用户关系
- * @param string $fid
- * @param int $userId
- * @param $run_id
- * @return void
- * @throws \RedisException
- */
- public function bind(string $fid,int $userId, $run_id = SERVER_RUN_ID)
- {
- //站点通道+用户
- $this->redis->hSet($run_id,$this->prefix_fn.$fid,$userId);
- //站点用户+通道
- $this->redis->hSet($run_id,$this->prefix_user.$userId,$fid);
- }
- /**
- * 解绑通道和用户关系
- * @param string $fid
- * @param int $userId
- * @param $run_id
- * @return void
- * @throws \RedisException
- */
- public function unbind(string $fid,int $userId, $run_id = SERVER_RUN_ID)
- {
- $this->redis->hDel($run_id,$this->prefix_fn.$fid);
- $this->redis->hDel($run_id,$this->prefix_user.$userId);
- }
- /**
- * 通过FD获取userID
- * @param string $fid
- * @param $run_id
- * @return false|\Redis|string
- * @throws \RedisException
- */
- public function findUser(string $fid, $run_id = SERVER_RUN_ID)
- {
- return $this->redis->hGet($run_id,$this->prefix_fn.$fid);
- }
- /**
- * 通过UserID 获取fd
- * @param int $userId
- * @param $run_id
- * @return false|\Redis|string
- * @throws \RedisException
- */
- public function findFd(int $userId, $run_id = SERVER_RUN_ID)
- {
- return $this->redis->hGet($run_id,$this->prefix_user.$userId);
- }
- }
|