mirror of
https://github.com/RRQM/TouchSocket.git
synced 2025-12-19 18:06:45 +08:00
修改:unity示例demo
This commit is contained in:
Binary file not shown.
@@ -3,96 +3,94 @@
|
||||
*/
|
||||
#pragma warning disable
|
||||
using System;
|
||||
using TouchSocket.Core;
|
||||
using TouchSocket.Sockets;
|
||||
using TouchSocket.Rpc;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TouchSocket.Core;
|
||||
using TouchSocket.Rpc;
|
||||
using TouchSocket.Sockets;
|
||||
namespace UnityRpcProxy_Json_HttpDmtp_2D
|
||||
{
|
||||
public interface IUnityRpcStore : TouchSocket.Rpc.IRemoteServer
|
||||
{
|
||||
///<summary>
|
||||
///单位移动
|
||||
///</summary>
|
||||
/// <exception cref="System.TimeoutException">调用超时</exception>
|
||||
/// <exception cref="TouchSocket.Rpc.RpcInvokeException">Rpc调用异常</exception>
|
||||
/// <exception cref="System.Exception">其他异常</exception>
|
||||
void JsonRpc_UnitMovement(System.Numerics.Vector3 vector3, IInvokeOption invokeOption = default);
|
||||
///<summary>
|
||||
///单位移动
|
||||
///</summary>
|
||||
/// <exception cref="System.TimeoutException">调用超时</exception>
|
||||
/// <exception cref="TouchSocket.Rpc.RpcInvokeException">Rpc调用异常</exception>
|
||||
/// <exception cref="System.Exception">其他异常</exception>
|
||||
Task JsonRpc_UnitMovementAsync(System.Numerics.Vector3 vector3, IInvokeOption invokeOption = default);
|
||||
public interface IUnityRpcStore:TouchSocket.Rpc.IRemoteServer
|
||||
{
|
||||
///<summary>
|
||||
///单位移动
|
||||
///</summary>
|
||||
/// <exception cref="System.TimeoutException">调用超时</exception>
|
||||
/// <exception cref="TouchSocket.Rpc.RpcInvokeException">Rpc调用异常</exception>
|
||||
/// <exception cref="System.Exception">其他异常</exception>
|
||||
void JsonRpc_UnitMovement(System.Numerics.Vector3 vector3,IInvokeOption invokeOption = default);
|
||||
///<summary>
|
||||
///单位移动
|
||||
///</summary>
|
||||
/// <exception cref="System.TimeoutException">调用超时</exception>
|
||||
/// <exception cref="TouchSocket.Rpc.RpcInvokeException">Rpc调用异常</exception>
|
||||
/// <exception cref="System.Exception">其他异常</exception>
|
||||
Task JsonRpc_UnitMovementAsync(System.Numerics.Vector3 vector3,IInvokeOption invokeOption = default);
|
||||
|
||||
}
|
||||
public class UnityRpcStore : IUnityRpcStore
|
||||
{
|
||||
public UnityRpcStore(IRpcClient client)
|
||||
{
|
||||
this.Client = client;
|
||||
}
|
||||
public IRpcClient Client { get; private set; }
|
||||
///<summary>
|
||||
///单位移动
|
||||
///</summary>
|
||||
/// <exception cref="System.TimeoutException">调用超时</exception>
|
||||
/// <exception cref="TouchSocket.Rpc.RpcInvokeException">Rpc调用异常</exception>
|
||||
/// <exception cref="System.Exception">其他异常</exception>
|
||||
public void JsonRpc_UnitMovement(System.Numerics.Vector3 vector3, IInvokeOption invokeOption = default)
|
||||
{
|
||||
if (this.Client == null)
|
||||
{
|
||||
throw new RpcException("IRpcClient为空,请先初始化或者进行赋值");
|
||||
}
|
||||
object[] @_parameters = new object[] { vector3 };
|
||||
this.Client.Invoke("JsonRpc_UnitMovement", null, invokeOption, @_parameters);
|
||||
|
||||
}
|
||||
///<summary>
|
||||
///单位移动
|
||||
///</summary>
|
||||
public Task JsonRpc_UnitMovementAsync(System.Numerics.Vector3 vector3, IInvokeOption invokeOption = default)
|
||||
{
|
||||
if (this.Client == null)
|
||||
{
|
||||
throw new RpcException("IRpcClient为空,请先初始化或者进行赋值");
|
||||
}
|
||||
object[] parameters = new object[] { vector3 };
|
||||
return this.Client.InvokeAsync("JsonRpc_UnitMovement", null, invokeOption, parameters);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
public static class UnityRpcStoreExtensions
|
||||
{
|
||||
///<summary>
|
||||
///单位移动
|
||||
///</summary>
|
||||
/// <exception cref="System.TimeoutException">调用超时</exception>
|
||||
/// <exception cref="TouchSocket.Rpc.RpcInvokeException">Rpc调用异常</exception>
|
||||
/// <exception cref="System.Exception">其他异常</exception>
|
||||
public static void JsonRpc_UnitMovement<TClient>(this TClient client, System.Numerics.Vector3 vector3, IInvokeOption invokeOption = default) where TClient :
|
||||
TouchSocket.JsonRpc.IJsonRpcClient
|
||||
{
|
||||
object[] @_parameters = new object[] { vector3 };
|
||||
client.Invoke("JsonRpc_UnitMovement", null, invokeOption, @_parameters);
|
||||
|
||||
}
|
||||
///<summary>
|
||||
///单位移动
|
||||
///</summary>
|
||||
public static Task JsonRpc_UnitMovementAsync<TClient>(this TClient client, System.Numerics.Vector3 vector3, IInvokeOption invokeOption = default) where TClient :
|
||||
TouchSocket.JsonRpc.IJsonRpcClient
|
||||
{
|
||||
object[] parameters = new object[] { vector3 };
|
||||
return client.InvokeAsync("JsonRpc_UnitMovement", null, invokeOption, parameters);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
public class UnityRpcStore :IUnityRpcStore
|
||||
{
|
||||
public UnityRpcStore(IRpcClient client)
|
||||
{
|
||||
this.Client=client;
|
||||
}
|
||||
public IRpcClient Client{get;private set; }
|
||||
///<summary>
|
||||
///单位移动
|
||||
///</summary>
|
||||
/// <exception cref="System.TimeoutException">调用超时</exception>
|
||||
/// <exception cref="TouchSocket.Rpc.RpcInvokeException">Rpc调用异常</exception>
|
||||
/// <exception cref="System.Exception">其他异常</exception>
|
||||
public void JsonRpc_UnitMovement(System.Numerics.Vector3 vector3,IInvokeOption invokeOption = default)
|
||||
{
|
||||
if(this.Client==null)
|
||||
{
|
||||
throw new RpcException("IRpcClient为空,请先初始化或者进行赋值");
|
||||
}
|
||||
object[] @_parameters = new object[]{vector3};
|
||||
this.Client.Invoke("JsonRpc_UnitMovement",null,invokeOption, @_parameters);
|
||||
|
||||
}
|
||||
///<summary>
|
||||
///单位移动
|
||||
///</summary>
|
||||
public Task JsonRpc_UnitMovementAsync(System.Numerics.Vector3 vector3,IInvokeOption invokeOption = default)
|
||||
{
|
||||
if(this.Client==null)
|
||||
{
|
||||
throw new RpcException("IRpcClient为空,请先初始化或者进行赋值");
|
||||
}
|
||||
object[] parameters = new object[]{vector3};
|
||||
return this.Client.InvokeAsync("JsonRpc_UnitMovement",null,invokeOption, parameters);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
public static class UnityRpcStoreExtensions
|
||||
{
|
||||
///<summary>
|
||||
///单位移动
|
||||
///</summary>
|
||||
/// <exception cref="System.TimeoutException">调用超时</exception>
|
||||
/// <exception cref="TouchSocket.Rpc.RpcInvokeException">Rpc调用异常</exception>
|
||||
/// <exception cref="System.Exception">其他异常</exception>
|
||||
public static void JsonRpc_UnitMovement<TClient>(this TClient client,System.Numerics.Vector3 vector3,IInvokeOption invokeOption = default) where TClient:
|
||||
TouchSocket.JsonRpc.IJsonRpcClient{
|
||||
object[] @_parameters = new object[]{vector3};
|
||||
client.Invoke("JsonRpc_UnitMovement",null,invokeOption, @_parameters);
|
||||
|
||||
}
|
||||
///<summary>
|
||||
///单位移动
|
||||
///</summary>
|
||||
public static Task JsonRpc_UnitMovementAsync<TClient>(this TClient client,System.Numerics.Vector3 vector3,IInvokeOption invokeOption = default) where TClient:
|
||||
TouchSocket.JsonRpc.IJsonRpcClient{
|
||||
object[] parameters = new object[]{vector3};
|
||||
return client.InvokeAsync("JsonRpc_UnitMovement",null,invokeOption, parameters);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace UnityServerConsoleApp_2D.TouchServer;
|
||||
/// </summary>
|
||||
public class Touch_JsonWebSocket_2D : BaseTouchServer
|
||||
{
|
||||
private readonly JsonHttpService dmtpService = new JsonHttpService();
|
||||
private readonly JsonHttpService m_dmtpService = new JsonHttpService();
|
||||
public async Task StartService(int port)
|
||||
{
|
||||
var config = new TouchSocketConfig()//配置
|
||||
@@ -48,11 +48,11 @@ public class Touch_JsonWebSocket_2D : BaseTouchServer
|
||||
|
||||
});
|
||||
|
||||
await this.dmtpService.SetupAsync(config);
|
||||
await this.dmtpService.StartAsync();
|
||||
await this.m_dmtpService.SetupAsync(config);
|
||||
await this.m_dmtpService.StartAsync();
|
||||
|
||||
|
||||
this.dmtpService.Logger.Info($"TCP_JsonWebSocket已启动,监听端口:{port}");
|
||||
this.m_dmtpService.Logger.Info($"TCP_JsonWebSocket已启动,监听端口:{port}");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
@@ -60,19 +60,19 @@ public class Touch_JsonWebSocket_2D : BaseTouchServer
|
||||
/// </summary>
|
||||
internal class Touch_JsonWebSocket_Log_Plguin : PluginBase, IWebSocketHandshakedPlugin, IWebSocketClosedPlugin
|
||||
{
|
||||
private readonly ILog Log;
|
||||
private readonly ILog m_log;
|
||||
public Touch_JsonWebSocket_Log_Plguin(ILog Log)
|
||||
{
|
||||
this.Log = Log;
|
||||
this.m_log = Log;
|
||||
|
||||
}
|
||||
private static int ID;
|
||||
private static int s_iD;
|
||||
public async Task OnWebSocketClosed(IWebSocket webSocket, ClosedEventArgs e)
|
||||
{
|
||||
webSocket.Client.Logger.Info($"TCP_WebSocket:客户端{webSocket.Client.IP}已断开");
|
||||
if (webSocket.Client is JsonHttpSessionClient client)
|
||||
{
|
||||
this.Log.Info("在线用户" + client.Service.Count);
|
||||
this.m_log.Info("在线用户" + client.Service.Count);
|
||||
|
||||
foreach (JsonHttpSessionClient clientItem in client.Service.GetClients())
|
||||
{
|
||||
@@ -92,8 +92,8 @@ internal class Touch_JsonWebSocket_Log_Plguin : PluginBase, IWebSocketHandshaked
|
||||
|
||||
if (webSocket.Client is JsonHttpSessionClient client)
|
||||
{
|
||||
this.Log.Info($"TCP_WebSocket:客户端{webSocket.Client.IP}已连接");
|
||||
client.ID = ++ID;
|
||||
this.m_log.Info($"TCP_WebSocket:客户端{webSocket.Client.IP}已连接");
|
||||
client.ID = ++s_iD;
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
foreach (JsonHttpSessionClient clientItem in client.Service.GetClients())
|
||||
@@ -108,7 +108,7 @@ internal class Touch_JsonWebSocket_Log_Plguin : PluginBase, IWebSocketHandshaked
|
||||
}
|
||||
await client.GetJsonRpcActionClient().PlayerLoginAsync(client.ID);
|
||||
});
|
||||
this.Log.Info("在线用户" + client.Service.Count);
|
||||
this.m_log.Info("在线用户" + client.Service.Count);
|
||||
}
|
||||
await e.InvokeNext();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.ComponentModel;
|
||||
using System.Numerics;
|
||||
using System.Threading.Tasks;
|
||||
using TouchSocket.Core;
|
||||
using TouchSocket.JsonRpc;
|
||||
using TouchSocket.Rpc;
|
||||
@@ -32,22 +33,22 @@ internal class UnityRpcStore : RpcServer
|
||||
|
||||
[Description("单位移动")]
|
||||
[JsonRpc(MethodInvoke = true, MethodName = "JsonRpc_{0}")]
|
||||
public void UnitMovement(ICallContext callContext, Vector3 vector3)
|
||||
public async Task UnitMovement(ICallContext callContext, Vector3 vector3)
|
||||
{
|
||||
if (callContext.Caller is JsonHttpSessionClient jsonsession)
|
||||
{
|
||||
jsonsession.Postion = vector3;
|
||||
foreach (JsonHttpSessionClient clientItem in jsonsession.Service.GetClients())
|
||||
{
|
||||
|
||||
//通知除开玩家的其他所有客户端
|
||||
if (jsonsession != clientItem)
|
||||
{
|
||||
clientItem.GetJsonRpcActionClient().UpdatePositionAsync(jsonsession.ID, jsonsession.Postion, ToTimestamp(DateTime.Now));
|
||||
await clientItem.GetJsonRpcActionClient().UpdatePositionAsync(jsonsession.ID, jsonsession.Postion, ToTimestamp(DateTime.Now));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
m_logger.Info($"玩家{jsonsession.ID}移动到{vector3}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user