wip
This commit is contained in:
parent
707291e637
commit
34dce54575
1 changed files with 32 additions and 6 deletions
|
@ -1,27 +1,41 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.socket;
|
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.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import javax.websocket.*;
|
import javax.websocket.*;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
public class SensorSocketEndpoint extends Endpoint {
|
public class SensorSocketEndpoint extends Endpoint {
|
||||||
|
|
||||||
private Gson gson = new Gson();
|
private Gson gson = new Gson();
|
||||||
|
|
||||||
private Set<Session> clients = Collections.synchronizedSet(new HashSet<>());
|
@Autowired private JWTTokenUtil jwtTokenUtil;
|
||||||
|
|
||||||
public Set<Session> getClients() {
|
private Set<Session> unauthorizedClients = Collections.synchronizedSet(new HashSet<Session>());
|
||||||
return clients;
|
|
||||||
|
// 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 {
|
public int broadcast(JsonObject message) throws IOException, EncodeException {
|
||||||
for (Session session : clients) {
|
for (Session session : unauthorizedClients) {
|
||||||
System.out.println(message);
|
System.out.println(message);
|
||||||
session.getBasicRemote().sendObject(message);
|
session.getBasicRemote().sendObject(message);
|
||||||
}
|
}
|
||||||
return clients.size();
|
return unauthorizedClients.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -30,9 +44,21 @@ public class SensorSocketEndpoint extends Endpoint {
|
||||||
test.addProperty("ciao", "mamma");
|
test.addProperty("ciao", "mamma");
|
||||||
try {
|
try {
|
||||||
session.getBasicRemote().sendText(gson.toJson(test));
|
session.getBasicRemote().sendText(gson.toJson(test));
|
||||||
clients.add(session);
|
unauthorizedClients.add(session);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
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 {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue