JavaScript client cannot connect to Spring 4 WebSocket
<p>I have implemented websocket in spring but the JavaScript client cannot connect to the websocket.</p>

<p>Here is the WebSocketConfig class:</p>

<pre><code>package com.myapp.spring.security.config;
import com.myapp.spring.web.controller.MyWebSocketHandler;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.PerConnectionWebSocketHandler;

@Configuration
@EnableWebMvc
@EnableWebSocket
@ComponentScan(basePackages={"com.myapp.spring.*"})
public class WebSocketConfig implements WebSocketConfigurer {

    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry
                .addHandler(myWebSocketHandler(), "/endpoint")
                .setAllowedOrigins("*");
    }

    @Bean
    public WebSocketHandler myWebSocketHandler() {
        return new PerConnectionWebSocketHandler(MyWebSocketHandler.class);
    }


}
</code></pre>

<p>Here is the <code>test.html</code> page that tries to connect to the websocket:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"&gt;

      &lt;body&gt;

        &lt;p&gt;Going to connect to the WebSocket at /endpoint. Check Console&lt;/p&gt;
        &lt;button onclick="ss()"&gt;Send&lt;/button&gt;
      &lt;/body&gt;

      &lt;script type="text/javascript"&gt;
        var webSocket = new WebSocket("wss://"+document.location.hostname+":8443"+"/endpoint");
        webSocket.onopen = function(message) {
                processOpen(message);
            };
            webSocket.onmessage = function(message) {
                processMessage(message);
            };
            webSocket.onclose = function(message) {
                processClose(message);
            };
            webSocket.onerror = function(message) {
                processError(message);
            };


            function processOpen(message) {
                console.log("JS: Server Connected... "+message);
            }
            function processMessage(message) {
                console.log("Getting a mess: "+message);
            }
            function processClose(message) {
                console.log("JS: Client disconnected... "+message);
            }
            function processError(message) { //
                console.log("Error occured: "+message);
            }

            function ss() {
                webSocket.send("test");
            }
      &lt;/script&gt;

&lt;/html&gt;
</code></pre>

<p>I initialized the websocket path to be at <code>/endpoint</code>. This is evident by my server logs which say that this has occurred: </p>

<pre><code>[org.springframework.web.socket.server.support.WebSocketHandlerMapping] (ServerService Thread Pool -- 75) Mapped URL path [/endpoint] onto handler of type [class org.springframework.web.socket.server.support.WebSocketHttpRequestHandler]
</code></pre>

<p>When I open up <code>test.html</code>, the connection opens and immediately disconnects. The <code>processOpen(message)</code> and <code>processClose(message)</code> function are immediately called, one after the other. So what am I doing wrong, and how can I fix this? </p>