弹簧SseEmitter输出似乎格式错误

时间:2019-04-15 21:57:23

标签: spring spring-mvc server-sent-events spring-web

我正在尝试使用带有Python客户端的Spring Boot设置服务器发送事件。不幸的是,我的python客户端不读取数据,而是打印空白行。我用Wireshark分析了TCP流,看来Spring正在将“ data:”字段发送为多行,这违反了SSE规范。

我发送消息的Java代码:

What what = new What(); // this class just has a payload string field and a getter and a setter
what.setPayload( data ); // data is just a string
SseEmitter.SseEventBuilder builder = SseEmitter.event( ).data( what, MediaType.APPLICATION_JSON ).name( "event-name" ).id( "my-event-id-1234" );
emitter.send( builder );

三次调用上述代码后,从服务器发送的TCP流如下所示:

HTTP/1.1 200 
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, PUT, GET, OPTIONS, DELETE
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: content-type, x-requested-with, authorization
X-Application-Context: application:8080
Cache-Control: no-store
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY
Content-Type: text/event-stream;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 15 Apr 2019 21:49:17 GMT

5
data:
2b
{"payload":"HELLO THIS IS FROM THE SERVER"}
27

event:event-name
id:my-event-id-1234


5
data:
2b
{"payload":"HELLO THIS IS FROM THE SERVER"}
27

event:event-name
id:my-event-id-1234


5
data:
2b
{"payload":"HELLO THIS IS FROM THE SERVER"}
27

event:event-name
id:my-event-id-1234

请注意,只有数据格式错误。正如预期的那样,事件和ID字段全部显示在一行上。我尝试了几种其他方法:仅发送字符串,发送对象而不设置事件和id等。数据总是以数字,数据说明符,数字和实际有效载荷出现在不同的行上。 / p>

具有十六进制值的行似乎正在通知解析器以下行包含了很多字节,但是此信息未在https://www.w3.org/TR/eventsource/的SSE规范中的任何地方覆盖

这是Spring的错误吗?我可以在某处设置配置选项,以使数据与事件和ID一样打印出来吗?

0 个答案:

没有答案