两个用户如何协作编辑 JSON 文件

How can two users collaboratively edit a JSON file?

本文关键字:文件 协作 编辑 JSON 两个 用户 何协作      更新时间:2023-09-26

我正在创建一个协作网络音乐平台。

目前,我有一个简单的鼓机在本地工作,有一个 JSON 文件记录所有节拍,即在模式中打孔后,代码在记录到控制台时如下所示。然后,调度程序和播放函数将遍历并播放节拍(如果它在当前节拍处"打开")。

  "kick": [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
    "snare": [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
    "hat": [0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1],
    "tom1": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    "tom2": [0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0],
    "tom3": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

因此,接下来我想将此Web应用程序联网,以便两个人可以同时编辑模式。我真的很挣扎,希望在这里得到一些帮助。我看过 meteor 和 sharejs,但变得很困惑。

如何在服务器上拥有一个由两个用户编辑的 JSON 文件?(他们会轮流编辑图案,就像这个游戏 http://sharejs.org/hex.html#9TGAyPGFOy)此 JSON 文件需要始终在代码中更新,以便可以向两个用户播放最新版本的曲目。

任何提示都会很棒,我觉得我在这里把自己复杂化了......

谢谢

我建议为这个项目使用 Socket.io。

您可以使用以下代码设置一个简单的套接字服务器:

(别忘了npm install --save socket.io

// server.js
// we're using filesystem stuff here
var fs = require('fs');
// set up socket.io
var io = require('socket.io').listen(80);
// load the current json file in
var currentJSON = fs.readFileSync('./beat.json');
io.on('connection', function(clientSocket) {
   console.log('Client connected!');
   // tell the client that just connected what the current JSON is
   clientSocket.emit('stateUpdate', {currentJSON: currentJSON});
   // listen for an "update" event from a client
   clientSocket.on('update', function(payload) {
       if(payload.updatedJSON) {
           console.log('We got updated JSON!');
           // validate the incoming JSON here
           validateBeatJSON(payload.updatedJSON);
           // update the "currentJSON" variable to reflect what the client sent
           currentJSON = payload.updatedJSON;
           // save the json file so the server will have it when it next starts. also, try not to use *sync methods for this, as they'll block the server but ive included them cos they're shorter
           fs.writeFileSync('/beat.json', JSON.stringify(payload.updatedJSON), 'utf8');
           // tell the other clients what the current state is
           clientSocket.broadcast.emit('stateUpdate', {currentJSON: currentJSON});
       }
   });
});
//client.html
<script src="socket.io.js"></script>
<script>
 var currentJSON = [];
 var socket = io(); // TIP: io() with no args does auto-discovery
  socket.on('stateUpdate', function (payload) {
     currentJSON = payload.currentJSON;
  });
  // blah blah, set event for when the json is updated
  window.addEventListener('updatedJSONLocally', function(e) {
    socket.emit('update', {updatedJSON: currentJSON});
  });
</script>

我基本上只是在答案框中输入了这个 - 这还没有经过测试或其他什么,但我认为它可以让您很好地了解项目的 Socket.io 库的基础知识。

正如我的评论中提到的,我不建议在执行文件系统操作时使用 *sync 方法。此方法更容易阅读,但在读取/写入文件时会锁定整个过程。如果有多个用户使用该项目,这将导致问题。查看 *async 方法以解决此问题。它们并不难实现。

祝你好运!

让我们尽可能简化这一点。

最简单的解决方案是在内存中(在您的节点.js服务器中)中拥有一个 javascript 对象,并让用户从单个 API 调用中编辑它。喜欢

app.put('/composition', updateJSONObject)

updateJSONObject函数将读取内存中的 JavaScript 对象,对其进行编辑,并返回更新的对象。

app.get('/composition', getJSONObject)

此路由将获取最新的 JSON 对象。

(我假设你使用的是快递.js这里)

然后,执行一个简单的setInterval,定期使用fs.createWriteStream将javascript对象保存到文件系统(每1分钟一次?这将保存您的 json 对象,就像filename.json如果内存中的 json 对象为空,您可以检索该对象。(例如,在服务器重新启动后)

处理碰撞是一个更大的问题。但是,由于您不想使解决方案过于复杂,因此这很简单,除非您预计每秒进行 100 次编辑,否则可以完成工作。

我认为服务器上的简单PHP脚本可以在这里解决问题。

您可以为每个会话创建一个 JSON/txt 文件(可能为每个会话生成一个随机 ID),并使用 AJAX 将数据从两个用户发送到服务器,然后服务器将更新 JSON 文件。

您可以使用 jQuery for AJAX:

$.post("myscript.php", { data: 'json goes here' });

在 PHP 中,您可以获取 POST 数据:

$data = $_POST['data'];

这将是一个字符串,您可以将其保存到服务器上。这是关于使用 PHP 保存文件的教程。

要取回数据,您只需从服务器重新加载 JSON 文件即可。我建议将整个内容保存为字符串(JSON.stringify() Javascript中),然后在取回时使用JSON.parse()对其进行解码。

鉴于用户将轮流,此体系结构应该可以正常工作。

LMK 如果有什么我不清楚的,祝你好运!