Administrator 2 months ago
parent
commit
f11cb0f809
  1. 17
      Class/HttpServer.cs
  2. 2
      Class/TcpServer.cs
  3. 2
      View/LogPage.xaml.cs
  4. 69
      WebSocketServer.cs

17
Class/HttpServer.cs

@ -19,8 +19,16 @@ namespace SunlightAggregationTerminal.Class
{ {
string[] parts = ip.Split('/'); string[] parts = ip.Split('/');
DataReceived.ServerIP = parts[0]; if (parts.Length >= 2)
DataReceived.ServerURL = parts[1]; {
DataReceived.ServerIP = parts[0];
DataReceived.ServerURL = parts[1];
}
else
{
DataReceived.ServerIP = "";
DataReceived.ServerURL = "";
}
DataReceived.ServerName = Name; DataReceived.ServerName = Name;
DataReceived.ServerPassword = Password; DataReceived.ServerPassword = Password;
DataReceived.ServerTerminal = Terminal; DataReceived.ServerTerminal = Terminal;
@ -71,6 +79,11 @@ namespace SunlightAggregationTerminal.Class
});*/ });*/
#endregion #endregion
if (httpclient.Online)//是否重置客户端
{
await httpclient.CloseAsync();
}
//配置config //配置config
await httpclient.SetupAsync(config); await httpclient.SetupAsync(config);
//连接 //连接

2
Class/TcpServer.cs

@ -87,7 +87,7 @@ namespace SunlightAggregationTerminal.Class
{ {
await tcpClient.SetupAsync(new TouchSocketConfig() await tcpClient.SetupAsync(new TouchSocketConfig()
.SetSingleStreamDataHandlingAdapter(() => new PeriodPackageAdapter())//以超时周期和包 .SetSingleStreamDataHandlingAdapter(() => new PeriodPackageAdapter())//以超时周期和包
.SetRemoteIPHost(ip) .SetRemoteIPHost(ip)
.ConfigurePlugins(a => .ConfigurePlugins(a =>
{ {
a.UseReconnection<TouchSocket.Sockets.TcpClient>(options => a.UseReconnection<TouchSocket.Sockets.TcpClient>(options =>

2
View/LogPage.xaml.cs

@ -71,7 +71,7 @@ public partial class LogPage : ContentPage
{ {
// 跳转到扫码页面 // 跳转到扫码页面
var scanPage = new ScanPage(3); var scanPage = new ScanPage(3);
Navigation.PushAsync(scanPage); Navigation.PushModalAsync(scanPage);
} }
private async void Log_Clicked(object sender, EventArgs e)//登录 private async void Log_Clicked(object sender, EventArgs e)//登录

69
WebSocketServer.cs

@ -1,69 +0,0 @@
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();
}
}
}
Loading…
Cancel
Save