yhs0092 commented on code in PR #4875: URL: https://github.com/apache/servicecomb-java-chassis/pull/4875#discussion_r2276859256
########## swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/sse/SseEventResponseEntity.java: ########## @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.servicecomb.swagger.invocation.sse; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import jakarta.validation.constraints.NotNull; + +public class SseEventResponseEntity<T> { + private static final Logger LOGGER = LoggerFactory.getLogger(SseEventResponseEntity.class); + + /** + * event id + */ + private Integer id; + + /** + * event type + */ + private String event; + + /** + * reconnection time + */ + private Long retry; + + /** + * business data + */ + @NotNull + private T data; + + public SseEventResponseEntity<T> id(int id) { + if (this.id != null) { + LOGGER.warn("origin id: [{}] is exists, overridden by the current value: [{}]", this.id, id); + } + this.id = id; + return this; + } + + public SseEventResponseEntity<T> event(String event) { + if (!StringUtils.isEmpty(this.event)) { + LOGGER.warn("origin event: [{}] is exists, overridden by the current value: [{}]", this.event, event); + } + this.event = event; + return this; + } + + public SseEventResponseEntity<T> retry(long retry) { + if (this.retry != null) { + LOGGER.warn("origin retry: [{}] is exists, overridden by the current value: [{}]", this.retry, retry); + } + this.retry = retry; + return this; + } + + public SseEventResponseEntity<T> data(Object data) { + if (this.data != null) { + LOGGER.warn("origin data: [{}] is exists, overridden by the current value: [{}]", this.data, data); + } + this.data = (T) data; Review Comment: 这里建议参考一下 Spring 的 `org.springframework.web.servlet.mvc.method.annotation.SseEmitter.SseEventBuilderImpl#data(java.lang.Object, org.springframework.http.MediaType)` 方法. 它在处理 SSE 的消息的时候允许业务在同一条消息中添加多个 `data` 字段, 并且如果消息中包含换行符时, 自动将一个`data`字段拆分成多个`data`字段存放在消息中. (当然这里是服务端的逻辑, 客户端应该也有类似的处理措施). 我觉得这里刚好顺带解答了另外一个问题, 就是如果`\r\n`, `\r`, `\n`都是 SSE 消息的本身的换行符, 那么消息内容中的换行符应该如何表示? 从 Spring 给的解决方案来看, 大概率就是一条消息中包含多个 `data` 字段, 就表示这条消息有多行. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@servicecomb.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org