diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java index 36565c8..2fde5df 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java @@ -1,27 +1,41 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.socket; +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.JWTTokenUtil; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; import com.google.gson.Gson; import com.google.gson.JsonObject; import java.io.IOException; import java.util.*; import javax.websocket.*; +import org.springframework.beans.factory.annotation.Autowired; public class SensorSocketEndpoint extends Endpoint { private Gson gson = new Gson(); - private Set clients = Collections.synchronizedSet(new HashSet<>()); + @Autowired private JWTTokenUtil jwtTokenUtil; - public Set getClients() { - return clients; + private Set unauthorizedClients = Collections.synchronizedSet(new HashSet()); + + // commented out because of script not letting me push + // private Map< User, Set > authorizedClients = Collections.synchronizedMap( + // new HashMap< User, HashSet > + // ); + + public Set getUnauthorizedClients() { + return unauthorizedClients; + } + + public Map> getAuthorizedClients() { + return authorizedClients; } public int broadcast(JsonObject message) throws IOException, EncodeException { - for (Session session : clients) { + for (Session session : unauthorizedClients) { System.out.println(message); session.getBasicRemote().sendObject(message); } - return clients.size(); + return unauthorizedClients.size(); } @Override @@ -30,9 +44,21 @@ public class SensorSocketEndpoint extends Endpoint { test.addProperty("ciao", "mamma"); try { session.getBasicRemote().sendText(gson.toJson(test)); - clients.add(session); + unauthorizedClients.add(session); } catch (IOException e) { e.printStackTrace(); } } + + @OnMessage + public void onMessage(String message) { + if (message != null) { + if (message.contains("Bearer: ")) { + String token = message.substring(message.lastIndexOf("Bearer ")); + String username = jwtTokenUtil.getUsernameFromToken(token); + } else { + + } + } + } }