修改websoket测试问题

master
mzhifa 1 year ago
parent 63fc8c0177
commit b65f5476c6

@ -6,8 +6,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Windows.Threading; using System.Windows.Threading;
using Txgy.FilesWatcher.model; using Txgy.FilesWatcher.model;
@ -21,15 +19,15 @@ using WebSocket4Net;
using System.IO; using System.IO;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using System.Text.Unicode; using System.Text.Unicode;
using static System.Windows.Forms.Design.AxImporter;
using System.Net.Sockets; using System.Net.Sockets;
using System.Security.Policy; using System.Security.Policy;
using Prism.Events;
namespace Txgy.FilesWatcher.ViewModels namespace Txgy.FilesWatcher.ViewModels
{ {
internal class MainViewModel : BindableBase internal class MainViewModel : BindableBase
{ {
public MainViewModel(WebsocketClient websocketClient) public MainViewModel(WebsocketClient websocketClient, IEventAggregator ea)
{ {
string filter = "*.txt"; string filter = "*.txt";
string str = File.ReadAllText("systemconfig.json"); string str = File.ReadAllText("systemconfig.json");
@ -38,6 +36,7 @@ namespace Txgy.FilesWatcher.ViewModels
InitializeParams(filter); InitializeParams(filter);
StartTime = DateTime.Now; StartTime = DateTime.Now;
this._websocketClient = websocketClient; this._websocketClient = websocketClient;
this._ea = ea;
_websocketClient.WebsocketError = WebSocket_Error; _websocketClient.WebsocketError = WebSocket_Error;
_websocketClient.WebSocketMessageReceived = WebSocket4Net_MessageReceived; _websocketClient.WebSocketMessageReceived = WebSocket4Net_MessageReceived;
_websocketClient.WebSocketInit(systemConfig.Url); _websocketClient.WebSocketInit(systemConfig.Url);
@ -61,6 +60,16 @@ namespace Txgy.FilesWatcher.ViewModels
//重新编码,解决中文乱码问题 //重新编码,解决中文乱码问题
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
}); });
if(user.code==200)
{
isLogin = true;
LoginContent = "登 出";
}
App.Current.Dispatcher.Invoke(() =>
{
HandyControl.Controls.MessageBox.Show(user.message, "warning");
});
this.HideLoading();
break; break;
case CSMessage.subscribe: case CSMessage.subscribe:
var sub= JsonSerializer.Deserialize<SCUserSubscribeMessage>(message, new JsonSerializerOptions var sub= JsonSerializer.Deserialize<SCUserSubscribeMessage>(message, new JsonSerializerOptions
@ -79,15 +88,22 @@ namespace Txgy.FilesWatcher.ViewModels
//重新编码,解决中文乱码问题 //重新编码,解决中文乱码问题
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
}); });
if (publish.serialNumber >= 0)
{
dataList[publish.serialNumber - 1].IsSend = true;
}
break; break;
default: default:
break; break;
} }
} }
} }
} }
void WebSocket_Error(SuperSocket.ClientEngine.ErrorEventArgs e) void WebSocket_Error(SuperSocket.ClientEngine.ErrorEventArgs e)
{ {
//出错后隐藏加载窗口
this.HideLoading();
Debug.WriteLine("websocket_Error:" + e.Exception.ToString()); Debug.WriteLine("websocket_Error:" + e.Exception.ToString());
} }
private string watcherPath; private string watcherPath;
@ -158,64 +174,33 @@ namespace Txgy.FilesWatcher.ViewModels
get { return password; } get { return password; }
set { SetProperty(ref password, value); } set { SetProperty(ref password, value); }
} }
public void InitializeParams(string fileFilter)
{
timer1.Interval = TimeSpan.FromSeconds(ProMonInterval);
IntervalTimesSource.Add(5);
IntervalTimesSource.Add(10);
IntervalTimesSource.Add(15);
IntervalTimesSource.Add(20);
IntervalTimesSource.Add(30);
IntervalTimesSource.Add(60);
SelectedIndex = 1;
//如果设置的目录不存在设置到根目录
if (!File.Exists(WatcherPath))
{
WatcherPath = AppDomain.CurrentDomain.BaseDirectory;
}
watcher = new FileSystemWatcher(WatcherPath);
//初始化监听
watcher.BeginInit();
//设置需要监听的更改类型(如:文件或者文件夹的属性,文件或者文件夹的创建时间;NotifyFilters枚举的内容)
watcher.NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.Security
| NotifyFilters.Size;
//设置监听的路径
watcher.Path = WatcherPath;
watcher.Changed += Watcher_Changed;
watcher.Created += Watcher_Created;
watcher.Deleted += Watcher_Deleted;
watcher.Renamed += Watcher_Renamed;
watcher.Error += OnError;
//设置监听文件类型 private string _loginContent="登 录";
watcher.Filter = fileFilter;
//设置是否监听子目录
// watcher.IncludeSubdirectories = false;
//设置是否启用监听
watcher.EnableRaisingEvents = false;
watcher.EndInit();
public string LoginContent
{
get { return _loginContent; }
set { SetProperty(ref _loginContent, value); }
} }
bool isLogin = false;
public DelegateCommand LoginCommand => new(() => { public DelegateCommand LoginCommand => new(() => {
if (!isLogin)
_websocketClient.SiginServer(Account, Password); {
this.ShowLoading();
_websocketClient.SiginServer(Account, Password);
}
else
{
isLogin = false;
LoginContent = "登 录";
_websocketClient.Closed();
}
}); });
public DelegateCommand StartCommand => new(Start); public DelegateCommand StartCommand => new(Start);
public DelegateCommand StopCommand => new(Stop); public DelegateCommand StopCommand => new(Stop);
private void Start() private void Start()
{ {
WatchStartOrSopt(true); WatchStartOrSopt(true);
@ -269,6 +254,52 @@ namespace Txgy.FilesWatcher.ViewModels
RunTime = DateDiff(DateTime.Now, StartTime); RunTime = DateDiff(DateTime.Now, StartTime);
} }
private void InitializeParams(string fileFilter)
{
timer1.Interval = TimeSpan.FromSeconds(ProMonInterval);
IntervalTimesSource.Add(5);
IntervalTimesSource.Add(10);
IntervalTimesSource.Add(15);
IntervalTimesSource.Add(20);
IntervalTimesSource.Add(30);
IntervalTimesSource.Add(60);
SelectedIndex = 1;
//如果设置的目录不存在设置到根目录
if (!File.Exists(WatcherPath))
{
WatcherPath = AppDomain.CurrentDomain.BaseDirectory;
}
watcher = new FileSystemWatcher();
//初始化监听
watcher.BeginInit();
//设置需要监听的更改类型(如:文件或者文件夹的属性,文件或者文件夹的创建时间;NotifyFilters枚举的内容)
watcher.NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.Security
| NotifyFilters.Size;
//设置监听的路径
watcher.Path = WatcherPath;
watcher.Changed += new FileSystemEventHandler(Watcher_Changed);
// watcher.Created += Watcher_Created;
watcher.Deleted += new FileSystemEventHandler(Watcher_Deleted);
watcher.Renamed += new RenamedEventHandler(Watcher_Renamed);
watcher.Error += OnError;
//设置监听文件类型
watcher.Filter = fileFilter;
//设置是否监听子目录
watcher.IncludeSubdirectories = false;
//设置是否启用监听
watcher.EnableRaisingEvents = false;
watcher.EndInit();
}
private string DateDiff(DateTime DateTime1, DateTime DateTime2) private string DateDiff(DateTime DateTime1, DateTime DateTime2)
{ {
string dateDiff = null; string dateDiff = null;
@ -296,95 +327,88 @@ namespace Txgy.FilesWatcher.ViewModels
{ {
} }
DateTime lastRead = DateTime.MinValue;
private void Watcher_Changed(object sender, System.IO.FileSystemEventArgs e) private void Watcher_Changed(object sender, System.IO.FileSystemEventArgs e)
{ {
// DateTime dt = DateTime.Now; // DateTime dt = DateTime.Now;
// string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n"; // string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n";
// watcher.EnableRaisingEvents = false;
DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath); DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath);
if (lastWriteTime != lastRead) watcher.EnableRaisingEvents = false;
Debug.WriteLine($"最后修改时间:{lastWriteTime}");
string lastLine = File.ReadAllLines(e.FullPath).Last();
System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{ {
lastRead = lastWriteTime; DataList.Add(new WatcherFileModel
string lastLine = File.ReadAllLines(e.FullPath).Last();
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{ {
DataList.Add(new WatcherFileModel CreateTime = DateTime.Now.ToString(),
{ ChangeType = e.ChangeType,
CreateTime = DateTime.Now.ToString(), Name = e.Name,
ChangeType = e.ChangeType, FullPath = e.FullPath,
Name = e.Name, Data = lastLine,
FullPath = e.FullPath, IsSend = false
Data = lastLine, });
IsSend = false
});
}));
CSDevicePublish cSDevicePublish = new CSDevicePublish() CSDevicePublish cSDevicePublish = new CSDevicePublish()
{ {
type = CSMessage.publish, type = CSMessage.publish,
message = lastLine, message = lastLine,
serialNumber = DataList.Count,
}; };
string jsonStr = JsonSerializer.Serialize(cSDevicePublish); string jsonStr = JsonSerializer.Serialize(cSDevicePublish);
_websocketClient.SendMes(jsonStr); _websocketClient.SendMes(jsonStr);
} }));
watcher.EnableRaisingEvents = true;
} }
private void Watcher_Renamed(object sender, RenamedEventArgs e) private void Watcher_Renamed(object sender, RenamedEventArgs e)
{ {
Application.Current.Dispatcher.BeginInvoke(new Action(() => { System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() => {
DateTime dt = DateTime.Now; DateTime dt = DateTime.Now;
string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n"; string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n";
tmp += "改变类型 :" + e.ChangeType.ToString() + "\r\n"; ; tmp += "改变类型 :" + e.ChangeType.ToString() + "\r\n"; ;
tmp += "文件全称:" + e.FullPath + "\r\n"; tmp += "文件全称:" + e.FullPath + "\r\n";
DataList.Add(new WatcherFileModel //DataList.Add(new WatcherFileModel
{ //{
CreateTime = DateTime.Now.ToString(), // CreateTime = DateTime.Now.ToString(),
ChangeType = e.ChangeType, // ChangeType = e.ChangeType,
Name = e.Name, // Name = e.Name,
FullPath = e.FullPath, // FullPath = e.FullPath,
Data = tmp // Data = tmp
}); //});
})); }));
} }
private void Watcher_Created(object sender, FileSystemEventArgs e) private void Watcher_Created(object sender, FileSystemEventArgs e)
{ {
Application.Current.Dispatcher.BeginInvoke(new Action(() => { DateTime dt = DateTime.Now;
string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n";
DateTime dt = DateTime.Now; tmp += "改变类型 :" + e.ChangeType.ToString() + "\r\n"; ;
string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n"; tmp += "文件全称:" + e.FullPath + "\r\n";
tmp += "改变类型 :" + e.ChangeType.ToString() + "\r\n"; ; //DataList.Add(new WatcherFileModel
tmp += "文件全称:" + e.FullPath + "\r\n"; //{
DataList.Add(new WatcherFileModel // CreateTime = DateTime.Now.ToString(),
{ // ChangeType = e.ChangeType,
CreateTime = DateTime.Now.ToString(), // Name = e.Name,
ChangeType = e.ChangeType, // FullPath = e.FullPath,
Name = e.Name, // Data = tmp
FullPath = e.FullPath, //});
Data = tmp
});
}));
} }
private void Watcher_Deleted(object sender, FileSystemEventArgs e) private void Watcher_Deleted(object sender, FileSystemEventArgs e)
{ {
Application.Current.Dispatcher.BeginInvoke(new Action(() => {
DateTime dt = DateTime.Now; DateTime dt = DateTime.Now;
string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n"; string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n";
tmp += "改变类型 :" + e.ChangeType.ToString() + "\r\n"; ; tmp += "改变类型 :" + e.ChangeType.ToString() + "\r\n"; ;
tmp += "文件全称:" + e.FullPath + "\r\n"; tmp += "文件全称:" + e.FullPath + "\r\n";
DataList.Add(new WatcherFileModel // DataList.Add(new WatcherFileModel
{ //{
CreateTime= DateTime.Now.ToString(), // CreateTime= DateTime.Now.ToString(),
ChangeType=e.ChangeType, // ChangeType=e.ChangeType,
Name=e.Name, // Name=e.Name,
FullPath= e.FullPath, // FullPath= e.FullPath,
Data=tmp // Data=tmp
}); //});
}));
} }
/// <summary> /// <summary>
@ -400,5 +424,15 @@ namespace Txgy.FilesWatcher.ViewModels
private DispatcherTimer timer1 = new DispatcherTimer(); private DispatcherTimer timer1 = new DispatcherTimer();
private SystemConfig systemConfig = new SystemConfig(); private SystemConfig systemConfig = new SystemConfig();
private readonly WebsocketClient _websocketClient; private readonly WebsocketClient _websocketClient;
private readonly IEventAggregator _ea;
protected void ShowLoading(string tip = "正在加载....")
{
_ea?.GetEvent<LoadingEvent>().Publish(new LoadingPayload { IsShow = true, Message = tip });
}
protected void HideLoading()
{
_ea?.GetEvent<LoadingEvent>().Publish(new LoadingPayload { IsShow = false });
}
} }
} }

