增加自定义不是房间的消息发送
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using UnityEngine;
|
||||
@@ -11,17 +10,14 @@ namespace Unity.RenderStreaming
|
||||
{
|
||||
public class MessageChannel : DataChannelBase
|
||||
{
|
||||
public event Action<string, UserInfo> OnUserInfoMessageReceived;
|
||||
public event Action<string, MediaStateChange> OnMediaStateChangeReceived;
|
||||
private const int MAX_HISTORY = 100;
|
||||
|
||||
[Header("消息记录")] [SerializeField, TextArea(1, 3)]
|
||||
[Header("消息记录")] [SerializeField] [TextArea(1, 3)]
|
||||
private string lastReceivedMessage;
|
||||
|
||||
[SerializeField] private int receivedMessageCount;
|
||||
|
||||
[SerializeField] private List<MessageRecord> messageHistory = new List<MessageRecord>();
|
||||
|
||||
private const int MAX_HISTORY = 100;
|
||||
[SerializeField] private List<MessageRecord> messageHistory = new();
|
||||
|
||||
public IReadOnlyList<MessageRecord> MessageHistory => messageHistory;
|
||||
public int ReceivedMessageCount => receivedMessageCount;
|
||||
@@ -31,18 +27,18 @@ namespace Unity.RenderStreaming
|
||||
label = "on-message";
|
||||
}
|
||||
|
||||
public event Action<string, UserInfo> OnUserInfoMessageReceived;
|
||||
public event Action<string, MediaStateChange> OnMediaStateChangeReceived;
|
||||
|
||||
public override void OnMessage(string message)
|
||||
{
|
||||
try
|
||||
{
|
||||
Debug.Log($"[MessageChannel] Received: {message}");
|
||||
var record = JsonConvert.DeserializeObject<MessageRecord>(message);
|
||||
if (record == null)
|
||||
{
|
||||
record = new MessageRecord { type = "raw", data = message };
|
||||
}
|
||||
if (record == null) record = new MessageRecord { type = "raw", data = message };
|
||||
record.timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
|
||||
ConnectionId=record.connectionId;
|
||||
ConnectionId = record.connectionId;
|
||||
JObject json = null;
|
||||
switch (record.type)
|
||||
{
|
||||
@@ -79,9 +75,9 @@ namespace Unity.RenderStreaming
|
||||
var record = new MessageRecord
|
||||
{
|
||||
type = type,
|
||||
data = JsonConvert.SerializeObject(data)
|
||||
data = data
|
||||
};
|
||||
string json = JsonConvert.SerializeObject(record);
|
||||
var json = JsonConvert.SerializeObject(record);
|
||||
Send(json);
|
||||
|
||||
record.isSent = true;
|
||||
@@ -90,14 +86,6 @@ namespace Unity.RenderStreaming
|
||||
messageHistory.RemoveAt(0);
|
||||
}
|
||||
|
||||
public void SendMessage(string message)
|
||||
{
|
||||
Send(message);
|
||||
var record = new MessageRecord { type = "raw", data = message, isSent = true };
|
||||
messageHistory.Add(record);
|
||||
if (messageHistory.Count > MAX_HISTORY)
|
||||
messageHistory.RemoveAt(0);
|
||||
}
|
||||
|
||||
public void ClearHistory()
|
||||
{
|
||||
@@ -110,11 +98,11 @@ namespace Unity.RenderStreaming
|
||||
public class MessageRecord
|
||||
{
|
||||
public string type;
|
||||
public object data;
|
||||
public bool isSent;
|
||||
public string timestamp = DateTime.Now.ToString();
|
||||
public string connectionId;
|
||||
public string participantId;
|
||||
public object data;
|
||||
}
|
||||
}
|
||||
}
|
||||
85
Assets/Script/WebRtc/SignalingMessageHelper.cs
Normal file
85
Assets/Script/WebRtc/SignalingMessageHelper.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
// Assets/Script/WebRtc/SignalingMessageHelper.cs
|
||||
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Unity.RenderStreaming
|
||||
{
|
||||
public static class SignalingMessageHelper
|
||||
{
|
||||
private static object _webSocketSignaling;
|
||||
private static MethodInfo _sendRawMessageMethod;
|
||||
|
||||
static SignalingMessageHelper()
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
private static void Initialize()
|
||||
{
|
||||
try
|
||||
{
|
||||
var signalingManager = GameObject.FindObjectOfType<SignalingManager>();
|
||||
if (signalingManager == null)
|
||||
{
|
||||
Debug.LogWarning("SignalingMessageHelper: 找不到 SignalingManager");
|
||||
return;
|
||||
}
|
||||
|
||||
var managerInternalField = typeof(SignalingManager).GetField("m_instance",
|
||||
BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
var managerInternal = managerInternalField.GetValue(signalingManager);
|
||||
|
||||
if (managerInternal == null)
|
||||
{
|
||||
Debug.LogWarning("SignalingMessageHelper: SignalingManager 内部实例为空");
|
||||
return;
|
||||
}
|
||||
|
||||
var signalingField = managerInternal.GetType().GetField("_signaling",
|
||||
BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
_webSocketSignaling = signalingField.GetValue(managerInternal);
|
||||
|
||||
if (_webSocketSignaling == null)
|
||||
{
|
||||
Debug.LogWarning("SignalingMessageHelper: WebSocketSignaling 实例为空");
|
||||
return;
|
||||
}
|
||||
|
||||
_sendRawMessageMethod = _webSocketSignaling.GetType().GetMethod("SendMessage");
|
||||
if (_sendRawMessageMethod == null) Debug.LogWarning("SignalingMessageHelper: 找不到 SendRawMessage 方法");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"SignalingMessageHelper 初始化失败: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public static bool SendMessage(string message)
|
||||
{
|
||||
if (_webSocketSignaling == null || _sendRawMessageMethod == null)
|
||||
{
|
||||
Debug.LogError("SignalingMessageHelper 未正确初始化,请确保 WebSocketSignaling 已启动");
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_sendRawMessageMethod.Invoke(_webSocketSignaling, new[] { message });
|
||||
Debug.Log("消息已发送到服务端");
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogError($"发送消息失败: {e.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsReady()
|
||||
{
|
||||
return _webSocketSignaling != null && _sendRawMessageMethod != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Script/WebRtc/SignalingMessageHelper.cs.meta
Normal file
11
Assets/Script/WebRtc/SignalingMessageHelper.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cee7bdd2c1f4deb4f9abcfba0ec09ddd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user