feat(cordis): 接入上下文组合与模组事务热替换
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
using System;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using ShrinkNetwork;
|
||||
using UnityEngine;
|
||||
|
||||
public enum ShrinkNetworkExampleTransportKind
|
||||
{
|
||||
Tcp = 0,
|
||||
Kcp = 1
|
||||
}
|
||||
|
||||
[AddComponentMenu("ShrinkSDK/Shrink Network Ping Client Example")]
|
||||
[DisallowMultipleComponent]
|
||||
public sealed class ShrinkNetworkPingClientExample : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private ShrinkNetworkExampleTransportKind transportKind = ShrinkNetworkExampleTransportKind.Tcp;
|
||||
[SerializeField] private string host = "127.0.0.1";
|
||||
[SerializeField] private int port = 17001;
|
||||
[SerializeField] private bool connectOnStart = true;
|
||||
[SerializeField] private bool sendPingOnConnected = true;
|
||||
[SerializeField] private string pingText = "unity";
|
||||
|
||||
private ShrinkNetworkService _service;
|
||||
private IShrinkNetworkTransport _transport;
|
||||
private ShrinkNetworkSession _session;
|
||||
private bool _isSending;
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
host = "127.0.0.1";
|
||||
port = transportKind == ShrinkNetworkExampleTransportKind.Kcp ? 17002 : 17001;
|
||||
connectOnStart = true;
|
||||
sendPingOnConnected = true;
|
||||
pingText = "unity";
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (connectOnStart)
|
||||
Connect();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
Disconnect();
|
||||
}
|
||||
|
||||
private void OnValidate()
|
||||
{
|
||||
if (port < 1)
|
||||
port = 1;
|
||||
else if (port > 65535)
|
||||
port = 65535;
|
||||
}
|
||||
|
||||
[ContextMenu("Connect")]
|
||||
public void Connect()
|
||||
{
|
||||
Disconnect();
|
||||
|
||||
_service = new ShrinkNetworkService(
|
||||
new ShrinkMessagePackNetworkSerializer(),
|
||||
new ShrinkNetworkMessageRegistry(),
|
||||
new ShrinkNetworkRouter());
|
||||
_service.AutoRegisterAttributedMessages();
|
||||
_service.OnSessionConnected += HandleSessionConnected;
|
||||
_service.OnSessionDisconnected += HandleSessionDisconnected;
|
||||
|
||||
_transport = CreateTransport();
|
||||
_service.BindTransport(_transport);
|
||||
|
||||
Debug.Log($"[ShrinkNetworkExample] Connecting to {transportKind} {host}:{port}");
|
||||
}
|
||||
|
||||
[ContextMenu("Disconnect")]
|
||||
public void Disconnect()
|
||||
{
|
||||
if (_service != null)
|
||||
{
|
||||
_service.OnSessionConnected -= HandleSessionConnected;
|
||||
_service.OnSessionDisconnected -= HandleSessionDisconnected;
|
||||
}
|
||||
|
||||
_transport?.Stop();
|
||||
_transport = null;
|
||||
_service = null;
|
||||
_session = null;
|
||||
_isSending = false;
|
||||
}
|
||||
|
||||
[ContextMenu("Send Ping")]
|
||||
public void SendPing()
|
||||
{
|
||||
SendPingAsync().Forget();
|
||||
}
|
||||
|
||||
public async UniTask SendPingAsync()
|
||||
{
|
||||
if (_session == null)
|
||||
{
|
||||
Debug.LogWarning("[ShrinkNetworkExample] No active session. Connect first.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (_isSending)
|
||||
return;
|
||||
|
||||
_isSending = true;
|
||||
try
|
||||
{
|
||||
var response = await _session.RpcAsync<PingRequest, PingResponse>(new PingRequest
|
||||
{
|
||||
Text = pingText
|
||||
});
|
||||
Debug.Log($"[ShrinkNetworkExample] Ping reply: {response.Reply}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogException(ex, this);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isSending = false;
|
||||
}
|
||||
}
|
||||
|
||||
private IShrinkNetworkTransport CreateTransport()
|
||||
{
|
||||
return transportKind == ShrinkNetworkExampleTransportKind.Kcp
|
||||
? new ShrinkKcpClientTransport(host, port, new ShrinkKcpTransportOptions
|
||||
{
|
||||
Interval = 10,
|
||||
UpdateIntervalMs = 10,
|
||||
IdleTimeoutMs = 20000
|
||||
})
|
||||
: new ShrinkTcpClientTransport(host, port);
|
||||
}
|
||||
|
||||
private void HandleSessionConnected(ShrinkNetworkSession session)
|
||||
{
|
||||
_session = session;
|
||||
Debug.Log($"[ShrinkNetworkExample] Connected: {session.RemoteAddress}");
|
||||
|
||||
if (sendPingOnConnected)
|
||||
SendPingAsync().Forget();
|
||||
}
|
||||
|
||||
private void HandleSessionDisconnected(ShrinkNetworkSession session)
|
||||
{
|
||||
if (_session == session)
|
||||
_session = null;
|
||||
|
||||
Debug.Log($"[ShrinkNetworkExample] Disconnected: {session.RemoteAddress}");
|
||||
}
|
||||
}
|
||||
|
||||
[ShrinkNetworkMessage(1001, "auth/ping")]
|
||||
public sealed class PingRequest : IShrinkNetworkRequest
|
||||
{
|
||||
public string Text { get; set; } = "";
|
||||
}
|
||||
|
||||
[ShrinkNetworkMessage(1002, "auth/ping_response")]
|
||||
public sealed class PingResponse : ShrinkRpcResponseBase
|
||||
{
|
||||
public string Reply { get; set; } = "";
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 807c6f7bb5cd6bb44ab2ee58fc9796cb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user