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:
commit
334d35a4cc
1 changed files with 16 additions and 4 deletions
|
@ -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
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue