Skip to content

Emitting event to resume stopwatch resets it #146

Description

@geoffhuntsgood

Hello! Not sure if this is a me thing as I'm still learning how websockets work. I'm using socket.io to help synchronize multiplayer in a webapp I've made, and I have a pause/resume button that pauses a stopwatch for everyone and resumes for everyone when clicked. This works exactly as expected when not using sockets, but I think there might be something else going on.

Here's a snippet:

const pause = (emit: boolean) => {
  stopwatch.pause();
  if (emit) {
    socket.emit("pause_client", roomName);
  }
};

const resume = (emit: boolean) => {
  stopwatch.start();
  if (emit) {
    socket.emit("resume_client", roomName);
  }
};

...

<Button
  label={stopwatch.isRunning ? "Pause" : "Resume"}
  handleClick={() => {
    if (stopwatch.isRunning) {
      pause(true);
    } else {
       resume(true);
    }
  }}
/>

In this code, when you click "Pause" the stopwatch pauses for everyone. However, when you click "Resume", the clicking player's stopwatch resumes, but everyone else's stopwatch resets. Putting the socket-related code aside for a moment, is there anything obvious I'm doing wrong here? Am I incorrectly using stopwatch.isRunning?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions