mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
TEngine 6
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.Networking.PlayerConnection;
|
||||
using UnityEngine;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
internal class RemotePlayerConnection
|
||||
{
|
||||
private static RemotePlayerConnection _instance;
|
||||
public static RemotePlayerConnection Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
_instance = new RemotePlayerConnection();
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly Dictionary<Guid, UnityAction<MessageEventArgs>> _messageCallbacks = new Dictionary<Guid, UnityAction<MessageEventArgs>>();
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_messageCallbacks.Clear();
|
||||
}
|
||||
public void Register(Guid messageID, UnityAction<MessageEventArgs> callback)
|
||||
{
|
||||
if (messageID == Guid.Empty)
|
||||
throw new ArgumentException("messageID is empty !");
|
||||
|
||||
if (_messageCallbacks.ContainsKey(messageID) == false)
|
||||
_messageCallbacks.Add(messageID, callback);
|
||||
}
|
||||
public void Unregister(Guid messageID)
|
||||
{
|
||||
if (_messageCallbacks.ContainsKey(messageID))
|
||||
_messageCallbacks.Remove(messageID);
|
||||
}
|
||||
public void Send(Guid messageID, byte[] data)
|
||||
{
|
||||
if (messageID == Guid.Empty)
|
||||
throw new ArgumentException("messageID is empty !");
|
||||
|
||||
// 接收对方的消息
|
||||
RemoteEditorConnection.Instance.HandlePlayerMessage(messageID, data);
|
||||
}
|
||||
|
||||
internal void HandleEditorMessage(Guid messageID, byte[] data)
|
||||
{
|
||||
if (_messageCallbacks.TryGetValue(messageID, out UnityAction<MessageEventArgs> value))
|
||||
{
|
||||
var args = new MessageEventArgs();
|
||||
args.playerId = 0;
|
||||
args.data = data;
|
||||
value?.Invoke(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user