@ -1,19 +1,45 @@
using Prism.Mvvm; using Prism.Events;
using Prism.Mvvm;
using Txgy.FilesWatcher.model;
namespace Txgy.FilesWatcher.ViewModels namespace Txgy.FilesWatcher.ViewModels
{ {
public class MainWindowViewModel : BindableBase public class MainWindowViewModel : BindableBase
{ {
public MainWindowViewModel(IEventAggregator ea)
{
this._ea = ea;
_ea.GetEvent<LoadingEvent>().Subscribe(OnShowLoading, ThreadOption.UIThread);
}
private string _title = "Application"; private string _title = "Application";
private readonly IEventAggregator _ea;
public string Title public string Title
{ {
get { return _title; } get { return _title; }
set { SetProperty(ref _title, value); } set { SetProperty(ref _title, value); }
} }
private string isLoadingVisibile = "Hidden";
public string IsLoadingVisibile
{
get { return isLoadingVisibile; }
set { SetProperty(ref isLoadingVisibile, value); }
}
private string _loadingMessage;
public MainWindowViewModel() public string LoadingMessage
{ {
get { return _loadingMessage; }
set { SetProperty(ref _loadingMessage, value); }
}
private void OnShowLoading(LoadingPayload msg)
{
this.IsLoadingVisibile = msg.IsShow? "Visible" : "Hidden";
this.LoadingMessage = msg.Message;
} }
} }
} }

