title: Why client cannot connect to the Java Websocket server?
body: <p>I am learning websockets with Spring 5. I use <a href="http://www.baeldung.com/spring-5-reactive-websockets" rel="nofollow noreferrer">this tutorial</a>. But when I try to connect to websocket via JavaScript I have the folowing error:</p>
  
  <pre><code>WebSocket connection to 'ws://localhost:8080/event-emitter' failed: Error during WebSocket handshake: Unexpected response code: 404
  </code></pre>
  
  <p>When I try to connect to websocket via Java I have the folowing error:</p>
  
  <pre><code>Exception in thread "main" reactor.ipc.netty.http.client.HttpClientException: HTTP request failed with code: 404.
  Failing URI: /event-emitter
  Suppressed: java.lang.Exception: #block terminated with an error
      at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:126)
      at reactor.core.publisher.Mono.block(Mono.java:1185)
  </code></pre>
  
  <p>My <em>ReactAndSpringDataRestApplication.java</em>:</p>
  
  <pre><code>@SpringBootApplication
  public class ReactAndSpringDataRestApplication {
  
  @Autowired
  private ReactiveWebSocketHandler webSocketHandler;
  
  public static void main(String[] args) {
      SpringApplication.run(ReactAndSpringDataRestApplication.class, args);
  }
  
  @Bean
  public HandlerMapping webSocketHandlerMapping() {
      Map&lt;String, WebSocketHandler&gt; map = new HashMap&lt;&gt;();
      map.put("/event-emitter", webSocketHandler);
  
      SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
      handlerMapping.setOrder(1);
      handlerMapping.setUrlMap(map);
      return handlerMapping;
  }
  @Bean
  public WebSocketHandlerAdapter handlerAdapter() {
      return new WebSocketHandlerAdapter();
  }
  }
  </code></pre>
  
  <p><em>ReactiveWebSocketHandler.java</em>:</p>
  
  <pre><code>@Component
  public class ReactiveWebSocketHandler implements WebSocketHandler {
  
  private static final ObjectMapper json = new ObjectMapper();
  
  private Flux&lt;String&gt; eventFlux = Flux.generate(sink -&gt; {
      EventTwo event = new EventTwo(Double.toString(Math.random()), new Date().toString());
      try {
          sink.next(json.writeValueAsString(event));
      } catch (JsonProcessingException e) {
          sink.error(e);
      }
  });
  private Flux&lt;String&gt; intervalFlux = Flux.interval(Duration.ofMillis(1000L))
          .zipWith(eventFlux, (time, event) -&gt; event);
  
  @Override
  public Mono&lt;Void&gt; handle(WebSocketSession webSocketSession) {
      return webSocketSession.send(intervalFlux
              .map(webSocketSession::textMessage))
              .and(webSocketSession.receive()
                      .map(WebSocketMessage::getPayloadAsText)
                      .log());
  }
  }
  </code></pre>
  
  <p>JavaScript which try to connect to websocket:</p>
  
  <pre><code>import React, {Component} from 'react';
  import { connect } from 'react-redux';
  import '../res/messages.css';
  class Messages extends Component {
  
  constructor(props) {
      super(props);
      this.state = {messages: []};
  }
  
  getInitialState(){
      return { messages : [] }
  }
  
  componentDidMount() {
      var clientWebSocket = new WebSocket("ws://localhost:8080/event-emitter");
      clientWebSocket.onopen = function() {
          console.log("clientWebSocket.onopen", clientWebSocket);
          console.log("clientWebSocket.readyState", "websocketstatus");
          clientWebSocket.send("event-me-from-browser");
      }
      clientWebSocket.onclose = function(error) {
          console.log("clientWebSocket.onclose", clientWebSocket, error);
      }
      clientWebSocket.onerror = function(error) {
          console.log("clientWebSocket.onerror", clientWebSocket, error);
      }
      clientWebSocket.onmessage = function(error) {
          console.log("clientWebSocket.onmessage", clientWebSocket, error);
          this.setState({
                          messages : this.state.messages.concat([ new Date() ])
                      })
      }
  }
  
  render() {
      return (
          &lt;div&gt;&lt;ul&gt;{ this.state.messages.map( (msg, idx) =&gt; &lt;li key={'msg-' + idx }&gt;{ msg }&lt;/li&gt; )}&lt;/ul&gt;&lt;/div&gt;
      )
  }
  }
  </code></pre>
  
  <p>Should there be additional configuration of websockets in Java side\Spring? Why client cannot connect to the server?</p>