0
package primer;

import java.io.IOException;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Component
public class SocketTextHandler extends TextWebSocketHandler {

    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) 

            throws InterruptedException, IOException {

        String clientMessage = message.getPayload();
        if (clientMessage.startsWith("start")) {    
            
        for (int h = 0; h < 60; h++) {
                for (int m = 0; m < 60; m++) {
                    for (int s = 0; s < 60; s++){
                        session.sendMessage(new TextMessage(
                            String.format("%02d:%02d:%02d", h, m, s)));
                            
                        Thread.sleep(1000);                                                                 
                    }       }           
                }
            } if (clientMessage.startsWith("stop")) {
                
            }
                    
                }       }
                
            



    

This is the socketTextHandler code that responds to messages sent from the client to the server.

The question is, the counter should be one for everyone, i.e. multiple clients see the same counter value, regardless of when the client typed start.

How to implement it? If you make it static, how? Or make one global counter on the server, which is launched by the first client and already subsequent clients, if you enter start, it shows the current counter. In principle, I can imagine how to implement this, but I don’t know by what methods. Hours of scrolled forums and Google did not give any knowledge. UPD when server get start, server will respond to client many messages like : 00:00:01, 00:00:02... i wanted to doesnt matter how much client writed start they all need to see value of first client timer

tried to static whole cycle, but then sendMessage can't reach "h, m, s" value.

7
  • Have you tried using AtomicInteger as a counter? because each client will start their own thread. Commented Nov 14, 2022 at 10:25
  • What use is a stopwatch when you can't account for internet latency? Commented Nov 14, 2022 at 11:27
  • Explain: If client A comes at first and sends "start", and 20 seconds later, client B comes and sends "start", should client B see "20" or "1"? Commented Nov 14, 2022 at 12:02
  • @RealSkeptic client B should see 20. Commented Nov 14, 2022 at 12:04
  • And do they get one message or how many? In short, edit your question, and add an explanation of what the inputs and the outputs are. "A client comes in and sends start, the system is supposed to give them such and such report such and such times" etc. Commented Nov 14, 2022 at 12:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.