@ -0,0 +1,149 @@
<UserControl x:Class="Txgy.FilesWatcher.Views.Loading"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="100" d:DesignWidth="100">
<UserControl.Resources>
<KeySpline x:Key="keySpline">0.1,0.4,0.9,0.6</KeySpline>
<Storyboard x:Key="keyFrameStoryboard1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="e1" Storyboard.TargetProperty="Opacity" RepeatBehavior="Forever">
<DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:5" Value="0"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:6" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="rt1" Storyboard.TargetProperty="Angle" RepeatBehavior="Forever">
<SplineDoubleKeyFrame KeyTime="0:0:0.3" Value="0" KeySpline="{StaticResource keySpline}"/>
<SplineDoubleKeyFrame KeyTime="0:0:2.5" Value="360" KeySpline="{StaticResource keySpline}"/>
<SplineDoubleKeyFrame KeyTime="0:0:5" Value="720" KeySpline="{StaticResource keySpline}"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:6" Value="720"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="e2" Storyboard.TargetProperty="Opacity" BeginTime="0:0:0.2" RepeatBehavior="Forever">
<DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:5" Value="0"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:6" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="rt2" Storyboard.TargetProperty="Angle" BeginTime="0:0:0.2" RepeatBehavior="Forever">
<SplineDoubleKeyFrame KeyTime="0:0:0.3" Value="0" KeySpline="{StaticResource keySpline}"/>
<SplineDoubleKeyFrame KeyTime="0:0:2.5" Value="360" KeySpline="{StaticResource keySpline}"/>
<SplineDoubleKeyFrame KeyTime="0:0:5" Value="720" KeySpline="{StaticResource keySpline}"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:6" Value="720"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="e3" Storyboard.TargetProperty="Opacity" BeginTime="0:0:0.4" RepeatBehavior="Forever">
<DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:5" Value="0"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:6" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="rt3" Storyboard.TargetProperty="Angle" BeginTime="0:0:0.4" RepeatBehavior="Forever">
<SplineDoubleKeyFrame KeyTime="0:0:0.3" Value="0" KeySpline="{StaticResource keySpline}"/>
<SplineDoubleKeyFrame KeyTime="0:0:2.5" Value="360" KeySpline="{StaticResource keySpline}"/>
<SplineDoubleKeyFrame KeyTime="0:0:5" Value="720" KeySpline="{StaticResource keySpline}"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:6" Value="720"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="e4" Storyboard.TargetProperty="Opacity" BeginTime="0:0:0.6" RepeatBehavior="Forever">
<DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:5" Value="0"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:6" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="rt4" Storyboard.TargetProperty="Angle" BeginTime="0:0:0.6" RepeatBehavior="Forever">
<SplineDoubleKeyFrame KeyTime="0:0:0.3" Value="0" KeySpline="{StaticResource keySpline}"/>
<SplineDoubleKeyFrame KeyTime="0:0:2.5" Value="360" KeySpline="{StaticResource keySpline}"/>
<SplineDoubleKeyFrame KeyTime="0:0:5" Value="720" KeySpline="{StaticResource keySpline}"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:6" Value="720"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="e5" Storyboard.TargetProperty="Opacity" BeginTime="0:0:0.8" RepeatBehavior="Forever">
<DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:5" Value="0"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:6" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="rt5" Storyboard.TargetProperty="Angle" BeginTime="0:0:0.8" RepeatBehavior="Forever">
<SplineDoubleKeyFrame KeyTime="0:0:0.3" Value="0" KeySpline="{StaticResource keySpline}"/>
<SplineDoubleKeyFrame KeyTime="0:0:2.5" Value="360" KeySpline="{StaticResource keySpline}"/>
<SplineDoubleKeyFrame KeyTime="0:0:5" Value="720" KeySpline="{StaticResource keySpline}"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:6" Value="720"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="e6" Storyboard.TargetProperty="Opacity" BeginTime="0:0:1" RepeatBehavior="Forever">
<DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:5" Value="0"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:6" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="rt6" Storyboard.TargetProperty="Angle" BeginTime="0:0:1" RepeatBehavior="Forever">
<SplineDoubleKeyFrame KeyTime="0:0:0.3" Value="0" KeySpline="{StaticResource keySpline}"/>
<SplineDoubleKeyFrame KeyTime="0:0:2.5" Value="360" KeySpline="{StaticResource keySpline}"/>
<SplineDoubleKeyFrame KeyTime="0:0:5" Value="720" KeySpline="{StaticResource keySpline}"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:6" Value="720"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<UserControl.Triggers>
<EventTrigger RoutedEvent="UserControl.Loaded">
<BeginStoryboard Storyboard="{StaticResource keyFrameStoryboard1}"/>
</EventTrigger>
</UserControl.Triggers>
<Grid Height="140">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid Height="70" Width="70">
<Border RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<RotateTransform Angle="0" x:Name="rt1"/>
</Border.RenderTransform>
<Ellipse Fill="Orange" Width="8" Height="8" VerticalAlignment="Bottom" Name="e1"/>
</Border>
<Border RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<RotateTransform Angle="0" x:Name="rt2"/>
</Border.RenderTransform>
<Ellipse Fill="Orange" Width="8" Height="8" VerticalAlignment="Bottom" Name="e2"/>
</Border>
<Border RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<RotateTransform Angle="0" x:Name="rt3"/>
</Border.RenderTransform>
<Ellipse Fill="Orange" Width="8" Height="8" VerticalAlignment="Bottom" Name="e3"/>
</Border>
<Border RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<RotateTransform Angle="0" x:Name="rt4"/>
</Border.RenderTransform>
<Ellipse Fill="Orange" Width="8" Height="8" VerticalAlignment="Bottom" Name="e4"/>
</Border>
<Border RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<RotateTransform Angle="0" x:Name="rt5"/>
</Border.RenderTransform>
<Ellipse Fill="Orange" Width="8" Height="8" VerticalAlignment="Bottom" Name="e5"/>
</Border>
<Border RenderTransformOrigin="0.5,0.5">
<Border.RenderTransform>
<RotateTransform Angle="0" x:Name="rt6"/>
</Border.RenderTransform>
<Ellipse Fill="Orange" Width="8" Height="8" VerticalAlignment="Bottom" Name="e6"/>
</Border>
</Grid>
<TextBlock Text="{Binding Message,RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor}}"
Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#333"
Margin="0,10"/>
<TextBlock Grid.Row="2" Margin="0,10" Foreground="Orange" VerticalAlignment="Center" HorizontalAlignment="Center"
Visibility="Collapsed">
<Hyperlink Command="{Binding CancelLoadingCommand}">取消</Hyperlink>
</TextBlock>
</Grid>
</UserControl>

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Txgy.FilesWatcher.Views
{
/// <summary>
/// Loading.xaml 的交互逻辑
/// </summary>
public partial class Loading : UserControl
{
public string Message
{
get { return (string)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
public static readonly DependencyProperty MessageProperty =
DependencyProperty.Register("Message", typeof(string), typeof(Loading), new PropertyMetadata("正在处理"));
public Loading()
{
InitializeComponent();
}
}
}

@ -47,7 +47,7 @@
<TextBlock Text="密 码:" VerticalAlignment="Center"/> <TextBlock Text="密 码:" VerticalAlignment="Center"/>
<hc:PasswordBox UnsafePassword="{Binding Password,UpdateSourceTrigger=PropertyChanged}" IsSafeEnabled="False" ShowClearButton="True" ShowEyeButton="True" DockPanel.Dock="Right"/> <hc:PasswordBox UnsafePassword="{Binding Password,UpdateSourceTrigger=PropertyChanged}" IsSafeEnabled="False" ShowClearButton="True" ShowEyeButton="True" DockPanel.Dock="Right"/>
</DockPanel> </DockPanel>
<Button Grid.Row="2" Content="登录" Command="{Binding LoginCommand}"/> <Button Grid.Row="2" Content="{Binding LoginContent}" Command="{Binding LoginCommand}"/>
</Grid> </Grid>
<RadioButton GroupName="start" Content="启动" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center"> <RadioButton GroupName="start" Content="启动" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center">
<i:Interaction.Triggers> <i:Interaction.Triggers>
@ -98,7 +98,7 @@
<!--模板化单元格内容--> <!--模板化单元格内容-->
<GridViewColumn.CellTemplate> <GridViewColumn.CellTemplate>
<DataTemplate> <DataTemplate>
<TextBox Foreground="CornflowerBlue" MinWidth="200" Cursor="Hand" Text="{Binding Data}" IsReadOnly="True" BorderBrush="Transparent" Background="Transparent"></TextBox> <TextBox Foreground="CornflowerBlue" MinWidth="200" Cursor="Hand" Text="{Binding Data}" IsReadOnly="True" BorderBrush="Transparent" Background="Transparent" TextWrapping="Wrap"></TextBox>
</DataTemplate> </DataTemplate>
</GridViewColumn.CellTemplate> </GridViewColumn.CellTemplate>
</GridViewColumn> </GridViewColumn>

@ -2,9 +2,13 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/" xmlns:prism="http://prismlibrary.com/"
xmlns:local="clr-namespace:Txgy.FilesWatcher.Views"
prism:ViewModelLocator.AutoWireViewModel="True" prism:ViewModelLocator.AutoWireViewModel="True"
Title="{Binding Title}" Height="500" Width="960" > Title="{Binding Title}" Height="500" Width="960" >
<Grid> <Grid>
<ContentControl prism:RegionManager.RegionName="MainContentRegion" /> <ContentControl prism:RegionManager.RegionName="MainContentRegion" />
<Grid Background="WhiteSmoke" Opacity="0.5" Visibility="{Binding IsLoadingVisibile}">
<local:Loading Message="{Binding LoadingMessage}"/>
</Grid>
</Grid> </Grid>
</Window> </Window>

@ -26,46 +26,55 @@ namespace Txgy.FilesWatcher
webSocket4Net.Error += new EventHandler<ErrorEventArgs>(WebSocket_Error); webSocket4Net.Error += new EventHandler<ErrorEventArgs>(WebSocket_Error);
webSocket4Net.MessageReceived += WebSocket4Net_MessageReceived; webSocket4Net.MessageReceived += WebSocket4Net_MessageReceived;
webSocket4Net.Open(); webSocket4Net.Open();
Debug.WriteLine("客户端连接成功!"); Debug.WriteLine("客户端连接成功!");
Thread thread = new Thread(SendHeartbeat);
thread.IsBackground = true; // thread.Start();
thread.Start();
} }
public void SiginServer(string account,string password) public void SiginServer(string account,string password)
{ {
isHeartbeat = false;
string jsonstr= JsonSerializer.Serialize(new CSUserSigin { type = CSMessage.sigin, utype = "device", account=account,password=password}); string jsonstr= JsonSerializer.Serialize(new CSUserSigin { type = CSMessage.sigin, utype = "device", account=account,password=password});
SendMes(jsonstr); SendMes(jsonstr);
Task.Run(async () =>{
await Task.Delay(2000);
isHeartbeat = true;
SendHeartbeat();
});
} }
public void Closed()
{
isHeartbeat = false;
}
public void SendMes(string message) public void SendMes(string message)
{ {
webSocket4Net.Send(message); webSocket4Net.Send(message);
} }
bool isHeartbeat = false;
private void SendHeartbeat() private void SendHeartbeat()
{ {
while (true) while (isHeartbeat)
{ {
//Console.WriteLine($"客户端发送数据{i++}"); //Console.WriteLine($"客户端发送数据{i++}");
Thread.Sleep(TimeSpan.FromSeconds(1));
if (webSocket4Net.State == WebSocketState.Open) if (webSocket4Net.State == WebSocketState.Open)
{ {
webSocket4Net.Send("{\"type\":\"heartbeat\",\"utype\":\"device\",\"uid\":123}"); webSocket4Net.Send("{\"type\":\"heartbeat\",\"utype\":\"device\"}");
} }
else if (webSocket4Net.State == WebSocketState.Closed) else if (webSocket4Net.State == WebSocketState.Closed)
{ {
Thread.Sleep(TimeSpan.FromSeconds(10)); Thread.Sleep(TimeSpan.FromSeconds(10));
webSocket4Net.Open(); webSocket4Net.Open();
} }
Thread.Sleep(TimeSpan.FromSeconds(5));
} }
} }
private void WebSocket4Net_MessageReceived(object sender, MessageReceivedEventArgs e) private void WebSocket4Net_MessageReceived(object sender, MessageReceivedEventArgs e)
{ {
WebSocketMessageReceived?.Invoke(e.Message); WebSocketMessageReceived?.Invoke(e.Message);
Debug.WriteLine($"服务端回复数据:{e.Message}");
} }
private void WebSocket4Net_Opened(object sender, EventArgs e) private void WebSocket4Net_Opened(object sender, EventArgs e)

@ -0,0 +1,16 @@
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Txgy.FilesWatcher.model
{
public class LoadingPayload
{
public bool IsShow { get; set; }
public string Message { get; set; }
}
public class LoadingEvent : PubSubEvent<LoadingPayload> { }
}

@ -46,12 +46,13 @@ namespace Txgy.FilesWatcher.model
{ {
public string type { get; set; } public string type { get; set; }
public string message { get; set; } public string message { get; set; }
public int uid { get; set; } public int serialNumber { get; set; }
} }
public class SCDevicePublish public class SCDevicePublish
{ {
public string type { get; set; } public string type { get; set; }
public int code { get; set; } public int code { get; set; }
public string message { get; set; } public string message { get; set; }
public int serialNumber { get; set; }
} }
} }

Loading…
Cancel
Save