R
R
ReactJSNinja2018-02-12 16:24:32
JavaScript
ReactJSNinja, 2018-02-12 16:24:32

How to add socket.id to js object but only once after every page refresh?

I want to understand how you can make the date current pop up in the storeEachSocket object once
I have such a problem when I update my page every time it adds the date to the object every time .... I even tried to delete their keys during disconnect but it didn’t help
help me understand how to do what if I went to the "/chat" route and it added an id, then if I updated the page on the "/chat" route, then it is necessary to show the current one user, I mean, in the storeEachSocket object, I did not add many id when updating the page and the object's tapes showed current 1

Client.js
var
io = require("socket.io-client"),
socketURL = "/chat";
socket=io.connect(socketURL);

socket
.on("connect", function () {
console.log("
})
.on("disconnect", function () {
console.log("chat disconnected");
})
.on("join", function (username) {
console.log(username+" : entered to chat");
})
.on("leave", function (username) {
console.log(username+" : left the chat");
})
.on("message", function (username,message) {
console.log("user : "+username+" > "+message);
})
.on("error", function (error) {
console.log("ERROR msg : "+error);
});
******************************************************* *******************************************************
server.

express = require("express"),
app = express();
//<-------------------------------------------
let
//- --------------socket.io----------------->
socket = require("socket.io"),
http = require( 'http'),
server = http.Server(app),
io = socket.listen(server),
//<------------------------ ----------------------
path = require("path"),
bodyParser = require('body-parser'),
router = express.Router(),
port = process.env.port || 3002;
server.listen(port, () => console.log("server is running..."));

let storeEachSocket={};
const chat_route=io.of("/chat");
chat_route.on('connection',
var userid = socket id;
storeEachSocket[userid] = socket;
let Objsize=Object.keys(storeEachSocket).length;
console.log("server connection established");
console.log("connected users : "+Objsize);
});
io.on('disconnect', function(socket){
console.log("server disconnected");
let userid = socket.id;
delete storeEachSocket[userid];
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2019-06-14
@HistoryART

Write socket.id in sessionStorage on the client, in this case, after refreshing the page, it will be the same, and when you open a new tab, there will be a new one

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question