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.
AtomicIntegeras a counter? because each client will start their own thread.start, the system is supposed to give them such and such report such and such times" etc.