Skip to content

Instantly share code, notes, and snippets.

@dunithd
Created February 21, 2021 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dunithd/e6d558aecf852762b4dccf015f179222 to your computer and use it in GitHub Desktop.
Save dunithd/e6d558aecf852762b4dccf015f179222 to your computer and use it in GitHub Desktop.
package com.edu.retail.ws;
import io.quarkus.scheduler.Scheduled;
import javax.enterprise.context.ApplicationScoped;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
@ServerEndpoint("/dashboard/{clientId}")
@ApplicationScoped
public class DashboardWebSocket {
private Map<String, Session> sessions = new ConcurrentHashMap<>();
private AtomicInteger totalOrders = new AtomicInteger();
@OnOpen
public void onOpen(Session session, @PathParam("clientId") String clientId) {
sessions.put(clientId, session);
}
@OnClose
public void onClose(Session session, @PathParam("clientId") String clientId) {
sessions.remove(clientId);
}
@OnError
public void onError(Session session, @PathParam("clientId") String clientId, Throwable throwable) {
sessions.remove(clientId);
}
@Scheduled(every="5s")
void increment() {
if (sessions != null) {
totalOrders.incrementAndGet();
broadcast(String.valueOf(totalOrders));
}
}
private void broadcast(String message) {
sessions.values().forEach(s -> {
s.getAsyncRemote().sendObject(message, result -> {
if (result.getException() != null) {
System.out.println("Unable to send message: " + result.getException());
}
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment