与node.js文件同步:unision == tcp == node —– —– node == tcp == unison

如果节点正在运行,并且节点正在运行,有没有人看到一个解决scheme,可以让我们保持一组文件的同步。

同步是复杂的,所以我认为可以留给像unison这样的工具(就像rsync),然后所有的节点将要做的是连接了一个TCPpipe道authentication的用户之间。

filesystem1---unision==tcp==node.js------[http]----node.js==tcp====unison---filesystem2 

这可能大概有12行JavaScript,但到目前为止,这已经超出了我的能力范围或者我能find的任何范例。

我已经看了一大堆其他文件同步选项(比如Git,准确性,化石,包括在Linux上安装一个Simias iFolder服务器一周,失败 – 这看起来很有前途,因为它包含了每个主要操作系统的文件浏览客户端)但是现在我正在想更多,更简单的事情可能就是这样了。

如果有人看到一个Node.js项目这样做,或者在连接两个TCPpipe道的水平不是很难,那么我会很高兴收到你的来信

我将采取这个问题的基本方法是使用stream2。
我的基本方法是这样的。

从第一个tcp获取数据

 var gulp = require('gulp') , thr = require('through2') ; gulp.watch('rootDirectory/**/*', function(evt) { if ('deleted' === evt.type) { // do stuff if file is deleted return } // file got changed, moved or added. gulp.src(evt.path).pipe(thr(chunk, enc, next){ // chunk is a vinyl filesytem object, // so better fix chunk before sending to tcp server next(null, fixedChunk) }).pipe(toYourTcpServer) }) 

然后在你的节点

 var net = require('net') , http = require('http') ; var clientTcp = net.connect({port: 'toYourTcpServerPort'}) var server = http.createServer(function(req, res){ clientTcp.pipe(res) }).listen('somePort') 

那么另一方面,则是相反的
在节点

 var clientTcp2 = net.connect({port: 'toYourTcpServerPort2'}) var server2 = http.createServer(function(req, res){ // fix response or something else. req.pipe(clientTcp2) }).listen('somePort') 

来自第二个TCP

 gutil = require('gulp-util') clientTcp2.pipe(thr(function(chunk, enc, next){ // You must create a vinyl filesystem before // sending down stream. Use new gutil.File() next(null, fixedChunk) })).pipe(gulp.dest('finalDirectoy')) 

through2创build转换stream和through2帮助您使用stream创build任务。 乙烯文件只是一个吞咽使用的抽象。

我希望这给你一个如何使用stream解决你的问题好运的想法