| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?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();
- if ($adminList && isset($adminList['data'])) {
- foreach ($adminList['data'] as $admin) {
- $fd = $redisClient->findFd((int)$admin['id']);
- if ($fd && $server->isEstablished((int)$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);
- 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;
- }
- }
- /**
- * 单聊
- */
- public function sendSingleChat($data){
- $userInfo = $this->userServiceClient->getImContact([
- 'user_id' => $data['receiver_id'],
- 'friend_id' => $data['user_id'],
- ]);
- // var_dump("用户信息:",$userInfo);
- $server = \Hyperf\Context\ApplicationContext::getContainer()->get(\Swoole\Server::class);
- $redisClient = new RedisService();
- $message = [
- 'talk_type' => 1,
- 'title' => $userInfo['data']['remark'],
- 'content' => $data['content'],
- 'messageType' => 1,
- 'receiver_id'=> $data['user_id'],
- // 'user_id' => $user['user_id'] ?? '',
- 'time' => microtime(),
- ];
- $fd = $redisClient->findFd((int)$data['receiver_id']);
- if ($fd && $server->isEstablished((int)$fd)) {
- $server->push((int)$fd, json_encode($message));
- }
- return true;
- }
- /**
- * 群聊
- */
- public function sendGroupChat($data){
- $userList = $this->userServiceClient->getImGroupMember(['user_id' => $data['user_id'],'group_id' => $data['receiver_id']]);
- // 获取 Swoole WebSocket Server 实例
- $server = \Hyperf\Context\ApplicationContext::getContainer()->get(\Swoole\Server::class);
- $redisClient = new RedisService();
- if($userList && isset($userList['data'])){
- foreach($userList['data'] as $user){
- $message = [
- 'talk_type' => 2,
- 'title' => $user['group_name'],
- 'content' => $data['content'],
- 'messageType' => 1,
- 'receiver_id'=> $data['receiver_id'],
- // 'user_id' => $user['user_id'] ?? '',
- 'time' => microtime(),
- ];
- $fd = $redisClient->findFd((int)$user['user_id']);
- if ($fd && $server->isEstablished((int)$fd)) {
- $server->push((int)$fd, json_encode($message));
- }
- }
- }
- }
- }
|