【m】优化socket代码

This commit is contained in:
2026-04-29 15:13:24 +08:00
parent 753878bdbb
commit 231021d318
127 changed files with 807 additions and 44137 deletions

View File

@@ -90,6 +90,26 @@ namespace Unity.RenderStreaming
/// </summary>
public event Action<string, RTCDataChannel> onAddChannel;
/// <summary>
/// 参与者加入事件 (connectionId, participantId)
/// </summary>
public event Action<string, string> onParticipantJoined;
/// <summary>
/// 参与者离开事件 (connectionId, participantId)
/// </summary>
public event Action<string, string> onParticipantLeft;
/// <summary>
/// 呼叫请求事件 (connectionId, data)
/// </summary>
public event Action<string, string> onCallRequest;
/// <summary>
/// 自定义消息事件 (connectionId, participantId, message)
/// </summary>
public event Action<string, string, string> onMessage;
private bool _disposed;
private readonly ISignaling _signaling;
private RTCConfiguration _config;
@@ -122,6 +142,10 @@ namespace Unity.RenderStreaming
_signaling.OnOffer += OnOffer;
_signaling.OnAnswer += OnAnswer;
_signaling.OnIceCandidate += OnIceCandidate;
_signaling.OnParticipantJoined += OnParticipantJoinedHandler;
_signaling.OnParticipantLeft += OnParticipantLeftHandler;
_signaling.OnCallRequest += OnCallRequestHandler;
_signaling.OnMessage += OnMessageHandler;
_signaling.Start();
_startCoroutine(WebRTC.WebRTC.Update());
}
@@ -153,6 +177,10 @@ namespace Unity.RenderStreaming
_signaling.OnOffer -= OnOffer;
_signaling.OnAnswer -= OnAnswer;
_signaling.OnIceCandidate -= OnIceCandidate;
_signaling.OnParticipantJoined -= OnParticipantJoinedHandler;
_signaling.OnParticipantLeft -= OnParticipantLeftHandler;
_signaling.OnCallRequest -= OnCallRequestHandler;
_signaling.OnMessage -= OnMessageHandler;
foreach (var pair in _mapConnectionIdAndPeer)
pair.Value.Dispose();
@@ -428,5 +456,30 @@ namespace Unity.RenderStreaming
RTCSessionDescription description = new RTCSessionDescription { type = RTCSdpType.Offer, sdp = e.sdp };
_startCoroutine(pc.OnGotDescription(description, () => onGotOffer?.Invoke(connectionId, e.sdp)));
}
void OnParticipantJoinedHandler(ISignaling signaling, ParticipantEventData e)
{
onParticipantJoined?.Invoke(e.connectionId, e.participantId);
}
void OnParticipantLeftHandler(ISignaling signaling, ParticipantEventData e)
{
// 参与者离开时销毁对应的PeerConnection
if (_mapConnectionIdAndPeer.ContainsKey(e.participantId))
{
DestroyConnection(e.participantId);
}
onParticipantLeft?.Invoke(e.connectionId, e.participantId);
}
void OnCallRequestHandler(ISignaling signaling, CallRequestData e)
{
onCallRequest?.Invoke(e.connectionId, e.data);
}
void OnMessageHandler(ISignaling signaling, OnMessageData e)
{
onMessage?.Invoke(e.connectionId, e.participantId, e.message);
}
}
}