Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在Unity中启动服务器无效,Unity卡住 #707

Open
RouderSky opened this issue Apr 9, 2024 · 7 comments
Open

在Unity中启动服务器无效,Unity卡住 #707

RouderSky opened this issue Apr 9, 2024 · 7 comments
Labels
2.0 SuperSocket 2.0 u3d

Comments

@RouderSky
Copy link

RouderSky commented Apr 9, 2024

在.net 7.0中服务器正常启动

image

            Task.Run(async () =>
            {
                var serverCfg = SuperSocketHostBuilder.Create<TextPackageInfo, LinePipelineFilter>();
                //var host = SuperSocketHostBuilder.Create<MyPackage, new MyPackageFilter() > (args)
                serverCfg.UsePackageHandler(async (s, p) =>
                    {
                        DebugManager.LogInfo($"server : Get data, length: {p.Text.Length}");

                        //处理接收到的数据
                        //DebugManager.LogInfo(p.Text);
                        //await s.SendAsync(Encoding.UTF8.GetBytes(p.Text + " from server " + "\r\n"));
                    });
                serverCfg.UseSessionHandler(async (s) =>
                    {
                        //新的连接建立
                        DebugManager.LogInfo("server : Session connected.");
                        await Task.CompletedTask;
                    },
                    async (s, e) =>
                    {
                        // s: 当前会话
                        // e: 会话关闭事件参数
                        // e.Reason: 会话关闭原因

                        // 会话连接断开后的逻辑
                        DebugManager.LogInfo("server : Session disconnected.");
                        await Task.CompletedTask;
                    });
                    //.UseSession<MyAppSession>()
                    //.UseHostedService< GameService<TextPackageInfo> >()
                serverCfg.ConfigureSuperSocket(options =>
                    {
                        //配置服务器如服务器名和监听端口等基本信息
                        options.Name = "Echo Server";
                        options.AddListener(new ListenOptions
                            {
                                Ip = "Any",
                                //Ip = IPAddress.Loopback.ToString(),
                                Port = 4040
                            }
                        );
                        options.MaxPackageLength = 1024 * 1024 * 100; //单位是字节
                    });
                serverCfg.ConfigureLogging((hostCtx, loggingBuilder) =>
                    {
                        //配置日志
                        //loggingBuilder.AddConsole();        //仅仅启用Console日志输出
                        loggingBuilder.AddFile((configure) =>
                        {
                            //configure.RootPath = "logs";
                            configure.BasePath = "logs";
                            configure.Files = new LogFileOptions[]
                            {
                                new LogFileOptions
                                {
                                    Path = "loghaha.txt",
                                    MinLevel = new Dictionary<string, LogLevel>
                                    {
                                        { "Default", LogLevel.Debug },
                                        { "System", LogLevel.Information },
                                        { "Microsoft", LogLevel.Information },
                                    }
                                }
                            };
                        });
                    });

                IHost serverHost = serverCfg.Build();
                await serverHost.RunAsync();
            });
            Task.Run(async () =>
            {
                //var client = new EasyClient<MyPackage>(new MyPackageFilter()).AsClient();
                var client = new EasyClient<TextPackageInfo>(new LinePipelineFilter()).AsClient();

                if (!await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 4040)))
                {
                    // Console.WriteLine("Failed to connect the target server.");
                    DebugManager.LogInfo("Failed to connect the target server.");
                    return;
                }

                //while (true)
                //{
                //    await client.SendAsync(Encoding.UTF8.GetBytes("hello world\r\n"));

                //    var p = await client.ReceiveAsync();

                //    if (p == null) // connection dropped
                //        break;

                //    Console.WriteLine(p.Text);
                //    //Console.WriteLine(p.Body);
                //}

                for (int _ = 0; _ < 2; _++)
                {
                    Byte[] bigBytes = new Byte[1024 * 1024 * 50];
                    //for (int i = 0; i < bigBytes.Length; i++)
                    //    bigBytes[i] = 0xFF;
                    string strOfBigBytes = Encoding.UTF8.GetString(bigBytes);
                    await client.SendAsync(Encoding.UTF8.GetBytes(strOfBigBytes + "\r\n"));
                }
            });

image

端口没有被占用

@RouderSky RouderSky changed the title 在Unity中启动服务器无效,且没有任何提示,直接卡住Unity 在Unity中启动服务器无效,没有生成日志文件,Unity卡住 Apr 9, 2024
@RouderSky RouderSky changed the title 在Unity中启动服务器无效,没有生成日志文件,Unity卡住 在Unity中启动服务器无效,Unity卡住 Apr 9, 2024
@RouderSky
Copy link
Author

我想看看日志,于是让日志输出到文件,在.net 7.0中能正常生成log文件,但是在unity中没有生成日志文件

@RouderSky
Copy link
Author

RouderSky commented Apr 9, 2024

逐步运行发现是 Build 这个函数阻塞住了
image

@RouderSky
Copy link
Author

等了2分钟,才执行到下面代码
image

@kerryjiang
Copy link
Owner

为什么在Task.Run里面?能debug吗?

@RouderSky
Copy link
Author

RouderSky commented Apr 14, 2024

为什么在Task.Run里面?能debug吗?

因为要使用await\async,所以放到了Task.Run里面。这两段代码在普通的C#工程中运行没有问题
可以运行debug模式,但是运行到Build函数就卡住了

@kerryjiang
Copy link
Owner

Debug的时候把Performance Profiler打开。

@kerryjiang
Copy link
Owner

Made a fix possibly related to U3d, could you try the latest version in myget?

@kerryjiang kerryjiang added 2.0 SuperSocket 2.0 u3d labels Sep 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.0 SuperSocket 2.0 u3d
Projects
None yet
Development

No branches or pull requests

2 participants