Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Socket.io-在服务器和客户端之间同步数据_Javascript_Jquery_Node.js_Socket.io_Express.io - Fatal编程技术网

Javascript Socket.io-在服务器和客户端之间同步数据

Javascript Socket.io-在服务器和客户端之间同步数据,javascript,jquery,node.js,socket.io,express.io,Javascript,Jquery,Node.js,Socket.io,Express.io,第一次使用node js时,我正在编写一个应用程序,供我和朋友之间私人使用,我的朋友可以随时加入 是否可以让服务器有一个对象数组,即“franksLibrary”,包含n个项目 用户是否能够阅读和修改“franksLibrary” 目前我所做的是在用户网页中设置“franksLibrary”,并通过socket.io发送franksLibrary和所有其他VAR进行同步 index.js是服务器代码, index.html是交付给用户的内容 examle index.js var http =

第一次使用node js时,我正在编写一个应用程序,供我和朋友之间私人使用,我的朋友可以随时加入

是否可以让服务器有一个对象数组,即“franksLibrary”,包含n个项目

用户是否能够阅读和修改“franksLibrary”

目前我所做的是在用户网页中设置“franksLibrary”,并通过socket.io发送franksLibrary和所有其他VAR进行同步

index.js是服务器代码, index.html是交付给用户的内容

examle index.js

var http = require('http').createServer(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg, usr){
    io.emit('chat message',msg, usr);
  });

});
http.listen(3000, function(){
  console.log('listening on *:3000');
});
index.html示例:

    var franksLibrary = [{
            "book1":"title of book 1"
        },
        {
            "book2":"title of book 2"
        }];

   socket.on('chat message', function(franksL /*,more variables*/){
       franksLibrary = franksL
    });

    synch = function(){
        socket.emit('chat message', franksLibrary /*,more variables*/);
    }

    removeBook = function(object, from){
        var a = object;
        var index = from.indexOf(a);
        from.splice(index,1);
        synch();
    }

franksLibrary
index.html
移动到
index.js
。然后,在服务器上的
连接
回调中,将带有数据的
聊天信息
发送到新连接的客户端

index.js

var franksLibrary = [{
  "book1": "title of book 1"
},
{
  "book2": "title of book 2"
}];

io.on('connection', function(socket) {
  socket.emit('chat message', franksLibrary);
  socket.on('chat message', function(data) {
    franksLibrary = data;
    socket.broadcast.emit('chat message', franksLibrary);
  });
});
index.html

// initialize variable
var franksLibrary = [];

const socket = io();

socket.on('chat message', function(franksL) {
  franksLibrary = franksL;
});

const synch = function(){
  socket.emit('chat message', franksLibrary);
};

const removeBook = function(object, from) {
  var a = object;
  var index = from.indexOf(a);
  from.splice(index,1);
  synch();
};

是否可能
目前我在做什么
好的,有什么问题吗?在a中共享相关代码,并解释需要改进的地方。我添加了一个简化版本的代码,我希望变量存储在服务器上而不是用户上。感谢这正是我想要的,这样无论我的用户是否重新连接,它仍然会给出准确的状态