Merge branch '49-fix-bug' into 'dev'

Fixed @tommi27 synchronization frenzy for sensors

Closes #49

See merge request sa4-2020/the-sanmarinoes/backend!61
This commit is contained in:
Claudio Maggioni 2020-03-25 17:28:11 +01:00
commit 334d35a4cc

View file

@ -8,6 +8,8 @@ import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.*;
import javax.websocket.*;
import org.springframework.beans.factory.annotation.Autowired;
@ -57,20 +59,30 @@ public class SensorSocketEndpoint extends Endpoint {
* @param u the user to which to send the message
* @return number of successful transfer
*/
public long broadcast(Object message, User u) {
public void broadcast(Object message, User u) {
final HashSet<Session> sessions = new HashSet<>(authorizedClients.get(u));
return sessions.stream()
sessions.stream()
.filter(
s -> {
if (s.isOpen()) return true;
sessions.remove(s);
return false;
})
.filter(didThrow(s -> s.getBasicRemote().sendText(gson.toJson(message))))
.count();
.forEach(s -> {
synchronized (this){
try {
s.getBasicRemote().sendText(gson.toJson(message));
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
/**
* Handles the opening of a socket session with a client
*