简体   繁体   中英

Multiple socket.event inside one socket.on

I am trying an application that emits multiple times when it is called once. For example:

io.on('connection', socket => {
   socket.on('compute-library', message => {
   while (true) {
     socket.emit('status', 1)
   };
  });
});

However, when I am running my application, I only get a single emit element. If I add a termination condition for the loop. In the last emit I get the whole list of emits following it. How can we resolve this issue? Does there needs to be a single emit inside socket.on or we can have multiple.

EDIT:

io.on("connection", socket => {

  socket.on("compute-library", message => {

    while (!library.computeCompleted) {

      compute.COMPUTE_FUNCTION.somefunction(library);

      socket.emit("compute-results", [[], library]);

    }

    const solutions = library.ans;

    socket.emit("compute-results", [solutions, library]);

  });

The actual function is this. Please suggest how to go about it.

why dont you use setInterval?

let intervalWhile
io.on('connection', socket => {
  socket.on('compute-library', message => {
     intervalWhile = setInterval(() => {
       socket.emit('status', 1)
     }, 1000);
  });
  socket.on('another-event', () => {
     clearInterval(intervalWhile)
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM