文件与node.js同步:unision == tcp == node ----- [http] ----- node == tcp == unison

Lic*_*orn 5 synchronization tcp unison node.js

如果你有节点运行并且我有节点运行,那么有没有人看到过一个允许我们保持一组文件同步的解决方案.

同步很复杂,所以我认为这可以留给像unison这样的工具(就像rsync一样),然后所有节点必须做的就是在经过身份验证的用户之间连接TCP管道.

filesystem1---unision==tcp==node.js------[http]----node.js==tcp====unison---filesystem2
Run Code Online (Sandbox Code Playgroud)

它可能大约有12行JavaScript,但到目前为止它已经超出了我,或者我能找到的任何一个例子.

我查看了一大堆其他文件同步选项(比如Git,准确性,化石,包括一周试图在Linux上安装Simias iFolder服务器,失败〜看起来很有希望因为它包含了每个主要操作系统的文件监视客户端)但现在我觉得有些东西,更简单可能就是这样.

如果有人看到这样做的Node.js项目,或者处于连接两个TCP管道并不太难的级别,那么我将不胜感激

mar*_*-gj 0

我解决这个问题的基本方法是使用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)

})
Run Code Online (Sandbox Code Playgroud)

然后在你的节点

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')
Run Code Online (Sandbox Code Playgroud)


然后在另一端,在节点处执行相反的操作

var clientTcp2 = net.connect({port: 'toYourTcpServerPort2'})

var server2 = http.createServer(function(req, res){
  // fix response or something else.
  req.pipe(clientTcp2)
}).listen('somePort')
Run Code Online (Sandbox Code Playgroud)

来自第二个 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'))
Run Code Online (Sandbox Code Playgroud)

through2创建转换流并gulp帮助您使用流创建任务。Vinyl 文件只是 gulp 使用的抽象。

我希望这能让您了解如何使用流来解决您的问题祝您好运