在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);
    }
}
相关推荐
mxwin12 小时前
Unity Shader 半透明物体为什么不能写入深度缓冲?
unity·游戏引擎·shader
晚枫歌F13 小时前
三层时间轮的实现
网络·unity·游戏引擎
咸鱼永不翻身14 小时前
Lua脚本事件检查工具
unity·lua·工具
leo__52016 小时前
单载波中继系统资源分配算法MATLAB仿真程序
算法·matlab·unity
Bytenerd_017 小时前
【中级软件设计师】传输层协议 —— TCP/UDP (附软考真题)
udp·tcp·传输层协议·中级软件设计师·软考真题
努力长头发的程序猿17 小时前
Unity使用ScriptableObject序列化资源
unity·游戏引擎
mxwin17 小时前
Unity Shader 手写基于 PBR 的 URP Lit Shader 核心光照计算
unity·游戏引擎·shader
小贺儿开发17 小时前
Unity3D 智能云端数字标牌系统
unity·阿里云·人机交互·视频·oss·广告·互动
魔士于安18 小时前
Unity windows 同步 异步 打开文件文件夹工具
游戏·unity·游戏引擎·贴图·模型
魔士于安19 小时前
unity lowpoly 风格 城市 建筑 道路 交通标志
游戏·unity·游戏引擎·贴图·模型