diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java index 1735b86..2801a74 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java @@ -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 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 *