在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);
    }
}
相关推荐
Little丶Seven2 小时前
使用adb获取安卓模拟器日志
android·unity·adb·个人开发
_星辰大海乀6 小时前
UDP / TCP 协议
网络·网络协议·tcp/ip·udp·tcp·三次握手·四次挥手
奔跑吧邓邓子1 天前
【C语言实战(73)】深入C语言网络编程:UDP与TCP的实战对决
c语言·udp·网络编程·tcp·开发实战
黄思搏2 天前
Unity坐标转换指南 - 3D与屏幕UI坐标互转
ui·3d·unity
weixin_424294672 天前
在 Unity 游戏开发中,为视频选择 VP8 还是 H.264
unity·游戏引擎
一步一个foot-print3 天前
【Unity】Light Probe 替代点光源给环境动态物体加光照
unity·游戏引擎
@LYZY3 天前
Unity 中隐藏文件规则
unity·游戏引擎·游戏程序·vr
霜绛3 天前
C#知识补充(二)——命名空间、泛型、委托和事件
开发语言·学习·unity·c#
Sator13 天前
使用Unity ASE插件设置数值不会生效的问题
unity·游戏引擎