SuperSocket
SuperSocket copied to clipboard
在Unity中启动服务器无效,Unity卡住
在.net 7.0中服务器正常启动
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"));
}
});
端口没有被占用
我想看看日志,于是让日志输出到文件,在.net 7.0中能正常生成log文件,但是在unity中没有生成日志文件
逐步运行发现是 Build 这个函数阻塞住了
等了2分钟,才执行到下面代码
为什么在Task.Run里面?能debug吗?
为什么在Task.Run里面?能debug吗?
因为要使用await\async,所以放到了Task.Run里面。这两段代码在普通的C#工程中运行没有问题 可以运行debug模式,但是运行到Build函数就卡住了
Debug的时候把Performance Profiler打开。
Made a fix possibly related to U3d, could you try the latest version in myget?