I am new to nodejs and was experimenting with socket.io. I am trying to send message to specific sockets so I thought I will store references to the socket.
var controls = {};
var clients = {};
var control = io
.of("/control")
.on("connection", function(socket){
socket.on("connect_player", function(data){
var id = data.screen_id;
controls[socket.id] = id;
});
socket.on("msg", function(data){
id = controls[socket.id]
player.socket(clients[id]).emit("msg", data);
});
});
var player = io
.of("/player")
.on("connection", function(socket){
socket.on("set_id", function(id){
clients[id] = socket.id
});
});
I first start a webpage which connects to the "player" namespace and displays an id(hard coded). Then another webpage which connects to the "control" namespace with the same id as player, now the controller should be able to send messages to that particular player but this works for a while and then fails.
During the period when it works I can send messages from the control page to the specific player page.
I looked at the sockets ids in the player namespace after it stopped working and found the socket ids are different from the initial ones.
I guess I am doing something wrong here or has a wrong understanding of the concepts. How can I fix this problem.
Edits:
I added listeners for "reconnect" event and found that the value of socket.id changes after each the "reconnect" event. So the controls and clients have invalid socket.id values which casues my problem. Is there away to overcome this.