mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-26 19:34:47 +00:00
91 lines
2.6 KiB
C#
91 lines
2.6 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Net.Sockets;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Cysharp.Threading.Tasks;
|
|||
|
using DotRecast.Core.Collections;
|
|||
|
using JNGame.Network;
|
|||
|
using Plugins.JNGame.Network;
|
|||
|
using Plugins.JNGame.Network.Action;
|
|||
|
|
|||
|
namespace AppGame.Systems
|
|||
|
{
|
|||
|
public class JNGServer : JNTCPServer
|
|||
|
{
|
|||
|
|
|||
|
private int _index = 1;
|
|||
|
|
|||
|
private bool isInit = false;
|
|||
|
|
|||
|
//客户端绑定的Id
|
|||
|
private Dictionary<int, int> ids = new();
|
|||
|
|
|||
|
public override async Task OnInit()
|
|||
|
{
|
|||
|
if (isInit) return;
|
|||
|
isInit = true;
|
|||
|
//监听服务端事件
|
|||
|
AddListener((int)NActionEnum.NSyncFrameInput,OnNSyncFrameInput);
|
|||
|
AddListener((int)NActionEnum.NSyncTileInput,OnNSyncTileInput);
|
|||
|
AddListener((int)GActionEnum.BindClientID,OnBindClientID);
|
|||
|
|
|||
|
//连接
|
|||
|
await base.OnInit();
|
|||
|
}
|
|||
|
|
|||
|
public override void OnClose()
|
|||
|
{
|
|||
|
isInit = false;
|
|||
|
base.OnClose();
|
|||
|
}
|
|||
|
|
|||
|
protected override async UniTask<int> GetPort()
|
|||
|
{
|
|||
|
return (await App.GAPI.NSyncTilePort).data;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 绑定客户端Id
|
|||
|
/// </summary>
|
|||
|
/// <param name="obj"></param>
|
|||
|
/// <exception cref="NotImplementedException"></exception>
|
|||
|
private void OnBindClientID(JNServerParam args)
|
|||
|
{
|
|||
|
var message = RClientIDMessage.Parser.ParseFrom(args.Message);
|
|||
|
ids[args.Client] = message.ClientId;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 接收帧数入
|
|||
|
/// </summary>
|
|||
|
/// <param name="args"></param>
|
|||
|
private void OnNSyncFrameInput(JNServerParam args)
|
|||
|
{
|
|||
|
var inputs = JNFrameInputs.Parser.ParseFrom(args.Message);
|
|||
|
var frame = new JNFrameInfo();
|
|||
|
frame.Index = 0;
|
|||
|
foreach (var input in inputs.Inputs)
|
|||
|
{
|
|||
|
frame.Messages.Add(input);
|
|||
|
}
|
|||
|
App.Game.AddInput(frame);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 接收瓦片输入
|
|||
|
/// </summary>
|
|||
|
/// <param name="args"></param>
|
|||
|
private void OnNSyncTileInput(JNServerParam args)
|
|||
|
{
|
|||
|
var inputs = JNStateTileInputs.Parser.ParseFrom(args.Message);
|
|||
|
//只有绑定过ID 的客户端才可以执行操作
|
|||
|
inputs.Message.Inputs.ForEach(child =>
|
|||
|
{
|
|||
|
child.ClientId = args.Client;
|
|||
|
});
|
|||
|
App.Game.AddTileInput(inputs);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|