Browse Source

公网访问相关

master
Administrator 2 months ago
parent
commit
a5f08f1f8e
  1. 155
      Class/DataReceived.cs
  2. 7
      Class/HttpServer.cs
  3. 2
      Models/AppModels.cs
  4. 69
      WebSocketServer.cs

155
Class/DataReceived.cs

@ -34,95 +34,126 @@ namespace SunlightAggregationTerminal.Class
} }
public static string ServerIP = ""; public static string ServerIP = "";
public static string ServerURL = "";
public static string ServerName = ""; public static string ServerName = "";
public static string ServerPassword = ""; public static string ServerPassword = "";
public static string ServerTerminal = ""; public static string ServerTerminal = "";
public static void Received(string dat) public static void Received(string dat)
{ {
try if (dat.Length < 6)
{ {
Person deserializedPerson = JsonSerializer.Deserialize<Person>(dat)!; string inf;
var data = JsonSerializer.Deserialize<Dictionary<string, object>>(deserializedPerson.Dat); switch (dat)
{
case "999":
inf = "不明错误";
break;
case "990":
inf = "拒接访问账户远程访问许可证过期";
break;
case "980":
inf = "目标对象服务器不存在或已离线";
break;
case "970":
inf = "目标对象服务器不存在或已离线";
break;
default:
inf = dat;
break;
}
if (data != null) MainThread.BeginInvokeOnMainThread(() =>
{ {
//处理服务器发送信息 Application.Current!.Windows[0].Page!.DisplayAlertAsync("", inf, "是");
if (data.TryGetValue("Enterprise", out var val) && val != null) App.GlobalData.Enterprise = val.ToString() ?? ""; });
if (data.TryGetValue("Department", out val) && val != null) App.GlobalData.Department = val.ToString() ?? ""; }
if (data.TryGetValue("Groups", out val) && val != null) App.GlobalData.Groups = val.ToString() ?? ""; else
if (data.TryGetValue("UserName", out val) && val != null) App.GlobalData.UserName = val.ToString() ?? ""; {
if (data.TryGetValue("UserId", out val) && val != null)
{
App.GlobalData.UserId = val.ToString() ?? "";
App.GlobalData.LogNo = true;
Transmit("{\"Command\":\"Info\"}"); try
} {
Person deserializedPerson = JsonSerializer.Deserialize<Person>(dat)!;
var data = JsonSerializer.Deserialize<Dictionary<string, object>>(deserializedPerson.Dat);
if (data.TryGetValue("UserData", out var value_UserData)) if (data != null)
{ {
//处理服务器发送信息
if (data.TryGetValue("Enterprise", out var val) && val != null) App.GlobalData.Enterprise = val.ToString() ?? "";
if (data.TryGetValue("Department", out val) && val != null) App.GlobalData.Department = val.ToString() ?? "";
if (data.TryGetValue("Groups", out val) && val != null) App.GlobalData.Groups = val.ToString() ?? "";
if (data.TryGetValue("UserName", out val) && val != null) App.GlobalData.UserName = val.ToString() ?? "";
if (data.TryGetValue("UserId", out val) && val != null)
{
App.GlobalData.UserId = val.ToString() ?? "";
App.GlobalData.LogNo = true;
} Transmit("{\"Command\":\"Info\"}");
if (data.TryGetValue("SysData", out var value_SysData)) }
{
} if (data.TryGetValue("UserData", out var value_UserData))
{
if (data.TryGetValue("Info", out val) && val != null)//机台信息接受 }
{ if (data.TryGetValue("SysData", out var value_SysData))
DataModel.Info_data = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(val.ToString()!)!; {
//发送消息
WeakReferenceMessenger.Default.Send(new object());
}
if (data.TryGetValue("Query", out val) && val != null)//查询接受
{
QueryPage.Query_data(val.ToString()!);
}
if (data.TryGetValue("QueryDetail", out val) && val != null)//查询接受
{
QueryDetail.Query_data(val.ToString()!);
}
if (data.TryGetValue("Notification", out val) && val != null)//信息通知接受
{
var dataNotification = JsonSerializer.Deserialize<Dictionary<string, object>>(val.ToString()!);
string _Title = "", _Content = ""; }
int _Type = 0;
if (dataNotification!.TryGetValue("Title", out var value_Notification_Title)) if (data.TryGetValue("Info", out val) && val != null)//机台信息接受
{ {
_Title = value_Notification_Title.ToString() ?? ""; DataModel.Info_data = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(val.ToString()!)!;
//发送消息
WeakReferenceMessenger.Default.Send(new object());
} }
if (dataNotification!.TryGetValue("Content", out var value_Notification_Content)) if (data.TryGetValue("Query", out val) && val != null)//查询接受
{ {
_Content = value_Notification_Content.ToString() ?? ""; QueryPage.Query_data(val.ToString()!);
} }
if (dataNotification!.TryGetValue("Type", out var value_Notification_Type)) if (data.TryGetValue("QueryDetail", out val) && val != null)//查询接受
{ {
string temp = value_Notification_Type?.ToString() ?? ""; QueryDetail.Query_data(val.ToString()!);
// 转换
_Type = temp switch
{
"System" => 2,
"Message" => 1,
_ => 0
};
} }
if (data.TryGetValue("Notification", out val) && val != null)//信息通知接受
{
var dataNotification = JsonSerializer.Deserialize<Dictionary<string, object>>(val.ToString()!);
string _Title = "", _Content = "";
int _Type = 0;
Models.AppModels.INSERT("INSERT INTO Notification (Title,Content,Time,Type)VALUES('" + if (dataNotification!.TryGetValue("Title", out var value_Notification_Title))
_Title + "','" + _Content + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "','" + _Type + "');"); {
_Title = value_Notification_Title.ToString() ?? "";
}
if (dataNotification!.TryGetValue("Content", out var value_Notification_Content))
{
_Content = value_Notification_Content.ToString() ?? "";
}
if (dataNotification!.TryGetValue("Type", out var value_Notification_Type))
{
string temp = value_Notification_Type?.ToString() ?? "";
// 转换
_Type = temp switch
{
"System" => 2,
"Message" => 1,
_ => 0
};
}
Models.AppModels.INSERT("INSERT INTO Notification (Title,Content,Time,Type)VALUES('" +
_Title + "','" + _Content + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "','" + _Type + "');");
}
} }
}
} }
catch (Exception ex) catch (Exception ex)
{
MainThread.BeginInvokeOnMainThread(() =>
{ {
Application.Current!.Windows[0].Page!.DisplayAlertAsync("", ex.Message.ToString(), "是"); MainThread.BeginInvokeOnMainThread(() =>
}); {
Application.Current!.Windows[0].Page!.DisplayAlertAsync("", ex.Message.ToString(), "是");
});
}
} }
} }

7
Class/HttpServer.cs

@ -17,7 +17,10 @@ namespace SunlightAggregationTerminal.Class
public static async void Configuration(string ip, string Name, string Password, string Terminal) public static async void Configuration(string ip, string Name, string Password, string Terminal)
{ {
DataReceived.ServerIP = ip; string[] parts = ip.Split('/');
DataReceived.ServerIP = parts[0];
DataReceived.ServerURL = parts[1];
DataReceived.ServerName = Name; DataReceived.ServerName = Name;
DataReceived.ServerPassword = Password; DataReceived.ServerPassword = Password;
DataReceived.ServerTerminal = Terminal; DataReceived.ServerTerminal = Terminal;
@ -89,7 +92,7 @@ namespace SunlightAggregationTerminal.Class
var request = new HttpRequest(); var request = new HttpRequest();
request.SetContent(new StringHttpContent(JsonSerializer.Serialize(dat), Encoding.UTF8, "application/scjson")) request.SetContent(new StringHttpContent(JsonSerializer.Serialize(dat), Encoding.UTF8, "application/scjson"))
.InitHeaders() .InitHeaders()
.SetUrl("/H") .SetUrl(DataReceived.ServerURL)
.SetHost(httpclient.RemoteIPHost.Host) .SetHost(httpclient.RemoteIPHost.Host)
.AsPost(); .AsPost();

2
Models/AppModels.cs

@ -73,6 +73,8 @@ namespace SunlightAggregationTerminal.Models
{ {
App.GlobalData.LogNo = false; App.GlobalData.LogNo = false;
// await WebSocketServer.WebSocketClient();
//发送登录请求 //发送登录请求
if (App.GlobalData.LocalAreaNetworkMode) if (App.GlobalData.LocalAreaNetworkMode)
{ {

69
WebSocketServer.cs

@ -0,0 +1,69 @@
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