aboutsummaryrefslogtreecommitdiff
path: root/juick-server
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-10-23 10:11:46 +0300
committerGravatar Vitaly Takmazov2018-10-23 10:12:43 +0300
commitd962d4bbc332ca8c91050883739882e4db43fe4e (patch)
treee574a8aacd5415a41441add61ed8dc4a43384a96 /juick-server
parentacba0c187acc928f3a3d563e644e35cccb509481 (diff)
SSE cleanup refactoring
Diffstat (limited to 'juick-server')
-rw-r--r--juick-server/src/main/java/com/juick/server/api/Service.java13
1 files changed, 9 insertions, 4 deletions
diff --git a/juick-server/src/main/java/com/juick/server/api/Service.java b/juick-server/src/main/java/com/juick/server/api/Service.java
index c65d89a1..2938e9da 100644
--- a/juick-server/src/main/java/com/juick/server/api/Service.java
+++ b/juick-server/src/main/java/com/juick/server/api/Service.java
@@ -39,7 +39,7 @@ import java.util.*;
@Controller
public class Service {
- private static Logger logger = LoggerFactory.getLogger(Post.class);
+ private static Logger logger = LoggerFactory.getLogger(Service.class);
@Inject
private UserService userService;
@Inject
@@ -57,7 +57,7 @@ public class Service {
@Inject
private ServerManager serverManager;
- Session session = Session.getDefaultInstance(new Properties());
+ private Session session = Session.getDefaultInstance(new Properties());
@ApiIgnore
@PostMapping("/api/mail")
@@ -137,6 +137,11 @@ public class Service {
throw new HttpForbiddenException();
}
}
+ private void endSession(SseEmitter emitter) {
+ serverManager.getSessions().stream()
+ .filter(s -> s.getEmitter().equals(emitter))
+ .forEach(session -> serverManager.getSessions().remove(session));
+ }
@GetMapping("/api/events")
public SseEmitter handle() throws IOException {
logger.info("{} connected", UserUtils.getCurrentUser().getName());
@@ -146,8 +151,8 @@ public class Service {
emitter.send(service);
serverManager.getSessions().add(new ServerManager.EventSession(UserUtils.getCurrentUser(), emitter));
- emitter.onCompletion(() -> serverManager.getSessions().remove(emitter));
- emitter.onTimeout(() -> serverManager.getSessions().remove(emitter));
+ emitter.onCompletion(() -> endSession(emitter));
+ emitter.onTimeout(() -> endSession(emitter));
return emitter;
}