MessageController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. /**
  5. * MessageController
  6. * @package App\Controller
  7. */
  8. use App\JsonRpc\UserServiceInterface;
  9. use Hyperf\Di\Annotation\Inject;
  10. use Hyperf\Context\ApplicationContext;
  11. use Swoole\WebSocket\Server as WebSocketServer;
  12. use App\Service\RedisService;
  13. use Hyperf\Server\ServerManager;
  14. use function Hyperf\Support\call;
  15. use swoole\Server;
  16. class MessageController extends AbstractController
  17. {
  18. /**
  19. * @var UserServiceInterface
  20. */
  21. #[Inject]
  22. private $userServiceClient;
  23. public function sendAdminMessage($data)
  24. {
  25. try {
  26. // 获取管理员列表
  27. $adminList = $this->userServiceClient->getTypeUserList(['type_id' => 10000]);
  28. // 获取 Swoole WebSocket Server 实例
  29. $server = \Hyperf\Context\ApplicationContext::getContainer()->get(\Swoole\Server::class);
  30. $redisClient = new RedisService();
  31. if ($adminList && isset($adminList['data'])) {
  32. foreach ($adminList['data'] as $admin) {
  33. $fd = $redisClient->findFd((int)$admin['id']);
  34. if ($fd && $server->isEstablished((int)$fd)) {
  35. $server->push((int)$fd, json_encode($data));
  36. }
  37. }
  38. }
  39. return true;
  40. } catch (\Throwable $e) {
  41. var_dump('发送消息错误: ' . $e->getMessage());
  42. return false;
  43. }
  44. }
  45. public function sendUserMessage($data){
  46. try {
  47. // 获取 Swoole WebSocket Server 实例
  48. $server = \Hyperf\Context\ApplicationContext::getContainer()->get(\Swoole\Server::class);
  49. $redisClient = new RedisService();
  50. $fd = $redisClient->findFd((int)$data['user_id']);
  51. if ($fd && $server->isEstablished((int)$fd)) {
  52. $server->push((int)$fd, json_encode($data));
  53. }
  54. } catch (\Throwable $e) {
  55. var_dump('发送消息错误: ' . $e->getMessage());
  56. return false;
  57. }
  58. }
  59. public function sendOtherUserMessage($data){
  60. try {
  61. // 获取 Swoole WebSocket Server 实例
  62. $server = \Hyperf\Context\ApplicationContext::getContainer()->get(\Swoole\Server::class);
  63. $redisClient = new RedisService();
  64. $data['user_id'] = json_decode($data['user_id'],true);
  65. if(is_array($data['user_id'])){
  66. foreach($data['user_id'] as $user_id){
  67. $fd = $redisClient->findFd((int)$user_id);
  68. if ($fd && $server->isEstablished((int)$fd)) {
  69. $server->push((int)$fd, json_encode($data));
  70. }
  71. }
  72. }
  73. } catch (\Throwable $e) {
  74. var_dump('发送消息错误: ' . $e->getMessage());
  75. return false;
  76. }
  77. }
  78. /**
  79. * 单聊
  80. */
  81. public function sendSingleChat($data){
  82. $userInfo = $this->userServiceClient->getImContact([
  83. 'user_id' => $data['receiver_id'],
  84. 'friend_id' => $data['user_id'],
  85. ]);
  86. // var_dump("用户信息:",$userInfo);
  87. $server = \Hyperf\Context\ApplicationContext::getContainer()->get(\Swoole\Server::class);
  88. $redisClient = new RedisService();
  89. $message = [
  90. 'talk_type' => 1,
  91. 'title' => $userInfo['data']['remark'],
  92. 'content' => $data['content'],
  93. 'messageType' => 1,
  94. 'receiver_id'=> $data['user_id'],
  95. // 'user_id' => $user['user_id'] ?? '',
  96. 'time' => microtime(),
  97. ];
  98. $fd = $redisClient->findFd((int)$data['receiver_id']);
  99. if ($fd && $server->isEstablished((int)$fd)) {
  100. $server->push((int)$fd, json_encode($message));
  101. }
  102. return true;
  103. }
  104. /**
  105. * 群聊
  106. */
  107. public function sendGroupChat($data){
  108. $userList = $this->userServiceClient->getImGroupMember(['user_id' => $data['user_id'],'group_id' => $data['receiver_id']]);
  109. // 获取 Swoole WebSocket Server 实例
  110. $server = \Hyperf\Context\ApplicationContext::getContainer()->get(\Swoole\Server::class);
  111. $redisClient = new RedisService();
  112. if($userList && isset($userList['data'])){
  113. foreach($userList['data'] as $user){
  114. $message = [
  115. 'talk_type' => 2,
  116. 'title' => $user['group_name'],
  117. 'content' => $data['content'],
  118. 'messageType' => 1,
  119. 'receiver_id'=> $data['receiver_id'],
  120. // 'user_id' => $user['user_id'] ?? '',
  121. 'time' => microtime(),
  122. ];
  123. $fd = $redisClient->findFd((int)$user['user_id']);
  124. if ($fd && $server->isEstablished((int)$fd)) {
  125. $server->push((int)$fd, json_encode($message));
  126. }
  127. }
  128. }
  129. }
  130. }