.net MVC前后端通信—SingleR,文件监控FileSystemWatcher
来源:互联网 发布:靠谱代购淘宝店 知乎 编辑:程序博客网 时间:2024/06/11 02:43
在这里介绍一个通过文件监视FileSystemWatcher和SingleR实现对文件的监控提示。
1、首先利用Nuget安装SingleR,
2、安装完SingleR之后,在项目中会多出来SingleR的dll文件和两个JS文件
3、修改Startup类,(若项目中没有Startup类,通过搜索Startup添加)
4、添加chatHub类:
public class ChatHub : Hub { private string filePath = AppDomain.CurrentDomain.BaseDirectory; /// <summary> /// 发送信息给所有用户 /// </summary> /// <param name="message"></param> public void Hello(string message) { Clients.All.hello(message); } public void watcherTxt() { WatcherStrat(filePath, "file.txt"); } private void WatcherStrat(string path, string filter)//监控文件 { FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = path; watcher.Filter = filter; watcher.Changed += new FileSystemEventHandler(OnProcess); watcher.Created += new FileSystemEventHandler(OnProcess); watcher.Deleted += new FileSystemEventHandler(OnProcess); watcher.Renamed += new RenamedEventHandler(OnRenamed); watcher.EnableRaisingEvents = true; watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size; watcher.IncludeSubdirectories = true; } public void OnProcess(object source, FileSystemEventArgs e) { if (e.ChangeType == WatcherChangeTypes.Created) { Clients.All.hello("Created"); } else if (e.ChangeType == WatcherChangeTypes.Changed) { Clients.All.hello("Changed"); } else if (e.ChangeType == WatcherChangeTypes.Deleted) {Clients.All.hello("Deleted"); } } private void OnRenamed(object source, RenamedEventArgs e) {Clients.All.hello("Renamed"); }}
5、修改view页面,实现前后交互:
<script src="~/Scripts/jquery.signalR-2.2.2.min.js"></script> <script src="~/signalr/js"></script> $(function () { //创建一个hub服务 var hub = $.connection.chatHub; $.connection.hub.start() .done(function () { hub.server.watcherTxt();//进入后台方法 }) .fail(function () { alert("连接失败!"); }); hub.client.hello = function (name) {//前台hello方法 alert(name); }; $("#sayHello").click(function () { hub.server.hello("Jack");//向后台发送数据 }) });
阅读全文
0 0
- .net MVC前后端通信—SingleR,文件监控FileSystemWatcher
- FileSystemWatcher文件监控
- 使用.NET FileSystemWatcher对象监控磁盘文件目录的改变
- C# FileSystemWatcher监控指定文件
- 使用FileSystemWatcher监控文件变化
- powershell FileSystemWatcher监控文件变化
- C# FileSystemWatcher监控指定文件
- 前后端基本通信
- .NET FileSystemWatcher对象监控C#目录改变
- C#中FileSystemWatcher控件实现文件监控
- 使用windows服务和.NET FileSystemWatcher对象来监控磁盘文件目录的改变
- SingleR
- MVC到前后端分离
- MVC与前后端分离
- 使用.NET FileSystemWatcher对象监控C#目录改变
- 谈谈C#文件监控对象FileSystemWatcher使用感受
- 谈谈C#文件监控对象FileSystemWatcher使用感受
- C#利用FileSystemWatcher监控对文件的操作
- 解决supervisord进程导致的队列时差问题
- cocos内存管理机制
- SDN开发笔记:md-sal应用程序开发
- springmvc 从入门到总结
- Spring+SpringMVC+hibernate spatial+oracle spatial 映射存储Geomety空间数据过程记录
- .net MVC前后端通信—SingleR,文件监控FileSystemWatcher
- 第一课:慕课网Vue.js音乐播放器
- 剑指offer 从上往下打印二叉树
- MySQL之进行行列转换
- Android TextView使用HTML处理字体样式、显示图片等
- LeetCode 191. Number of 1 Bits
- .net 实现http下载
- Python基础入门之List和Tuple类型三
- Codeforces790B. Bear and Tree Jumps 【树型DP】