使用socketio存储会话和数据

时间:2021-02-28 16:48:30

I need to store information about connections on my server. This should store 4 connections of 4 different clients. My clients send information like this:

我需要存储有关我服务器上的连接的信息。这应该存储4个不同客户端的4个连接。我的客户发送如下信息:

 "0#0.0 0.0 766.5#3#0.0#"

In the server I assigned an id, and show socket.broadcast.emit('message', map) shows this:

在服务器中我分配了一个id,并显示socket.broadcast.emit('message',map)显示:

message : {"id":"data"}

The console shows:

控制台显示:

message : {"/#DVWkJsrfHFB21XUkAAAC":"0#0.0 0.0 766.5#3#0.0#"}

All this is stored in an object map. Now I don't need the id, just the data "0#0.0 0.0 766.5#3#0.0#".

所有这些都存储在对象图中。现在我不需要id,只需要数据“0#0.0 0.0 766.5#3#0.0#”。

I tried to implement this:

我试图实现这个:

var result = "";
for(var a in map){
 result = result + map[a];
}

But that doesn't work right, it obtains:

但这不起作用,它获得:

"0#0.0 0.0 766.5#3#0.0#"
"0#0.0 0.0 766.5#3#0.0#0#0.0 0.0 766.5#3#0.0#"
"0#0.0 0.0 766.5#3#0.0#0#0.0 0.0 766.5#3#0.0#0#0.0 0.0 766.5#3#0.0#"

This should be showing 4 connections of 4 different clients. This is the real challenge. If I have 4 connections, I just need to show one string, with the data of the 4 connections in a single chain of objects.

这应该显示4个不同客户端的4个连接。这是真正的挑战。如果我有4个连接,我只需要显示一个字符串,在单个对象链中显示4个连接的数据。

This is data that I want:

这是我想要的数据:

{"data client 1", "data client 2","data client 3","data client 4"}

How I see in the server, this should be:

我如何在服务器中看到,这应该是:

{"0#0.0 0.0 3593.0#3#0.0#", "0#0.0 0.0 3593.0#3#0.0#","0#0.0 0.0 3593.0#3#0.0#","0#0.0 0.0 3593.0#3#0.0#"}

This is the code that I am using:

这是我正在使用的代码:

var map = {};
var result = "";
function storeInfo(event, value){
    map[event]= value;
}
io.on('connection', function(socket) {
    var clientPlayer = null;
    socket.on('message', function(data) {
        var sessionid = socket.id;
        if(sessionid in map){
            map[sessionid] = data;
            for(var a in map){
                result = result + map[a];
                // show bucle infinite
            }
            socket.broadcast.emit('message', map);
        } else {
            storeInfo(sessionid, data);
        }
        socket.emit('message', data);
    });
});

1 个解决方案

#1


0  

Well, after of this night of work this is the solution:

那么,在这个晚上的工作之后,这就是解决方案:

var map = {};
function storeInfo(event, value){
    map[event]= value;
} 
io.on('connection', function(socket) {
        var clientPlayer = null;
        socket.on('message', function(data) {
            var sessionid = socket.id;
            if(sessionid in map){
                map[sessionid] = data;
                var result = [];
                for(var o in map){
                    result.push(map[o]);
                }
                result.join(" ");
                socket.broadcast.emit('message', result);
            } else {
                storeInfo(sessionid, data);
            }
            socket.emit('message', data);
        });
});

#1


0  

Well, after of this night of work this is the solution:

那么,在这个晚上的工作之后,这就是解决方案:

var map = {};
function storeInfo(event, value){
    map[event]= value;
} 
io.on('connection', function(socket) {
        var clientPlayer = null;
        socket.on('message', function(data) {
            var sessionid = socket.id;
            if(sessionid in map){
                map[sessionid] = data;
                var result = [];
                for(var o in map){
                    result.push(map[o]);
                }
                result.join(" ");
                socket.broadcast.emit('message', result);
            } else {
                storeInfo(sessionid, data);
            }
            socket.emit('message', data);
        });
});