|
@@ -1,5 +1,6 @@
|
|
|
<?php
|
|
|
-declare (strict_types = 1);
|
|
|
+
|
|
|
+declare(strict_types=1);
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
@@ -41,7 +42,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
|
|
|
protected $receiveHandle;
|
|
|
protected $server;
|
|
|
|
|
|
-
|
|
|
+
|
|
|
public function onMessage($server, $frame): void
|
|
|
{
|
|
|
//把数据推给前端
|
|
@@ -67,29 +68,33 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
|
|
|
$myFriendsArr = [];
|
|
|
if ($myFriends) {
|
|
|
$myFriendsArr = json_decode($myFriends);
|
|
|
-
|
|
|
} else {
|
|
|
$myFriends = $this->chatServiceClient->getFriendsList(['user_id' => $userId, 'status' => 2]);
|
|
|
$redisClient->setUserFriends($userId, $myFriends['data']);
|
|
|
$myFriendsArr = $myFriends['data'];
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
//推送给前台
|
|
|
+ if (isset($result['type']) && in_array($result['type'], ['offer', 'answer', 'ice-candidate'])) {
|
|
|
+ // 是视频通话信令,走特殊处理逻辑
|
|
|
+ $this->handleVideoSignaling($server, $frame, $userId, $result);
|
|
|
+ return;
|
|
|
+ }
|
|
|
//组装数据+头像
|
|
|
if ($result['talk_type'] == 1) {
|
|
|
//判断$result['receiver_id']是否是好友
|
|
|
- $myFriendsID = array_column($myFriendsArr, 'friend_id');
|
|
|
- if (!in_array($result['receiver_id'], $myFriendsID)) {
|
|
|
- $myFriends = $this->chatServiceClient->getFriendsList(['user_id' => $userId, 'status' => 2]);
|
|
|
- $redisClient->setUserFriends($userId, $myFriends['data']);
|
|
|
- $myFriendsArrdata = $myFriends['data'];
|
|
|
- $myFriendsArrID = array_column($myFriendsArrdata, 'friend_id');
|
|
|
+ $myFriendsID = array_column($myFriendsArr, 'friend_id');
|
|
|
+ if (!in_array($result['receiver_id'], $myFriendsID)) {
|
|
|
+ $myFriends = $this->chatServiceClient->getFriendsList(['user_id' => $userId, 'status' => 2]);
|
|
|
+ $redisClient->setUserFriends($userId, $myFriends['data']);
|
|
|
+ $myFriendsArrdata = $myFriends['data'];
|
|
|
+ $myFriendsArrID = array_column($myFriendsArrdata, 'friend_id');
|
|
|
if (!in_array($result['receiver_id'], $myFriendsArrID)) {
|
|
|
$result['content'] = '您还不是好友,无法发送消息!';
|
|
|
$server->push((int) $frame->fd, json_encode($result));
|
|
|
return;
|
|
|
}
|
|
|
- }
|
|
|
+ }
|
|
|
//给自己推一条数据
|
|
|
if ($server->isEstablished($frame->fd)) {
|
|
|
$result['is_read'] = 1;
|
|
@@ -163,7 +168,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
|
|
|
} else if ($result['talk_type'] == 2) {
|
|
|
//根据群找到 群用户,群发一遍消息
|
|
|
$groupUserList = $this->chatServiceClient->getGroupMembers(['group_id' => $result['receiver_id']]);
|
|
|
- var_dump($groupUserList['data'],'----------------------------------');
|
|
|
+ var_dump($groupUserList['data'], '----------------------------------');
|
|
|
if ($groupUserList['data']) {
|
|
|
$chatdata = $result;
|
|
|
foreach ($groupUserList['data'] as $val) {
|
|
@@ -231,6 +236,30 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ protected function handleVideoSignaling($server, $frame, $userId, $message)
|
|
|
+ {
|
|
|
+ $redisClient = new RedisService();
|
|
|
+ $receiverId = $message['receiver_id'] ?? null;
|
|
|
+ if (!$receiverId) {
|
|
|
+ $server->push($frame->fd, json_encode(['error' => 'Missing receiver_id']));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 获取接收方 fd
|
|
|
+ $fd = $redisClient->findFd((int)$receiverId);
|
|
|
+
|
|
|
+ // 判断接收者是否在线
|
|
|
+ if ($server->isEstablished((int)$fd)) {
|
|
|
+ // 转发消息给接收方
|
|
|
+ $server->push((int)$fd, json_encode($message));
|
|
|
+ } else {
|
|
|
+ // 可选:缓存或通知对方不在线
|
|
|
+ $server->push($frame->fd, json_encode([
|
|
|
+ 'type' => 'offline',
|
|
|
+ 'receiver_id' => $receiverId,
|
|
|
+ 'content' => '对方不在线'
|
|
|
+ ]));
|
|
|
+ }
|
|
|
+ }
|
|
|
public function retry($message): void
|
|
|
{
|
|
|
$maxRetries = 10; // 最大重试次数
|
|
@@ -278,5 +307,4 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
|
|
|
],
|
|
|
]));
|
|
|
}
|
|
|
-
|
|
|
}
|