Files
ThingsGateway/framework/ThingsGateway.Blazor/DebugPage/TcpServerPage.razor.cs
2023-08-18 09:57:03 +08:00

99 lines
3.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#region copyright
//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权除特别声明外的代码归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库https://github.com/kimdiego2098/ThingsGateway
// 使用文档https://diego2098.gitee.io/thingsgateway-docs/
// QQ群605534569
//------------------------------------------------------------------------------
#endregion
using TouchSocket.Core;
using TouchSocket.Sockets;
namespace ThingsGateway.Blazor;
/// <inheritdoc/>
public partial class TcpServerPage
{
/// <summary>
/// 日志输出
/// </summary>
public Action<LogLevel, object, string, Exception> LogAction;
private TouchSocketConfig config;
private string ip = "127.0.0.1";
private int port = 502;
private TcpService TcpServer { get; set; } = new();
/// <inheritdoc/>
public void Dispose()
{
TcpServer.SafeDispose();
}
private void Connect()
{
try
{
TcpServer.Stop();
GetTcpServer().Start();
}
catch (Exception ex)
{
LogAction?.Invoke(LogLevel.Error, null, null, ex);
}
}
private void DisConnect()
{
try
{
TcpServer.Stop();
}
catch (Exception ex)
{
LogAction?.Invoke(LogLevel.Error, null, null, ex);
}
}
/// <summary>
/// 获取对象
/// </summary>
/// <returns></returns>
public TcpService GetTcpServer()
{
config ??= new TouchSocketConfig();
var LogMessage = new TouchSocket.Core.LoggerGroup() { LogLevel = TouchSocket.Core.LogLevel.Trace };
LogMessage.AddLogger(new EasyLogger(LogOut) { LogLevel = TouchSocket.Core.LogLevel.Trace });
config.ConfigureContainer(a => a.RegisterSingleton<ILog>(LogMessage));
config.SetListenIPHosts(new IPHost[] { new IPHost(ip + ":" + port) });
config.SetBufferLength(300);
//载入配置
TcpServer.Setup(config);
return TcpServer;
}
/// <summary>
/// <inheritdoc/>
/// </summary>
protected override void OnInitialized()
{
config?.Dispose();
config = new TouchSocketConfig();
var LogMessage = new TouchSocket.Core.LoggerGroup() { LogLevel = TouchSocket.Core.LogLevel.Trace };
LogMessage.AddLogger(new EasyLogger(LogOut) { LogLevel = TouchSocket.Core.LogLevel.Trace });
config.ConfigureContainer(a => a.RegisterSingleton<ILog>(LogMessage));
config.SetListenIPHosts(new IPHost[] { new IPHost(ip + ":" + port) });
config.SetBufferLength(300);
TcpServer = new TcpService();
TcpServer.Setup(config);
base.OnInitialized();
}
private void LogOut(LogLevel logLevel, object source, string message, Exception exception) => LogAction?.Invoke(logLevel, source, message, exception);
}