Store session and data received with socketio
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:
"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:
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#"
.
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.
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);
});
});
Answer
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);
});
});