admin管理员组

文章数量:1389754

To give an example, let's say a user sends some json to a server containing his socket id, and the server takes 5 seconds to respond. When it's ready to respond, it extracts the socket.id from the json object, and emits some data only to that socket.

If the user where to refresh (therefore changing the socket he is connecting via ) between sending the message and the server responding, how would I go about ensuring they still receives the data?

Server:

io.on("connection", function(socket) {
  socket.on("data", function(data) {
    var id = data.socket_id; 
    
    io.sockets.connected[id].emit("response", "sup"); 

    // I know I could just do -> socket.emit("response", "sup") <- but im simply trying it out for      learning purposes
  })
})

To give an example, let's say a user sends some json to a server containing his socket id, and the server takes 5 seconds to respond. When it's ready to respond, it extracts the socket.id from the json object, and emits some data only to that socket.

If the user where to refresh (therefore changing the socket he is connecting via ) between sending the message and the server responding, how would I go about ensuring they still receives the data?

Server:

io.on("connection", function(socket) {
  socket.on("data", function(data) {
    var id = data.socket_id; 
    
    io.sockets.connected[id].emit("response", "sup"); 

    // I know I could just do -> socket.emit("response", "sup") <- but im simply trying it out for      learning purposes
  })
})

Client:

socket.emit("data", {
  username: "chris", 
  socket_id: socket.id, 
});

socket.on("response", function(res) {
  console.log(res); 
})

Share Improve this question asked Mar 17, 2018 at 16:05 LorzaLorza 50110 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

If the user refreshes their page, that will make a new socket.io connection that will have a new and different socket.id. So, if you want to be able to identify a particular browser session across refreshes, then you need an identifier that survives a page refresh. That is not the socket.id.

Most likely, you would use some sort of long lasting userID in a cookie. If you don't already have any sort of authentication happening that gives you a userID in a cookie, then you can simply coin a unique ID in a cookie if there isn't already one anytime a user makes a request to your server. That will give you a unique userID in a cookie for every ining socket.io connection (as long as cookies aren't disabled). When your socket.io connection connects, you can add it to a Map object you keep where the userID is the key and the current socket.id is the data and you can add the userID as a property on the socket. When the socket disconnects, you would then remove it from the Map object.

Then, when you want to save a reference to the user, you save the userID, not the socket.id and that reference stays the same across a page refresh. When you want to send to the userID, you can look it up in the Map object to get the current socket.id (which will be automatically kept current by your connect and disconnect event handlers as described above) and then use that to send to it.

本文标签: javascriptHow to send data to a users socket id after they have refreshedsocketioStack Overflow