修改测试

master
mzhifa 1 year ago
parent 345b4d970b
commit 63fc8c0177

@ -21,6 +21,9 @@ 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;
namespace Txgy.FilesWatcher.ViewModels
{
@ -42,9 +45,45 @@ namespace Txgy.FilesWatcher.ViewModels
private void WebSocket4Net_MessageReceived(string message)
{
Debug.WriteLine($"服务端回复数据:{message}");
if(message == "sigin")
using (JsonDocument document = JsonDocument.Parse(message))
{
MessageBox.Show("登录成功");
JsonElement root = document.RootElement;
if (root.TryGetProperty("type", out JsonElement dataElement))
{
string type = dataElement.ToString();
switch (type)
{
case CSMessage.sigin:
var user= JsonSerializer.Deserialize<SCUserSigin>(message, new JsonSerializerOptions
{
// 整齐打印
WriteIndented = true,
//重新编码,解决中文乱码问题
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
});
break;
case CSMessage.subscribe:
var sub= JsonSerializer.Deserialize<SCUserSubscribeMessage>(message, new JsonSerializerOptions
{
// 整齐打印
WriteIndented = true,
//重新编码,解决中文乱码问题
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
});
break;
case CSMessage.publish:
var publish = JsonSerializer.Deserialize<SCDevicePublish>(message, new JsonSerializerOptions
{
// 整齐打印
WriteIndented = true,
//重新编码,解决中文乱码问题
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
});
break;
default:
break;
}
}
}
}
void WebSocket_Error(SuperSocket.ClientEngine.ErrorEventArgs e)
@ -105,14 +144,14 @@ namespace Txgy.FilesWatcher.ViewModels
get { return isIndeterminate; }
set { SetProperty(ref isIndeterminate, value); }
}
private string account;
private string account="admin";
public string Account
{
get { return account; }
set { SetProperty(ref account, value); }
}
private string password;
private string password="123";
public string Password
{
@ -158,9 +197,9 @@ namespace Txgy.FilesWatcher.ViewModels
//设置监听文件类型
watcher.Filter = fileFilter;
//设置是否监听子目录
watcher.IncludeSubdirectories = true;
// watcher.IncludeSubdirectories = false;
//设置是否启用监听
// watcher.EnableRaisingEvents = false;
watcher.EnableRaisingEvents = false;
watcher.EndInit();
}
@ -185,6 +224,7 @@ namespace Txgy.FilesWatcher.ViewModels
timer1.Start();
timer1.Tick += timer1_Tick;
IsIndeterminate = true;
_websocketClient.SendMes(JsonSerializer.Serialize(new CSUserSubscribeMessage { type = CSMessage.subscribe }));
}
private void Stop()
@ -192,6 +232,8 @@ namespace Txgy.FilesWatcher.ViewModels
WatchStartOrSopt(false);
timer1.Stop();
IsIndeterminate = false;
_websocketClient.SendMes(JsonSerializer.Serialize(new CSUserSubscribeMessage { type = CSMessage.unsubscribe }));
}
public DelegateCommand FilePathSaveCommand => new(() =>
@ -254,14 +296,19 @@ namespace Txgy.FilesWatcher.ViewModels
{
}
DateTime lastRead = DateTime.MinValue;
private void Watcher_Changed(object sender, System.IO.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;
// 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;
string lastLine = File.ReadAllLines(e.FullPath).Last();
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
DataList.Add(new WatcherFileModel
{
CreateTime = DateTime.Now.ToString(),
@ -270,8 +317,9 @@ namespace Txgy.FilesWatcher.ViewModels
FullPath = e.FullPath,
Data = lastLine,
IsSend = false
}) ;
});
}));
CSDevicePublish cSDevicePublish = new CSDevicePublish()
{
type = CSMessage.publish,
@ -279,7 +327,7 @@ namespace Txgy.FilesWatcher.ViewModels
};
string jsonStr = JsonSerializer.Serialize(cSDevicePublish);
_websocketClient.SendMes(jsonStr);
}));
}
}
private void Watcher_Renamed(object sender, RenamedEventArgs e)

@ -34,7 +34,7 @@ namespace Txgy.FilesWatcher
}
public void SiginServer(string account,string password)
{
string jsonstr= JsonSerializer.Serialize(new CSUserSigin { type = CSMessage.sigin, utpye = "device", account=account,password=password});
string jsonstr= JsonSerializer.Serialize(new CSUserSigin { type = CSMessage.sigin, utype = "device", account=account,password=password});
SendMes(jsonstr);
}

@ -8,7 +8,7 @@ namespace Txgy.FilesWatcher.model
public class CSUserSigin
{
public string type { get; set; }
public string utpye { get; set; }
public string utype { get; set; }
/// <summary>
/// 用户id
/// </summary>
@ -27,7 +27,7 @@ namespace Txgy.FilesWatcher.model
public string type { get; set; }
public string message { get; set; }
public int uid { get; set; }
public string code { get; set; }
public int code { get; set; }
}
public class CSUserSubscribeMessage
@ -39,7 +39,7 @@ namespace Txgy.FilesWatcher.model
public class SCUserSubscribeMessage
{
public string type { get; set; }
public string code { get; set; }
public int code { get; set; }
public int uid { get; set; }
}
public class CSDevicePublish
@ -51,7 +51,7 @@ namespace Txgy.FilesWatcher.model
public class SCDevicePublish
{
public string type { get; set; }
public string code { get; set; }
public int code { get; set; }
public string message { get; set; }
}
}

Loading…
Cancel
Save