在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);
    }
}
相关推荐
康de哥10 小时前
在OpenCode中配置unity3d-mcp
unity·glm-4.7·minimax m2.1·opencode·unity3d-mcp
在路上看风景11 小时前
1.5 AssetDataBase
unity
qianbo_insist12 小时前
unity 无头模式启动
unity·游戏引擎
郝学胜-神的一滴12 小时前
深入解析Mipmap层级判定原理:从理论到实践
c++·unity·godot·游戏程序·图形渲染·unreal engine
绀目澄清1 天前
unity3d AI Navigation 中文文档
游戏·unity
绀目澄清1 天前
Unity 的AI Navigation 系统详细总结
人工智能·unity·游戏引擎
绀目澄清1 天前
Unity3D AI Navigation 详解:从基础概念到实战应用
unity·游戏引擎
绀目澄清2 天前
Unity3D AI导航系统完全指南:从核心概念到动画耦合
人工智能·unity
__water2 天前
RHK《模型贴图自由更换位置》
unity·贴图·模型贴图·移动不丢失
JIes__2 天前
Unity(二)——3D数学
unity·游戏引擎