| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- declare(strict_types=1);
- namespace App\Controller;
- /**
- * MessageController
- * @package App\Controller
- */
- use App\JsonRpc\UserServiceInterface;
- use Hyperf\Di\Annotation\Inject;
- use Hyperf\Context\ApplicationContext;
- use Swoole\WebSocket\Server as WebSocketServer;
- use App\Service\RedisService;
- use Hyperf\Server\ServerManager;
- use function Hyperf\Support\call;
- use swoole\Server;
- class MessageController extends AbstractController
- {
- /**
- * @var UserServiceInterface
- */
- #[Inject]
- private $userServiceClient;
- public function sendAdminMessage($data)
- {
- try {
- // 获取管理员列表
- $adminList = $this->userServiceClient->getTypeUserList(['type_id' => 10000]);
- // 获取 Swoole WebSocket Server 实例
- $server = \Hyperf\Context\ApplicationContext::getContainer()->get(\Swoole\Server::class);
- $redisClient = new RedisService();
- var_dump($adminList, '-------------&&&&&&&&&&&&&&&&&&&&&&----');
- if ($adminList && isset($adminList['data'])) {
- foreach ($adminList['data'] as $admin) {
- $fd = $redisClient->findFd((int)$admin['id']);
- if ($fd && $server->isEstablished((int)$fd)) {
- var_dump($fd, '-------------&&&&&&&&&&&&&&&&&&&&&&----');
- $server->push((int)$fd, json_encode($data));
- }
- }
- }
- return true;
- } catch (\Throwable $e) {
- var_dump('发送消息错误: ' . $e->getMessage());
- return false;
- }
- }
- public function sendUserMessage($data){
- try {
- // 获取 Swoole WebSocket Server 实例
- $server = \Hyperf\Context\ApplicationContext::getContainer()->get(\Swoole\Server::class);
- $redisClient = new RedisService();
- $fd = $redisClient->findFd((int)$data['user_id']);
- if ($fd && $server->isEstablished((int)$fd)) {
- $server->push((int)$fd, json_encode($data));
- }
- } catch (\Throwable $e) {
- var_dump('发送消息错误: ' . $e->getMessage());
- return false;
- }
- }
- public function sendOtherUserMessage($data){
- try {
- // 获取 Swoole WebSocket Server 实例
- $server = \Hyperf\Context\ApplicationContext::getContainer()->get(\Swoole\Server::class);
- $redisClient = new RedisService();
- $data['user_id'] = json_decode($data['user_id'],true);
- // var_dump($data['user_id'],"====================%%%%%%%%%%%====");
-
- if(is_array($data['user_id'])){
- foreach($data['user_id'] as $user_id){
- $fd = $redisClient->findFd((int)$user_id);
- if ($fd && $server->isEstablished((int)$fd)) {
- $server->push((int)$fd, json_encode($data));
- }
- }
- }
-
- } catch (\Throwable $e) {
- var_dump('发送消息错误: ' . $e->getMessage());
- return false;
- }
- }
- }
|