2025-05-22 13:06:49 +08:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
using SocketTool;
|
|
|
|
|
|
using SocketTool.Entity;
|
|
|
|
|
|
using WcsMain.DataBase.Dao;
|
|
|
|
|
|
using WcsMain.DataBase.TableEntity;
|
|
|
|
|
|
using WcsMain.Business.CommonAction;
|
|
|
|
|
|
using WcsMain.WcsAttribute.AutoFacAttribute;
|
|
|
|
|
|
|
|
|
|
|
|
namespace WcsMain.Socket;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// socket 连接类
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Component]
|
|
|
|
|
|
public class SocketOperation(AppTcpDao socketDao)
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly AppTcpDao _socketDao = socketDao;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 连接 socket
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void Connect()
|
|
|
|
|
|
{
|
|
|
|
|
|
/* 查找所有待连接的 socket 信息 */
|
|
|
|
|
|
List<AppTcp>? sockets = _socketDao.GetNeedUseSocket(1);
|
|
|
|
|
|
if (sockets == default || sockets.Count < 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
ConsoleLog.Info($"[提示]没有找到需要连接的 socket 信息,将不会进行连接。");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
List<string?> socketInfo = [];
|
|
|
|
|
|
foreach (var socket in sockets)
|
|
|
|
|
|
{
|
|
|
|
|
|
socketInfo.Add(socket.TcpIp);
|
|
|
|
|
|
}
|
|
|
|
|
|
/* 注册事件,连接 socket */
|
|
|
|
|
|
SocketClient socketClient = new(Encoding.UTF8, [.. socketInfo]);
|
|
|
|
|
|
socketClient.SocketConnecting += address =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ConsoleLog.Info($"socket {address} 正在连接...");
|
|
|
|
|
|
};
|
|
|
|
|
|
socketClient.SocketOffline += address =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ConsoleLog.Error($"[警告]socket {address} 失去连接...");
|
|
|
|
|
|
};
|
|
|
|
|
|
socketClient.SocketOnline += address =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ConsoleLog.Info($"socket {address} 连接成功。");
|
|
|
|
|
|
};
|
|
|
|
|
|
socketClient.SocketConnectFail += (address, exception) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ConsoleLog.Error($"[警告]socket {address} 连接失败,异常信息:{exception.Message}");
|
|
|
|
|
|
};
|
|
|
|
|
|
socketClient.RecevErrEvent += (address, exception) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
ConsoleLog.Error($"[警告]socket {address} 接收数据异常,异常信息:{exception.Message}");
|
|
|
|
|
|
};
|
|
|
|
|
|
socketClient.HandleCodeInfo += SocketClientOnHandleCodeInfo;
|
|
|
|
|
|
socketClient.Connect();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//private ScanCodeAction? scanCodeAction;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 处理收到的数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="codeEntity"></param>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// 扫码器方法所在的类为 <see cref="ScanCodeAction"/>
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
private void SocketClientOnHandleCodeInfo(ScanCodeClass codeEntity)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 后面单独表配置
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
}
|
2024-05-14 16:30:56 +08:00
|
|
|
|
}
|