WebFlux support for SSE with multiline fragments

See gh-33194
This commit is contained in:
rstoyanchev
2024-08-06 11:06:21 +03:00
parent b734156f32
commit 8e2b27e5d8
9 changed files with 138 additions and 39 deletions

View File

@@ -30,6 +30,7 @@ import org.springframework.http.server.ServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;
/**
* A specialization of {@link ResponseBodyEmitter} for sending
@@ -203,6 +204,8 @@ public class SseEmitter extends ResponseBodyEmitter {
@Nullable
private StringBuilder sb;
private boolean hasName;
@Override
public SseEventBuilder id(String id) {
append("id:").append(id).append('\n');
@@ -211,6 +214,7 @@ public class SseEmitter extends ResponseBodyEmitter {
@Override
public SseEventBuilder name(String name) {
this.hasName = true;
append("event:").append(name).append('\n');
return this;
}
@@ -234,6 +238,9 @@ public class SseEmitter extends ResponseBodyEmitter {
@Override
public SseEventBuilder data(Object object, @Nullable MediaType mediaType) {
if (object instanceof ModelAndView mav && !this.hasName && mav.getViewName() != null) {
name(mav.getViewName());
}
append("data:");
saveAppendedText();
if (object instanceof String text) {