2024-10-20 12:10:57 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2024-10-22 00:13:13 +08:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
using Serein.Library.Network.WebSocketCommunication;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Serein.Library.Utils
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 管理远程环境,具备连接、发送消息、停止的功能
|
|
|
|
|
|
/// </summary>
|
2024-10-27 00:54:10 +08:00
|
|
|
|
public class RemoteMsgUtil
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// 远程环境配置
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// </summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public class ControlConfiguration
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 远程环境的网络地址
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string Addres { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 远程环境的对外端口
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int Port { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 登录远程环境必须携带的token(可以为可序列化的JSON对象)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public object Token { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 有关消息ID的 Json Key
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string MsgIdJsonKey { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 有关消息主题的 Json Key
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string ThemeJsonKey { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 有关数据的 Json Key
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string DataJsonKey { get; set; }
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// 配置远程连接IP端口
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// </summary>
|
2024-10-27 00:54:10 +08:00
|
|
|
|
public RemoteMsgUtil(ControlConfiguration controlConfiguration)
|
2024-10-22 00:13:13 +08:00
|
|
|
|
{
|
|
|
|
|
|
Config = controlConfiguration;
|
|
|
|
|
|
}
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// 配置信息
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// </summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public ControlConfiguration Config { get; }
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 连接到远程的客户端
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public WebSocketClient EnvClient { get; } = new WebSocketClient();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 是否连接到了远程环境
|
|
|
|
|
|
/// </summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
//public bool IsConnectdRemoteEnv { get => isConnectdRemoteEnv; }
|
|
|
|
|
|
//private bool isConnectdRemoteEnv = false;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 尝试连接到远程环境
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<bool> ConnectAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 第2种,WebSocket连接到远程环境,实时接收远程环境的响应?
|
2024-10-22 00:13:13 +08:00
|
|
|
|
Console.WriteLine($"准备连接:{Config.Addres}:{Config.Port},{Config.Token}");
|
2024-10-20 12:10:57 +08:00
|
|
|
|
bool success = false;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var tcpClient = new TcpClient();
|
2024-10-22 00:13:13 +08:00
|
|
|
|
var result = tcpClient.BeginConnect(Config.Addres, Config.Port, null, null);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(3));
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!success)
|
|
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
Console.WriteLine($"无法连通远程端口 {Config.Addres}:{Config.Port}");
|
2024-10-20 12:10:57 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
var url = $"ws://{Config.Addres}:{Config.Port}/";
|
2024-10-20 12:10:57 +08:00
|
|
|
|
var result = await EnvClient.ConnectAsync(url); // 尝试连接远程环境
|
2024-10-22 00:13:13 +08:00
|
|
|
|
//this.isConnectdRemoteEnv = result;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 发送消息
|
|
|
|
|
|
/// </summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// <param name="msgId"></param>
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// <param name="theme"></param>
|
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public async Task SendAsync(string msgId , string theme, object data)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
JObject jsonData;
|
|
|
|
|
|
|
|
|
|
|
|
if (data is null)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
jsonData = new JObject()
|
|
|
|
|
|
{
|
|
|
|
|
|
[Config.MsgIdJsonKey] = msgId,
|
|
|
|
|
|
[Config.ThemeJsonKey] = theme,
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
JToken dataToken;
|
|
|
|
|
|
if (data is System.Collections.IEnumerable || data is Array)
|
|
|
|
|
|
{
|
|
|
|
|
|
dataToken = JArray.FromObject(data);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
dataToken = JObject.FromObject(data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
jsonData = new JObject()
|
|
|
|
|
|
{
|
|
|
|
|
|
[Config.MsgIdJsonKey] = msgId,
|
|
|
|
|
|
[Config.ThemeJsonKey] = theme,
|
|
|
|
|
|
[Config.DataJsonKey] = dataToken
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
var msg = jsonData.ToString();
|
2024-10-27 00:54:10 +08:00
|
|
|
|
Console.WriteLine($"[{msgId}] => {theme}");
|
2024-10-20 12:10:57 +08:00
|
|
|
|
await EnvClient.SendAsync(msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|