在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);
    }
}
相关推荐
め.1 小时前
定点数运算库
算法·unity
郝学胜-神的一滴5 小时前
[简化版 GAMES 104] 现代游戏引擎 02:拆解现代游戏引擎5+1层级架构,吃透引擎底层核心逻辑
c++·unity·架构·游戏引擎·图形渲染·unreal engine·系统设计
LONGZETECH7 小时前
新能源汽车充电设备装配与调试仿真教学软件 技术架构与核心实现解析
大数据·算法·3d·unity·架构·汽车
丁小未8 小时前
Unity 几种常见合批手段的要求
游戏·unity·合批·srpbatcher·动态合批·静态合批
旧物有情1 天前
游戏开发常用架构 #MVP,MVC
游戏·unity·架构·mvc
勇踏前人未索之境1 天前
Unity打包运行于鸿蒙手机
unity·智能手机·harmonyos
玖玥拾1 天前
Unity 3D 笔记(十一)UI 框架进阶:栈弹窗交互、BasePanel 基类虚方法、DoTween 界面动画
笔记·3d·unity
郝学胜-神的一滴1 天前
中级OpenGL教程 023:Assimp模型加载全解——从源码到架构的骈文探秘
c++·unity·游戏引擎·cmake·unreal engine·opengl
xcLeigh2 天前
Unity基础:GameObject与Component——Unity核心架构思想彻底理解
unity·教程·component·gameobject