using System.Diagnostics;
using XFEExtension.NetCore.CyberComm;
namespace XFEExtension.NetCore.XFEConsole;
///
/// XFE控制台程序客户端
///
///
/// 默认构造函数
///
/// 控制台输出端IP地址
/// 客户端名称
/// 客户端ID
/// 密码
public class XFEConsoleProgramClient(string ipAddress, string clientName, string clientID, string password)
{
///
/// 客户端
///
public CyberCommClient Client { get; set; } = new(new CyberCommClientOptions
{
ServerUri = new Uri(ipAddress, UriKind.Absolute),
Reconnect = new CyberCommReconnectOptions { Enabled = false, MaxAttempts = 0 },
RequestHeaders = new Dictionary
{
[nameof(ClientName)] = clientName,
[nameof(Password)] = password,
[nameof(ClientID)] = clientID
}
});
///
/// 客户端名称
///
public string ClientName { get; set; } = clientName;
///
/// 客户端ID
///
public string ClientID { get; set; } = clientID;
///
/// 连接时密码
///
public string Password { get; set; } = password;
///
/// 连接服务器
///
///
public async Task Connect()
{
try
{
Client.ErrorHandler = exception => Debug.WriteLine(exception);
await Client.ConnectAsync().ConfigureAwait(false);
return Client.IsConnected;
}
catch (Exception exception)
{
Debug.WriteLine(exception);
return false;
}
}
///
/// 输出控制台消息
///
/// 消息文本
/// 是否是一整行
///
public async Task OutputMessage(string message, bool isLine)
{
try
{
if (Client.IsConnected)
await Client.SendTextAsync(XFEConsoleProtocol.CreateOutputMessage(message, isLine));
}
catch { }
}
}