java -jar command with a " &" to make it run at backend
@Component
public class CounterHandler extends TextWebSocketHandler {
@Autowired
private TradeRepository repo;
private Logger logger = LoggerFactory.getLogger(CounterHandler.class);
WebSocketSession session;
// This will send only to one client(most recently connected)
public void counterIncrementedCallback() {
if (session != null && session.isOpen()) {
try {
List<SwapTrade> trades = repo.findAll();
session.sendMessage(new TextMessage(JsonParser.parseObjToJson(trades)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void afterConnectionEstablished(WebSocketSession session) {
logger.info("Connection established");
this.session = session;
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message)
throws Exception {
if ("CLOSE".equalsIgnoreCase(message.getPayload())) {
session.close();
} else {
logger.info("Received:" + message.getPayload());
}
}
}