在Unity3d中使用Netly开启TCP服务

github文档

实测Windows和安卓上可运行

cs 复制代码
using Byter;
using Netly;
using System.Collections.Generic;
using System.Net;
using UnityEngine;
using UnityEngine.UI;

public class NetlyTest : MonoBehaviour
{
    public Text textPrompt;
    public Text textClientCount;
    TCP.Server server;
    void Start()
    {
        server = new TCP.Server(isFraming: false);

        server.On.Open(() =>
        {
            Log($"Server started at: {server.Host}");
        });

        server.On.Close(() =>
        {
            Log($"Server stopped at: {server.Host}");
        });

        server.On.Error((exception) =>
        {
            Log($"Error: {exception}");
        });

        server.On.Accept((client) =>
        {
            // scope reserved to each client
            // each client have on scope like this.

            // it mean each client will have own instance of eventData and rawData
            List<(string message, byte[] data)> eventData = new();
            List<byte[]> rawData = new();
            
            Log("Client connected");
            client.On.Open(() =>
            {

            });

            client.On.Close(() =>
            {
                // client closed;
                Log($"Client {client.Host} disconnected");

                // clean all data received by this client
                rawData.Clear();
                eventData.Clear();
            });

            client.On.Data((bytes) =>
            {
                Log($"Client data ({client.Host}): {bytes.GetString()}");
                //给所有终端发送消息
                //server.To.DataBroadcast(bytes); // broadcast data
                //给单个终端发消息
                client.To.Data($"server send:{bytes.GetString()}");
                // save history of data
                rawData.Add(bytes);
            });

            client.On.Event((name, bytes) =>
            {
                Log($"Client event ({name}): ({client.Host}): {bytes.GetString()}");
                server.To.EventBroadcast(name, bytes); // broadcast event

                // save history of events
                eventData.Add((name, bytes));
            });
        });

        server.To.Open(new Host(IPAddress.Any, 8080));
    }

    // Update is called once per frame
    void Update()
    {
        if (Time.frameCount % 5 == 0)
        {
            textClientCount.text = ($"server.Clients.Length = {server.Clients.Length}");
        }
    }

    private void OnDisable()
    {
        server.To.Close();
    }

    void Log(string text)
    {
        textPrompt.text = text;
        Debug.Log(text);
    }
}
相关推荐
叶帆12 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
久数君12 天前
AI三维建模工具“造形家”:地理场景三维化的高效解决方案
unity·glb·ai算法·ai三维建模工具·地图框选·造形家·城市建筑模型
会思考的猴子12 天前
Unity VFX 属性 Postion 和 TargetPostion
unity
心前阳光12 天前
Unity资源导入之自动化资源导入
unity·自动化·游戏引擎
心前阳光13 天前
Unity之2021.3.45f2c1发布安卓程序遇到的问题
android·unity·游戏引擎
纪纯13 天前
PicoVR Unity Integration SDK 3.4 常用交互API
unity·游戏引擎·vr·pico
龙智DevSecOps解决方案13 天前
3A 游戏优化技术栈:如何打通引擎级分析工具与 DevOps 持续集成管线?
unity·性能优化·游戏开发·技术美术·perforce·unrealengine
葛兰岱尔13 天前
从 SolidWorks 到 Three.js,从 Inventor 到 Unity——制造业CAD模型“几何-语义一体化“转换,不再是天方夜谭!
开发语言·javascript·unity
玉夏13 天前
【Shader基础】UV 与纹理采样 Part1
unity·着色器·uv
liulilittle13 天前
回归物理本质:对拥塞控制实验室依赖与公平性误置的反思
网络·tcp/ip·计算机网络·算法·tcp·通信·拥塞控制