{"id":304960,"date":"2020-06-07T15:00:23","date_gmt":"2020-06-07T15:00:23","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=304960"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=304960","title":{"rendered":"WebSockets \u0438 Spring WebFlux"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/505710\/\">\n<h1 id=\"vstuplenie\">\u0412\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435<\/h1>\n<p>  <\/p>\n<p>\u0412\u0441\u0435 \u043c\u044b \u0437\u043d\u0430\u0435\u043c \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442\u044b, \u043b\u044e\u0431\u0438\u043c \u0438\u043b\u0438 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0438 \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0445 \u0441\u043e Spring MVC. <\/p>\n<p>  <\/p>\n<p>\u0410 \u0447\u0442\u043e \u043d\u0430 \u0441\u0447\u0435\u0442 \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f?<\/p>\n<p>  <\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043e \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Spring WebFlux.<\/p>\n<p>  <\/p>\n<p>\u041a\u043e\u043d\u0442\u0435\u043d\u0442:<\/p>\n<p>  <\/p>\n<ol>\n<li>\u041a\u0430\u043a \u0441\u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442.<\/li>\n<li>\u041a\u0430\u043a \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435?<\/li>\n<li>\u041a\u0430\u043a \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435?<\/li>\n<li>\u0414\u0435\u043c\u043e \u043f\u0440\u043e\u0435\u043a\u0442.<\/li>\n<li>\u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438 \u043a\u0430\u043a \u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u0438\u0442\u044c \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442\u044b.<\/li>\n<\/ol>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0430\u0441\u044c \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 <a href=\"http:\/\/start.spring.io\">start.spring.io<\/a> \u0441 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c\u044e Reactive Web.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h1 id=\"kak-skonfigurirovat-vebsoket\">\u041a\u0430\u043a \u0441\u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442<\/h1>\n<p>  <\/p>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0447\u0435\u043c\u0443 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0442\u043e\u0442 \u0438\u043b\u0438 \u0438\u043d\u043e\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u0442.<\/p>\n<p>  <\/p>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435 \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0434\u043b\u044f \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442\u0430.<\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0434\u0432\u0430 \u0431\u0438\u043d\u0430: HandlerMapping \u0438 HamdlerAdapter.<\/p>\n<p>  <\/p>\n<p>HandlerAdapter \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0442\u0430\u043a\u0438 \u043b\u0435\u0433\u043a\u043e:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">    @Bean     public HandlerAdapter wsHandlerAdapter() {         return new WebSocketHandlerAdapter();     }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u0435\u043d \u044d\u0442\u043e\u0442 \u0431\u0438\u043d?<\/p>\n<p>  <\/p>\n<p>\u0410\u0434\u0430\u043f\u0442\u0435\u0440 \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0435\u0431\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442 \u0441\u0435\u0440\u0432\u0438\u0441 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 (\u0435\u0449\u0435 \u043d\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u0443\u044e \u043d\u0430\u043c\u0438) \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e WebSocketHandler. \u0414\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u044d\u0442\u043e\u0442 \u0431\u0438\u043d \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442 \u0432 Spring WebFlux.<\/p>\n<p>  <\/p>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0431\u0438\u043d \u044d\u0442\u043e HandlerMapping. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u0435\u0433\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c url \u0438 \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u0437\u0430 \u043d\u0435\u0433\u043e WebSocketHandler, \u0430 \u0442\u0430\u043a \u0436\u0435 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0438\u043d\u0430. \u0421\u043f\u0440\u0438\u043d\u0433 \u0431\u0443\u0434\u0435\u0442 \u0437\u043d\u0430\u0442\u044c \u043a\u0430\u043a \u043d\u0430\u0441\u0442\u043e\u0438\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0439 url \u0438 \u043a\u0430\u043a \u0435\u0433\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0431\u043b\u0430\u0433\u043e\u0434\u044f\u0440\u044f \u044d\u0442\u043e\u0439 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438. \u041d\u0430\u0448 url \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442\u044c \u0442\u0430\u043a <a href=\"http:\/\/localhost:8080\/push\">http:\/\/localhost:8080\/push<\/a>, \u0442\u0430\u043a \u043a\u0430\u043a \u043d\u0438\u0436\u0435 \u043c\u044b \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c \u043f\u0443\u0442\u044c \u043a\u0430\u043a \/push.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">    @Bean     public HandlerMapping handlerMapping() {     \/\/ url         String path = &quot;\/push&quot;;      \/\/ here webSocketHandler not defined yet         Map&lt;String, WebSocketHandler&gt; map = Map.of(path, webSocketHandler);      \/\/ -1 is order         return new SimpleUrlHandlerMapping(map, -1);     }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0433\u043e\u0434\u043d\u043e url \u0438 WebSocketHandler.<\/p>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e WebSocketHandler. \u0421 \u044d\u0442\u0438\u043c \u043a\u043b\u0430\u0441\u0441\u043e\u043c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044c \u043e\u0431\u044a\u0435\u043a\u0442 WebSocketSession, \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438 \u0447\u0438\u0442\u0430\u0442\u044c \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<p>\u041d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043c\u044b \u043e\u0441\u0442\u0430\u0432\u0438\u043c Mono.empty(), \u043d\u043e \u043d\u0435 \u0432\u043e\u043b\u043d\u0443\u0439\u0442\u0435\u0441\u044c, \u043d\u0438\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u0430\u044f \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@Component public class DefaultWebSocketHandler implements WebSocketHandler {      @Override     public Mono&lt;Void&gt; handle(WebSocketSession session) {     return Mono.empty();     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@Configuration public class WebSocketConfig {      private WebSocketHandler webSocketHandler;      @Autowired     public WebSocketConfig(WebSocketHandler webSocketHandler) {         this.webSocketHandler = webSocketHandler;     }      @Bean     public HandlerMapping handlerMapping() {         String path = &quot;\/push&quot;;         Map&lt;String, WebSocketHandler&gt; map = Map.of(path, webSocketHandler);         return new SimpleUrlHandlerMapping(map, -1);     }      @Bean     public HandlerAdapter wsHandlerAdapter() {         return new WebSocketHandlerAdapter();     } }<\/code><\/pre>\n<p>  <\/p>\n<h1 id=\"kak-prochitat-soobschenie\">\u041a\u0430\u043a \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435?<\/h1>\n<p>  <\/p>\n<p>\u041b\u043e\u0433\u0438\u043a\u0430 \u0447\u0442\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0432 \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 WebSocketHandler.<\/p>\n<p>  <\/p>\n<p>\u041d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 WebSocketSession \u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 receive(), \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0442\u0440\u0438\u043c \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0435\u0433\u043e.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@Component public class DefaultWebSocketHandler implements WebSocketHandler {      @Override     public Mono&lt;Void&gt; handle(WebSocketSession session) {     session.receive().subscribe(message =&gt; {         \/\/ process message here     });     return Mono.empty();     } }<\/code><\/pre>\n<p>  <\/p>\n<h1 id=\"kak-otpravit-soobschenie\">\u041a\u0430\u043a \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435?<\/h1>\n<p>  <\/p>\n<p>\u041f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c WebSocketHandler \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u0430\u043a\u0443\u0439-\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0438\u0439 \u0434\u0430\u0442\u0430 \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u043b\u0430\u0442\u044c \u043a\u043b\u0438\u0435\u043d\u0442\u0443.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">public class Event {      private String name;      private int count;      public Event() {}      public Event(String name, int count) {         this.name = name;         this.count = count;     }          \/\/ Getters, Setters, etc. }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0430\u043a \u0436\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0438\u0432\u0435\u043d\u0442\u044b \u0432 \u0432\u0438\u0434\u0435 \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0441\u0442\u0440\u0438\u043c\u0430 (\u043a\u0430\u043a \u0442\u0440\u0435\u0431\u0443\u0435\u0442 WebSocketSession).<\/p>\n<p>  <\/p>\n<p>\u0413\u043b\u0430\u0432\u043d\u043e\u0435: \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0441\u044f\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u0438\u0432\u0435\u043d\u0442\u044b \u0432 \u0441\u0442\u0440\u0438\u043c.<\/p>\n<p>  <\/p>\n<p>\u0422\u0430\u043a \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u0430\u0448 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">public interface EventUnicastService {      \/**      * Add message to stream      * @param next - message which will be added to stream      *\/     void onNext(Event next);      Flux&lt;Event&gt; getMessages(); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c EmitterProcessor, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0434 \u0432\u0441\u0435 \u043d\u0430\u0448\u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f. \u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u044f\u0442\u044c \u043d\u0430\u0448\u0438 \u0438\u0432\u0435\u043d\u0442\u044b \u0438 \u0440\u0430\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0430\u043c.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@Service public class EventUnicastServiceImpl implements EventUnicastService {      private EmitterProcessor&lt;Event&gt; processor = EmitterProcessor.create();      @Override     public void onNext(Event next) {         processor.onNext(next);     }      @Override     public Flux&lt;Event&gt; getMessages() {         return processor.publish().autoConnect();     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0434\u0435\u0441\u044c: <\/p>\n<p>  <\/p>\n<ul>\n<li>publish() \u0441\u043e\u0437\u0434\u0430\u0435\u0442 ConnectableFlux, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u043e\u043c\u0443 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0430 Flux;<\/li>\n<li>autoConnect() \u0441\u043e\u0435\u0434\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0441 ConnectableFlux, \u043a\u043e\u0433\u0434\u0430 \u043a\u0442\u043e-\u0442\u043e \u0432\u044b\u0449\u044b\u0432\u0430\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 subscribe \u0443 \u0441\u0442\u0440\u0438\u043c\u0430.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0422\u0430\u043a \u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 replay(int), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043a\u044d\u0448\u0438\u0440\u0443\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0435\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0430\u043c.<\/p>\n<p>  <\/p>\n<p>\u041e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">     @Override     public Mono&lt;Void&gt; handle(WebSocketSession session) {     Flux&lt;WebSocketMessage&gt; messages = unicastService.getMessages()         .flatMap(o -&gt; {                     try {                         return Mono.just(objectMapper.writeValueAsString(o)); \/\/ &lt;- convert object to json                     } catch (JsonProcessingException e) {                         return Mono.error(e);                     }                 })             .map(session::textMessage);      return session.send(messages);     }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u0438\u0437 \u043a\u043e\u0434\u0430 \u0432\u044b\u0448\u0435, \u043f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0443, \u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 json. Spring WebFlux \u043d\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0434\u043b\u044f \u0434\u0435\u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0432 json \u0434\u043b\u044f \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442\u043e\u0432.<\/p>\n<p>  <\/p>\n<p>\u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f Jackson, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e ObjectMapper#writeValueAsString, \u0442\u0430\u043a \u0447\u0442\u043e \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043c\u044b \u0438\u043c\u0435\u0435\u043c json \u0441\u0442\u0440\u043e\u043a\u0443.<\/p>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u044a\u0435\u0434\u0435\u043d\u0438\u0442\u044c \u0447\u0442\u0435\u043d\u0438\u0435 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443 \u0432 DefaultWebSocketHandler.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@Component public class DefaultWebSocketHandler implements WebSocketHandler {      private EventUnicastService eventUnicastService;      private ObjectMapper objectMapper;      @Autowired     public DefaultWebSocketHandler(EventUnicastService eventUnicastService, ObjectMapper objectMapper) {         this.eventUnicastService = eventUnicastService;         this.objectMapper = objectMapper;     }      @Override     public Mono&lt;Void&gt; handle(WebSocketSession session) {         Flux&lt;WebSocketMessage&gt; messages = session.receive()                 \/\/ .doOnNext(message -&gt; { read message here or in the block below })                 .flatMap(message -&gt; {                     \/\/ or read message here                     return eventUnicastService.getMessages();                 })                 .flatMap(o -&gt; {                     try {                         return Mono.just(objectMapper.writeValueAsString(o));                     } catch (JsonProcessingException e) {                         return Mono.error(e);                     }                 }).map(session::textMessage);         return session.send(messages);     } }<\/code><\/pre>\n<p>  <\/p>\n<h1 id=\"demo\">\u0414\u0435\u043c\u043e<\/h1>\n<p>  <\/p>\n<h2 id=\"event-generator\">Event generator<\/h2>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0434\u0435\u043c\u043e \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438\u0432\u0435\u043d\u0442 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043a\u0430\u0436\u0434\u0443\u044e \u0441\u0435\u043a\u0443\u043d\u0434\u0443 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u0432\u0435\u043d\u0442.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@Component public class EventGenerator {      private AtomicInteger counter = new AtomicInteger(0);      private EventUnicastService eventUnicastService;      @Autowired     public EventGenerator(EventUnicastService eventUnicastService) {         this.eventUnicastService = eventUnicastService;     }      @Scheduled(initialDelay = 1000, fixedDelay = 1000)     public void generateEvent() {         int count = counter.getAndIncrement();         Event event = new Event(&quot;event&quot;, count);         eventUnicastService.onNext(event);     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c Scheduler \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@SpringBootApplication @EnableScheduling \/\/ &lt;- enable scheduling!!! public class WebfluxwebsocketsApplication<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"klient\">\u041a\u043b\u0438\u0435\u043d\u0442<\/h2>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f Angular.<\/p>\n<p>  <\/p>\n<p>\u0427\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c:<\/p>\n<p>  <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442.<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\">ng new projectname<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418\u0437\u043c\u0435\u043d\u0438\u0442\u044c app.component.ts.<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import {Component, OnInit} from '@angular\/core'; import {webSocket} from 'rxjs\/webSocket';  @Component({   selector: 'app-root',   templateUrl: '.\/app.component.html',   styleUrls: ['.\/app.component.css'] }) export class AppComponent implements OnInit {   title = 'websocket-angular';    messages: string[] = [];      \/\/ create WebSocket subject   private subject = webSocket('ws:\/\/localhost:8080\/push');    ngOnInit(): void {     this.subject.next({message: 'message'}); \/\/ &lt;- ping first message     this.subject.subscribe(message =&gt; {       \/\/ &lt;- listen messages from server       const event = message as Event       this.messages.push(event.name + ' #' + event.count);     });   } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c rxjs websockets.<\/p>\n<p>  <\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043d\u0430\u0448\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043d\u0430\u0434\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c app.component.html.<\/p>\n<p>  <\/p>\n<pre><code class=\"html\">&lt;div style=&quot;text-align:center&quot;&gt;   &lt;h1&gt;     Welcome to {{ title }}!   &lt;\/h1&gt; &lt;\/div&gt; &lt;ul&gt;   &lt;li *ngFor=&quot;let m of messages&quot;&gt;{{m}}&lt;\/li&gt; &lt;\/ul&gt;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u044c \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 <a href=\"http:\/\/localhost:4200\">localhost:4200<\/a>.<\/p>\n<p>  <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/im\/xz\/ik\/imxzikc-v0rrzqb9sneadc81prm.png\"\/><\/p>\n<p>  <\/p>\n<h1 id=\"rekomendacii-kak-obezopasit-vebsokety\">\u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438 \u043a\u0430\u043a \u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u0438\u0442\u044c \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442\u044b<\/h1>\n<p>  <\/p>\n<ol>\n<li>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442\u044b \u0447\u0435\u0440\u0435\u0437 SSL\/TLS (wss:\/\/ \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b).<\/li>\n<li>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u043b\u0438\u0435\u0442\u0430 \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/li>\n<li>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438.<\/li>\n<li>\u0413\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0439\u0442\u0435 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e http \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u043a \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442\u0430\u043c.<\/li>\n<li>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 Origin \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a.<\/li>\n<\/ol>\n<p>  <\/p>\n<h1 id=\"vyvody\">\u0412\u044b\u0432\u043e\u0434\u044b<\/h1>\n<p>  <\/p>\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u0430 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043a\u043e\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f, \u0447\u0442\u0435\u043d\u0438\u0435 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0435 \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u044b\u043b\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043e \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0434\u0435\u043c\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430. \u0422\u0430\u043a \u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u0447\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u043f\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0432 \u0437\u043d\u0430\u043d\u0438\u0435 Project Reactor.<\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u043a\u043e\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 GitHub <a href=\"https:\/\/github.com\/MaxNeutrino\/snippets\/tree\/master\/webflux-websocket\">https:\/\/github.com\/MaxNeutrino\/snippets\/tree\/master\/webflux-websocket<\/a><\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/505710\/\"> https:\/\/habr.com\/ru\/post\/505710\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/505710\/\">\n<h1 id=\"vstuplenie\">\u0412\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435<\/h1>\n<p>  <\/p>\n<p>\u0412\u0441\u0435 \u043c\u044b \u0437\u043d\u0430\u0435\u043c \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442\u044b, \u043b\u044e\u0431\u0438\u043c \u0438\u043b\u0438 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0438 \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0445 \u0441\u043e Spring MVC. <\/p>\n<p>  <\/p>\n<p>\u0410 \u0447\u0442\u043e \u043d\u0430 \u0441\u0447\u0435\u0442 \u0440\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f?<\/p>\n<p>  <\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043e \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Spring WebFlux.<\/p>\n<p>  <\/p>\n<p>\u041a\u043e\u043d\u0442\u0435\u043d\u0442:<\/p>\n<p>  <\/p>\n<ol>\n<li>\u041a\u0430\u043a \u0441\u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442.<\/li>\n<li>\u041a\u0430\u043a \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435?<\/li>\n<li>\u041a\u0430\u043a \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435?<\/li>\n<li>\u0414\u0435\u043c\u043e \u043f\u0440\u043e\u0435\u043a\u0442.<\/li>\n<li>\u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438 \u043a\u0430\u043a \u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u0438\u0442\u044c \u0432\u0435\u0431\u0441\u043e\u043a\u0435\u0442\u044b.<\/li>\n<\/ol>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0430\u0441\u044c \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 <a href=\"http:\/\/start.spring.io\">start.spring.io<\/a> \u0441 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c\u044e Reactive Web.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-304960","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/304960","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=304960"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/304960\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=304960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=304960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}