This commit is contained in:
Tommaso Rodolfo Masera 2020-03-14 20:17:47 +01:00 committed by Claudio Maggioni
parent 707291e637
commit 34dce54575

View file

@ -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<Session> clients = Collections.synchronizedSet(new HashSet<>());
@Autowired private JWTTokenUtil jwtTokenUtil;
public Set<Session> getClients() {
return clients;
private Set<Session> unauthorizedClients = Collections.synchronizedSet(new HashSet<Session>());
// commented out because of script not letting me push
// private Map< User, Set<Session> > authorizedClients = Collections.synchronizedMap(
// new HashMap< User, HashSet<Session> >
// );
public Set<Session> getUnauthorizedClients() {
return unauthorizedClients;
}
public Map<User, Set<Session>> 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 {
}
}
}
}