Windows file Watch Nodejs

Source: Internet
Author: User

This blog, is mainly to record the recent has been tangled in the Gulp.watch method, in the work we must have encountered, the newly added files can not automatically trigger watch, below we see what way to deal with

1. First of all we must first Baidu a bit

Baidu recommended is Gulp-watch plug-in, this plugin is actually based on the blog mentioned in the node module Chokidar. can monitor file additions and deletions, renamed will send an add and a delete

But with this module, when the console is closed, there will be a period of non-response time, feel the importance of strange

2. I usually under Windows, will also be familiar with writing Arrdio scripting language, the language of the file monitoring, there are related modules, in fact, the relative use of Kernel32.dll inside the Windows API

READDIRECTORYCHANGESW MSDN Train

But Arrdio EXE program how to communicate with the NODEJS program, there have been related attempts before,

A. Through the process input, output device-related, printing information. The background finds that the scenario can only be discarded when the child process returns information to the main process.

B. Based on HTTP, the main feeling is to occupy the port, and HTTP is responsive, the related interaction is also a bit troublesome

C. Based on the TCP,TCP is a long connection, the server and client can communicate with each other, this is a good meal toss (TCP service is node or Arrdio created, later is to consider Arrdio creation, mainly in the MyNote project, but also with the help of TCP services, Do let node in Windows use SQLite database "mainly online did not find the relevant Windows compiled SQLite module, without the relevant environment and knowledge")

Summary of the relevant comparison

Gulp.watch Chokidar Aau.watch
Initialization Before the ready event there will be an add event for the file
File additions No response Add Add File
deleting files

Deleted that existed before running

No response added after the run

Unlink Remove files from
Renaming files

Deleted that existed before running

No response added after the run

Files with duplicate names unlink

Rename file after add

Renamed file Rename: Original file

File Rename after renaming: New file name

File file is modified after renaming

Modify a file

Changed that existed before running

No response added after the run

Change

File is modified

Aau.watch uses TCP message communication, but due to file renaming, the message is more frequent, a short period of 3 of information transmission, resulting in node TCP client, there is a swap, or less response to data time phenomenon.

Related I have also written arrdio TCP client response processing, just get the message will be a one-time acquisition, no swap situation.

Do not know what the cause of the cause.

Post the relevant code below

Node Watch.js code

varNET = require (' net ');varGulp = require (' Gulp '));varChokidar = require (' Chokidar '));varHOST = ' 127.0.0.1 ';varPORT = 7070;varActiontab = ["", "Add File", "Remove file", "File Modified", "Rename: Original filename", "Rename: New file name"];varClient =NewNet. Socket (); Client.connect (PORT, HOST,function() {Client.write (' {action: ' dirwatch ', url: ' d:/snailshop/wgu/'} ');}); Client.on (' Data ',function(data) {vartab = Data.tostring (). Split (', '); Console.log (' [Aau.watch] ', tab[1], actiontab[tab[0]], +NewDate)}); Client.on (' Close ',function() {Console.log (' Connection closed ');});/*Net.createserver (function (sock) {Console.log (' CONNECTED: ' + sock.remoteaddress + ': ' + sock.remoteport);        Sock.on (' Data ', function (data) {console.log (' data ' + sock.remoteaddress + ': ' + data ');    Sock.write (' you said ' + Data + ');    });    Sock.on (' Close ', function (data) {Console.log (' CLOSED: ' + sock.remoteaddress + ' + sock.remoteport); });}). Listen (PORT, HOST);*/Gulp.watch (['./wgu/**/* '], event ={Console.log (' [Gulp.watch] ', Event.path, Event.type, +NewDate)});varWatcher = Chokidar.watch (['./wgu/**/* ')], {ignored:/[\/\\]\./, persistent:true});' Add,change,unlink,adddir,unlinkdir,ready '. Split (', '). ForEach (item ={watcher.on (item, Path={Console.log (' [Chokidar] ', path, item, +NewDate)})})

Arrdio TCP server, related to the package, only need to write less code, the only pity is the official website of the document is relatively small, need to spend some time to understand how the writing

import console;import wsock.tcp.server;import wsock.tcp.client;import Thread.command;varTcptab = {};varListener =: Thread.command ();//thread command to process messages thrown by threads Listener.onfilechange=function(msg) { for(i = 1; #tcpTab; 1{tcptab[i].write (msg)}};varTCPServer, errmsg = Wsock.tcp.server ("127.0.0.1", 7070);if(!tcpserver) {Console.log (errmsg) Console.pause ();} Else{tcpserver.forever (function(acceptsocket) {Table.push (Tcptab, Wsock.tcp.client (, acceptsocket))//Save client socket: Thread.invoke (function(AcceptSocket, Hwndlistener) {//Start a separate thread, responding to each client's request import wsock.tcp.client;            Import Web.json;            Import Fsys.dirwatcher;                Import console;                         Import Thread.command; varTcpClient =wsock.tcp.client (, acceptsocket);  Do{                varCStr =Tcpclient.recv (); if(CStr) {varobj =Web.json.parse (CStr); Console.dump (obj)if(obj && type (obj) = = ' table ' && obj.action = = ' Dirwatch ') {                        varWatcher =Fsys.dirwatcher (Obj.url);  for(Filename,action,actiontext,iteminchwatcher.eachchanges (Flags,subtree)) {                            //Console.log (1, String.Join ({action;filename;}, ","))                            //Tcpclient.write (String.Join ({action;filename;}, ","))Thread.command.post (Hwndlistener, "Onfilechange", String.Join ({action;filename;}, ",")); Thread command Processing} }}} while(1)}, AcceptSocket, Listener.hwnd); })}      

Arrdio TCP Client (used to test the processing power of the renamed message)

import Console;import wsock.tcp.client;import fsys.dirwatcher; var tcpc = wsock.tcp.client (); Tcpc.connect ("127.0.0.1", 7070); Tcpc.write (' {action: ') Dirwatch ", url:" d:/snailshop/wgu/"} ');  Do {    var cstr = tcpc.recv ();    Console.log (CStr);      while (1)

Originally also want to arrdio file monitoring, using the window API, relatively better response, but in and nodejs information transmission aspect still exist very difficult, not I this front-end knowledge can solve the problem. can only give up.

Chokidar that is Gulp-watch monitoring file aspect is also good, performance above some consumption, but should not be affected by too much.

Windows file Watch Nodejs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.