我正在创建一个协作的网络音乐平台。
目前,我有一台简单的鼓机在本地工作,有一个JSON文件记录所有的节拍。也就是说,在模式中穿孔之后,代码在登录到控制台时看起来是这样的。调度程序和play函数然后迭代并播放节拍,如果它在当前节拍上是“on”的话。
"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]所以接下来我想网络这个网络应用程序,这样两个人可以同时编辑模式。我真的很挣扎,我很想在这里得到帮助。我看过流星和股票,但变得相当困惑。
如何在服务器上运行一个由两个用户编辑的JSON文件?(他们将轮流编辑模式,就像这个游戏http://sharejs.org/hex.html#9TGAyPGFOy)这个JSON文件需要在任何时候都在代码中更新,这样就可以向两个用户播放最新版本的跟踪。
任何建议都会很好,我觉得我把自己弄得太复杂了.
谢谢
发布于 2015-03-09 13:51:07
我建议在这个项目中使用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方法。这个方法比较容易读,但是在读/写文件时会锁定整个进程。这将导致问题,如果有超过两个用户使用该项目。查看*异步方法,找出解决这一问题的方法。它们不难实现。
祝好运!
发布于 2015-03-09 13:58:40
让我们尽可能地简化这一点。
最简单的解决方案是在内存中(在node.js服务器中)有一个javascript对象,并让用户从一个API调用中编辑这个对象。喜欢,
app.put('/composition', updateJSONObject)updateJSONObject函数将读取内存中的javascript对象,编辑它,并返回更新的对象。
app.get('/composition', getJSONObject)此路径将获得最新的JSON对象。
(我假设你在这里使用express.js )
然后,执行一个简单的setInterval,定期使用fs.createWriteStream将javascript对象保存到文件系统(每1分钟一次?)。这将保存json对象,如filename.json,如果内存中的json对象为空,则可以检索该对象。(比如,在服务器重新启动后)
处理碰撞是一个更大的问题。但是,由于您不想使解决方案过于复杂,这很简单,并且完成工作,除非您期望每秒编辑100个。
发布于 2015-03-09 13:47:41
我认为在您的服务器上使用一个简单的PHP脚本就可以做到这一点。
您可以为每个会话创建一个JSON/txt文件(可能为每个会话生成一个随机ID ),并使用AJAX将数据从两个用户发送到服务器,然后服务器将更新JSON文件。
可以将jQuery用于AJAX:
$.post("myscript.php", { data: 'json goes here' });在PHP中,您可以获得POST数据:
$data = $_POST['data'];这将是一个字符串,您可以保存到服务器上。这里是关于用PHP保存文件的教程。
要获得数据,只需从服务器重新加载JSON文件即可。我建议将整个过程保存为一个字符串(Javascript中的JSON.stringify()),然后使用JSON.parse()对其进行解码。
考虑到用户将轮流使用,这个架构应该工作得很好。
如果有什么我不清楚的地方,祝你好运!
https://stackoverflow.com/questions/28942871
复制相似问题