Правильная обработка DisconnectReason при сборсах и обрывах соединения

This commit is contained in:
RoyceDa
2026-03-18 19:34:48 +02:00
parent 6166211c60
commit 163d66d0b0
8 changed files with 114 additions and 7 deletions

View File

@@ -10,10 +10,14 @@ import java.util.concurrent.TimeUnit;
import im.rosetta.client.ClientManager;
import im.rosetta.logger.Logger;
import im.rosetta.logger.enums.Color;
import im.rosetta.packet.Packet26SignalPeer;
import im.rosetta.packet.Packet27WebRTC;
import im.rosetta.packet.runtime.NetworkSignalType;
import im.rosetta.packet.runtime.NetworkWebRTCType;
import io.g365sfu.Room;
import io.g365sfu.SFU;
import io.g365sfu.net.DisconnectReason;
import io.g365sfu.net.DisconnectedPeer;
import io.g365sfu.webrtc.ICECandidate;
import io.g365sfu.webrtc.RTCIceServer;
import io.g365sfu.webrtc.SDPAnswer;
@@ -113,6 +117,14 @@ public class ForwardUnitService {
e.printStackTrace();
}
});
connection.setPeerDisconnectedConsumer(arg0 -> {
try{
onPeerDisconnected(arg0);
}catch(ProtocolException e){
this.logger.error(Color.RED + "Failed to handle peer disconnection from SFU server: " + address + ", error: " + e.getMessage());
e.printStackTrace();
}
});
this.sfuConnections.add(connection);
this.logger.info(Color.GREEN + "Successfully connected to SFU server: " + address);
} catch (Exception e) {
@@ -121,6 +133,35 @@ public class ForwardUnitService {
}
}
public void onPeerDisconnected(DisconnectedPeer disconnectedPeer) throws ProtocolException {
Room room = disconnectedPeer.getRoom();
if(disconnectedPeer.getReason() != DisconnectReason.FAILED){
/**
* Если у нас произошло штатное отключение, а не в результате обрыва связи - то не нужно отправлять
* оппонентам пакеты о том, что участник отключился в результате обрыва связи.
*/
return;
}
for(String peerId : room.getParticipants()) {
/**
* Уведомляем все пиры, что соединение с пиром было потеряно
*/
if(room.getParticipants().size() == 1) {
/**
* Звонок был завершен, так как в комнате остался только один участник, который не может продолжать звонок в одиночку.
*/
Packet26SignalPeer packet = new Packet26SignalPeer();
packet.setSignalType(NetworkSignalType.END_CALL_BECAUSE_PEER_DISCONNECTED);
this.clientManager.sendPacketToAuthorizedPK(peerId, packet);
}
}
}
/**
* Выполняется когда сервер SFU отправляет SDP answer для одного из участников комнаты.
* @param sdpAnswer объект SDPAnswer, который содержит информацию о комнате, участнике и самом answer,
* @throws ProtocolException
*/
public void onSdpAnswer(SDPAnswer sdpAnswer) throws ProtocolException {
String participantId = sdpAnswer.getParticipantId();
Packet27WebRTC packet = new Packet27WebRTC();