Fixed ConcurrentModificationException on socket broadcast
This commit is contained in:
parent
27cb73292c
commit
6c9e2a7d7d
1 changed files with 11 additions and 22 deletions
|
@ -1,6 +1,5 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.socket;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.socket;
|
||||||
|
|
||||||
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.didThrow;
|
|
||||||
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonConfig;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonConfig;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
|
||||||
|
@ -8,7 +7,6 @@ import com.google.common.collect.HashMultimap;
|
||||||
import com.google.common.collect.Multimap;
|
import com.google.common.collect.Multimap;
|
||||||
import com.google.common.collect.Multimaps;
|
import com.google.common.collect.Multimaps;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import javax.websocket.*;
|
import javax.websocket.*;
|
||||||
|
@ -61,28 +59,19 @@ public class SensorSocketEndpoint extends Endpoint {
|
||||||
*/
|
*/
|
||||||
public void broadcast(Object message, User u) {
|
public void broadcast(Object message, User u) {
|
||||||
final HashSet<Session> sessions = new HashSet<>(authorizedClients.get(u));
|
final HashSet<Session> sessions = new HashSet<>(authorizedClients.get(u));
|
||||||
|
for (Session s : sessions) {
|
||||||
|
|
||||||
|
|
||||||
sessions.stream()
|
|
||||||
.filter(
|
|
||||||
s -> {
|
|
||||||
if (s.isOpen()) return true;
|
|
||||||
sessions.remove(s);
|
|
||||||
return false;
|
|
||||||
})
|
|
||||||
.forEach(s -> {
|
|
||||||
synchronized (this){
|
|
||||||
try {
|
try {
|
||||||
|
if (s.isOpen()) {
|
||||||
s.getBasicRemote().sendText(gson.toJson(message));
|
s.getBasicRemote().sendText(gson.toJson(message));
|
||||||
|
} else {
|
||||||
|
authorizedClients.remove(u, s);
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles the opening of a socket session with a client
|
* Handles the opening of a socket session with a client
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue