Spring Boot SSE(服务器发送的事件):如何动态发送响应

时间:2019-09-09 12:38:02

标签: spring-boot spring-webflux server-sent-events

我是SSE(服务器发送事件)的新手。我找到了一种在春季引导中使用SSE发送响应的方法。我能够每20秒发送一次响应。我用下面的代码相同。

+----+-------------+--------------------------+------------+--------+--------------------------------------------------------------------------------------------------------------------------------+----------------------------+---------+-------------------------------------------+---------+----------+---------------------------------------------------------------------+
| id | select_type | table                    | partitions | type   | possible_keys                                                                                                                  | key                        | key_len | ref                                       | rows    | filtered | Extra                                                               |
+----+-------------+--------------------------+------------+--------+--------------------------------------------------------------------------------------------------------------------------------+----------------------------+---------+-------------------------------------------+---------+----------+---------------------------------------------------------------------+
|  1 | PRIMARY     | contacts                 | NULL       | ref    | idx_contacts_del_last,idx_cont_del_reports,idx_del_id_user,idx_contacts_tmst_id,idx_del_date_modified,idx_del_date_modified_id | idx_contacts_del_last      | 2       | const                                     | 1114111 |   100.00 | Using temporary; Using filesort                                     |
|  1 | PRIMARY     | <derived2>               | NULL       | ALL    | NULL                                                                                                                           | NULL                       | NULL    | NULL                                      |       2 |    50.00 | Using where; Using join buffer (Block Nested Loop)                  |
|  1 | PRIMARY     | contacts_cstm            | NULL       | eq_ref | PRIMARY                                                                                                                        | PRIMARY                    | 144     | sugarcrm.contacts.id                      |       1 |   100.00 | Using index                                                         |
|  2 | DERIVED     | team_membershipscontacts | NULL       | ref    | idx_team_membership,idx_teammemb_team_user,idx_del_team_user                                                                   | idx_team_membership        | 145     | const                                     |       2 |    99.36 | Using index condition; Using where; Using temporary; Using filesort |
|  2 | DERIVED     | tst                      | NULL       | ref    | idx_ud_set_id,idx_ud_team_id,idx_ud_team_set_id,idx_ud_team_id_team_set_id                                                     | idx_ud_team_id_team_set_id | 144     | sugarcrm.team_membershipscontacts.team_id |       1 |   100.00 | Using index                                                         |
+----+-------------+--------------------------+------------+--------+--------------------------------------------------------------------------------------------------------------------------------+----------------------------+---------+-------------------------------------------+---------+----------+---------------------------------------------------------------------+

但是问题是,此方法每20秒调用一次。

但是,我的要求是每20秒向客户端发送一个空响应,并在新用户添加到数据库时发送响应。

我如何实现我的目标?或者我在概念上是完全错误的。

1 个答案:

答案 0 :(得分:0)

您应该创建事件发布者和侦听器,以便在注册新用户时可以将事件发送给发射器。

由于您使用的是spring-boot并可能处于休眠状态,因此您可以在此处看到有关如何拦截休眠事件的示例。

Hibernate interceptor or listener with Spring Boot and Spring Data JPA

我会亲自 不使用助焊剂,并在春季制定一种调度方法将这些空的ping响应发送给发射器。可以在此处找到此示例:https://www.roytuts.com/server-sent-events-spring-push-notifications/

有关春季活动的更多信息,请参见以下网址: https://www.baeldung.com/spring-events