• 大小: 0.13M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2024-06-18
  • 语言: C#
  • 标签: unity  IT  ty  

资源简介

NET的Socket KCP Protobuf的Unity局域网联机测试范例, 大量功能待补充

资源截图

代码片段和文件信息

using System;
using System.Net;
using System.Net.Sockets;
using UnityEngine;
using System.Timers;

namespace HNet
{
    internal class NetController : Singleton
    {
        //刷新间隔
        public readonly int Interval = 10;

        public ServerClass Server;
        public ClientClass Client;

        private Timer _timer;

        //计算时间戳
        private static readonly DateTime UtcTime = new DateTime(1970 1 1);

        public bool StartServer(int port)
        {
            if (Server == null)
            {
                Server = new ServerClass();
                if (Server.Start(port))
                {
                    InitTimer();
                    return true;
                }
                else
                {
                    Server.Close();
                    return false;
                }
            }
            return false;
        }

        public void CloseServer()
        {
            if (Server != null)
            {
                Server.Close();
                Server = null;
            }
        }

        public bool StartClient(string host int port)
        {
            if (Client == null)
            {
                Client = new ClientClass();

                if (Client.TcpConnetion(host port))
                {
                    InitTimer();
                    InitClientCoroutinue();

                    //判断是否是本地IP
                    string hostName = Dns.GetHostName();   //获取本机名
                    IPHostEntry localhost = Dns.GetHostEntry(hostName);    //方法已过期,可以获取IPv4的地址

                    for (int i = localhost.AddressList.Length - 1; i >= 0; i--)
                    {
                        if (localhost.AddressList[i].ToString().Contains(“.“))
                        {
                            if (localhost.AddressList[i].ToString() == host)
                            {
                                //本地数据互联
                                if (Server != null && Client != null)
                                {
                                    Server.SetLocalClient((int)Client.Conv NetCoroutinue.Instance.LocalHostDistribute);
                                    NetCoroutinue.Instance.SetLocalHost(Server.LocalHostDistribute);
                                }
                            }
                        }
                    }

                    return true;
                }
                else
                {
                    CloseClient();

                    return false;
                }
            }
            return false;
        }

        public void CloseClient()
        {
            if (Client != null)
            {
                Client.Close();
                CloseClientCoroutinue();
                Client = null;
            }
        }

        private void Update()
        {
            uint current = Iclock();

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-07-12 18:18  unity-net\
     文件         191  2017-07-12 18:18  unity-net\Core.meta
     目录           0  2017-07-12 18:18  unity-net\Core\
     文件         191  2017-07-12 18:18  unity-net\Core\Client.meta
     目录           0  2017-07-12 18:18  unity-net\Core\Client\
     文件        6311  2017-07-12 18:18  unity-net\Core\Client\ClientClass.cs
     文件         262  2017-07-12 18:18  unity-net\Core\Client\ClientClass.cs.meta
     文件        1089  2017-07-12 18:18  unity-net\Core\Client\MessageProcessing.cs
     文件         262  2017-07-12 18:18  unity-net\Core\Client\MessageProcessing.cs.meta
     文件         289  2017-07-12 18:18  unity-net\Core\Client\NetMonoBehaviour.cs
     文件         262  2017-07-12 18:18  unity-net\Core\Client\NetMonoBehaviour.cs.meta
     文件         329  2017-07-12 18:18  unity-net\Core\Client\NetobjectManager.cs
     文件         262  2017-07-12 18:18  unity-net\Core\Client\NetobjectManager.cs.meta
     文件        2524  2017-07-12 18:18  unity-net\Core\Client\RemoteMethod.cs
     文件         262  2017-07-12 18:18  unity-net\Core\Client\RemoteMethod.cs.meta
     文件        4824  2017-07-12 18:18  unity-net\Core\NetController.cs
     文件         262  2017-07-12 18:18  unity-net\Core\NetController.cs.meta
     文件         652  2017-07-12 18:18  unity-net\Core\NetLog.cs
     文件         262  2017-07-12 18:18  unity-net\Core\NetLog.cs.meta
     文件         191  2017-07-12 18:18  unity-net\Core\Server.meta
     目录           0  2017-07-12 18:18  unity-net\Core\Server\
     文件        2098  2017-07-12 18:18  unity-net\Core\Server\Conn.cs
     文件         262  2017-07-12 18:18  unity-net\Core\Server\Conn.cs.meta
     文件       11291  2017-07-12 18:18  unity-net\Core\Server\ServerClass.cs
     文件         262  2017-07-12 18:18  unity-net\Core\Server\ServerClass.cs.meta
     文件         191  2017-07-12 18:18  unity-net\Core\common.meta
     目录           0  2017-07-12 18:18  unity-net\Core\common\
     文件        1082  2017-07-12 18:18  unity-net\Core\common\Message.cs
     文件         262  2017-07-12 18:18  unity-net\Core\common\Message.cs.meta
     文件        3248  2017-07-12 18:18  unity-net\Core\common\NetCoroutinue.cs
     文件         262  2017-07-12 18:18  unity-net\Core\common\NetCoroutinue.cs.meta
............此处省略30个文件信息

评论

共有 条评论