修改websoket测试问题

master
mzhifa 1 year ago
parent 63fc8c0177
commit b65f5476c6

@ -6,8 +6,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using System.Collections.ObjectModel;
using System.Windows.Threading;
using Txgy.FilesWatcher.model;
@ -21,15 +19,15 @@ using WebSocket4Net;
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using static System.Windows.Forms.Design.AxImporter;
using System.Net.Sockets;
using System.Security.Policy;
using Prism.Events;
namespace Txgy.FilesWatcher.ViewModels
{
internal class MainViewModel : BindableBase
{
public MainViewModel(WebsocketClient websocketClient)
public MainViewModel(WebsocketClient websocketClient, IEventAggregator ea)
{
string filter = "*.txt";
string str = File.ReadAllText("systemconfig.json");
@ -38,6 +36,7 @@ namespace Txgy.FilesWatcher.ViewModels
InitializeParams(filter);
StartTime = DateTime.Now;
this._websocketClient = websocketClient;
this._ea = ea;
_websocketClient.WebsocketError = WebSocket_Error;
_websocketClient.WebSocketMessageReceived = WebSocket4Net_MessageReceived;
_websocketClient.WebSocketInit(systemConfig.Url);
@ -61,6 +60,16 @@ namespace Txgy.FilesWatcher.ViewModels
//重新编码,解决中文乱码问题
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;
case CSMessage.subscribe:
var sub= JsonSerializer.Deserialize<SCUserSubscribeMessage>(message, new JsonSerializerOptions
@ -79,15 +88,22 @@ namespace Txgy.FilesWatcher.ViewModels
//重新编码,解决中文乱码问题
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
});
if (publish.serialNumber >= 0)
{
dataList[publish.serialNumber - 1].IsSend = true;
}
break;
default:
break;
}
}
}
}
void WebSocket_Error(SuperSocket.ClientEngine.ErrorEventArgs e)
{
//出错后隐藏加载窗口
this.HideLoading();
Debug.WriteLine("websocket_Error:" + e.Exception.ToString());
}
private string watcherPath;
@ -158,64 +174,33 @@ namespace Txgy.FilesWatcher.ViewModels
get { return password; }
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;
//设置监听文件类型
watcher.Filter = fileFilter;
//设置是否监听子目录
// watcher.IncludeSubdirectories = false;
//设置是否启用监听
watcher.EnableRaisingEvents = false;
watcher.EndInit();
private string _loginContent="登 录";
public string LoginContent
{
get { return _loginContent; }
set { SetProperty(ref _loginContent, value); }
}
bool isLogin = false;
public DelegateCommand LoginCommand => new(() => {
if (!isLogin)
{
this.ShowLoading();
_websocketClient.SiginServer(Account, Password);
}
else
{
isLogin = false;
LoginContent = "登 录";
_websocketClient.Closed();
}
});
public DelegateCommand StartCommand => new(Start);
public DelegateCommand StopCommand => new(Stop);
private void Start()
{
WatchStartOrSopt(true);
@ -269,6 +254,52 @@ namespace Txgy.FilesWatcher.ViewModels
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)
{
string dateDiff = null;
@ -296,18 +327,15 @@ namespace Txgy.FilesWatcher.ViewModels
{
}
DateTime lastRead = DateTime.MinValue;
private void Watcher_Changed(object sender, System.IO.FileSystemEventArgs e)
{
// DateTime dt = DateTime.Now;
// 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);
if (lastWriteTime != lastRead)
{
lastRead = lastWriteTime;
watcher.EnableRaisingEvents = false;
Debug.WriteLine($"最后修改时间:{lastWriteTime}");
string lastLine = File.ReadAllLines(e.FullPath).Last();
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
DataList.Add(new WatcherFileModel
{
@ -319,72 +347,68 @@ namespace Txgy.FilesWatcher.ViewModels
IsSend = false
});
}));
CSDevicePublish cSDevicePublish = new CSDevicePublish()
{
type = CSMessage.publish,
message = lastLine,
serialNumber = DataList.Count,
};
string jsonStr = JsonSerializer.Serialize(cSDevicePublish);
_websocketClient.SendMes(jsonStr);
}
}));
watcher.EnableRaisingEvents = true;
}
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;
string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n";
tmp += "改变类型 :" + e.ChangeType.ToString() + "\r\n"; ;
tmp += "文件全称:" + e.FullPath + "\r\n";
DataList.Add(new WatcherFileModel
{
CreateTime = DateTime.Now.ToString(),
ChangeType = e.ChangeType,
Name = e.Name,
FullPath = e.FullPath,
Data = tmp
});
//DataList.Add(new WatcherFileModel
//{
// CreateTime = DateTime.Now.ToString(),
// ChangeType = e.ChangeType,
// Name = e.Name,
// FullPath = e.FullPath,
// Data = tmp
//});
}));
}
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";
tmp += "改变类型 :" + e.ChangeType.ToString() + "\r\n"; ;
tmp += "文件全称:" + e.FullPath + "\r\n";
DataList.Add(new WatcherFileModel
{
CreateTime = DateTime.Now.ToString(),
ChangeType = e.ChangeType,
Name = e.Name,
FullPath = e.FullPath,
Data = tmp
});
}));
//DataList.Add(new WatcherFileModel
//{
// CreateTime = DateTime.Now.ToString(),
// ChangeType = e.ChangeType,
// Name = e.Name,
// FullPath = e.FullPath,
// Data = tmp
//});
}
private void Watcher_Deleted(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";
tmp += "改变类型 :" + e.ChangeType.ToString() + "\r\n"; ;
tmp += "文件全称:" + e.FullPath + "\r\n";
DataList.Add(new WatcherFileModel
{
CreateTime= DateTime.Now.ToString(),
ChangeType=e.ChangeType,
Name=e.Name,
FullPath= e.FullPath,
Data=tmp
});
}));
// DataList.Add(new WatcherFileModel
//{
// CreateTime= DateTime.Now.ToString(),
// ChangeType=e.ChangeType,
// Name=e.Name,
// FullPath= e.FullPath,
// Data=tmp
//});
}
/// <summary>
@ -400,5 +424,15 @@ namespace Txgy.FilesWatcher.ViewModels
private DispatcherTimer timer1 = new DispatcherTimer();
private SystemConfig systemConfig = new SystemConfig();
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
{
public class MainWindowViewModel : BindableBase
{
public MainWindowViewModel(IEventAggregator ea)
{
this._ea = ea;
_ea.GetEvent<LoadingEvent>().Subscribe(OnShowLoading, ThreadOption.UIThread);
}
private string _title = "Application";
private readonly IEventAggregator _ea;
public string Title
{
get { return _title; }
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"/>
<hc:PasswordBox UnsafePassword="{Binding Password,UpdateSourceTrigger=PropertyChanged}" IsSafeEnabled="False" ShowClearButton="True" ShowEyeButton="True" DockPanel.Dock="Right"/>
</DockPanel>
<Button Grid.Row="2" Content="登录" Command="{Binding LoginCommand}"/>
<Button Grid.Row="2" Content="{Binding LoginContent}" Command="{Binding LoginCommand}"/>
</Grid>
<RadioButton GroupName="start" Content="启动" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center">
<i:Interaction.Triggers>
@ -98,7 +98,7 @@
<!--模板化单元格内容-->
<GridViewColumn.CellTemplate>
<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>
</GridViewColumn.CellTemplate>
</GridViewColumn>

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

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

Loading…
Cancel
Save