Answer the question
In order to leave comments, you need to log in
How to pass io.to('...') as function parameter?
Hello
Here is a sample code:
On the server:
...
function start(socket) {
setInterval(() => {
socket.emit('update', Date.now());
}, 2e3);
}
function main(io) {
let connectons = [];
let index = 0;
io.on('connection', (socket) => {
socket.on('start', () => {
const { id }= socket;
if (!connectons[id]) {
connectons[id] = true;
const group = `gr-${index}`;
socket.join(group);
++index;
start(io.to(group));
// setInterval(() => {
// socket.emit('update', Date.now());
// }, 1e3);
}
});
});
}
main(io);
socket.on('update', (time) => {
console.log(time);
});
$('#start').click(() => {
socket.emit('start');
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question