| | |
| | | using System.Text; |
| | | using System.Text.Json; |
| | | using System.IO; |
| | | using WIDESEAWCS_Model.Models; |
| | | |
| | | namespace WIDESEAWCS_Tasks.SocketServer |
| | | { |
| | |
| | | /// <param name="client">TCP 客户端连接</param> |
| | | /// <param name="clientId">客户端唯一标识</param> |
| | | /// <param name="cancellationToken">取消令牌</param> |
| | | /// <param name="robotCrane">机器人状态</param> |
| | | public async Task HandleClientAsync(TcpClient client, string clientId, CancellationToken cancellationToken, RobotSocketState robotCrane) |
| | | public async Task HandleClientAsync(TcpClient client, string clientId, CancellationToken cancellationToken) |
| | | { |
| | | using (client) |
| | | using (NetworkStream networkStream = client.GetStream()) |
| | |
| | | break; |
| | | } |
| | | |
| | | if(message == lastMessage) |
| | | // 按客户端去重:检查是否与该客户端上次消息相同 |
| | | lock (_syncRoot) |
| | | { |
| | | // 重复消息,忽略 |
| | | continue; |
| | | if (_clientLastMessage.TryGetValue(clientId, out var prev) && message == prev) |
| | | { |
| | | continue; |
| | | } |
| | | _clientLastMessage[clientId] = message; |
| | | } |
| | | |
| | | // 更新客户端状态 |
| | |
| | | { |
| | | // 判断是否为 JSON 格式 |
| | | bool isJsonFormat = TryParseJsonSilent(message); |
| | | _ = MessageReceived.Invoke(message, isJsonFormat, client, robotCrane); |
| | | _ = MessageReceived.Invoke(message, isJsonFormat, client); |
| | | } |
| | | catch { } |
| | | } |