整合管理器应用端(MAUI跨平台)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.2 KiB

using System;
using System.Collections.Generic;
using System.Text;
using TouchSocket.Core;
using TouchSocket.Http;
using TouchSocket.Http.WebSockets;
using TouchSocket.Sockets;
namespace SunlightAggregationTerminal
{
public class WebSocketServer
{
public static async Task WebSocketClient()
{
using var webSocket = new WebSocketClient();
webSocket.Connected = (c, e) =>
{
Console.WriteLine("Connected");
return EasyTask.CompletedTask;
};
#region WebSocket客户端使用Received委托接收数据
webSocket.Received = (c, e) =>
{
Console.WriteLine(e.DataFrame.ToText());
return EasyTask.CompletedTask;
};
#endregion WebSocket客户端使用Received委托接收数据
webSocket.Closed = (c, e) =>
{
Console.WriteLine("Closed");
return EasyTask.CompletedTask;
};
await webSocket.SetupAsync(new TouchSocketConfig()
.ConfigureContainer(a =>
{
a.AddConsoleLogger();
})
.ConfigurePlugins(a =>
{
a.UseReconnection<WebSocketClient>(options =>
{
//设置在线状态轮询时间为5秒钟。
options.PollingInterval = TimeSpan.FromSeconds(5);
//使用websocket专门的心跳在线检测。基本逻辑如下:
//由于设置的PollingInterval为5秒,所以每5秒会检查一下WebSocketClient在线状态。
//1.每隔pingInterval的时间,发送一次ping报文。
//2.如果在activeTimeSpan(30秒)内,有数据收发,则也会认为在活。
options.UseWebSocketCheckAction(activeTimeSpan: TimeSpan.FromSeconds(30), pingInterval: TimeSpan.FromSeconds(5));
});
})
.SetRemoteIPHost("http://127.0.0.1:7792/ws"));
await webSocket.ConnectAsync();
}
}
}