title: Whoops! Lost connection to undefined - Connection Lost Just After The Connection Established
body: <p>For Last couple of days i have been trying spring 4 websocket . But There is a problem 
  I am using apache-tomcat-8 and this is not a maven project.</p>
  
  <p>Here are my snippets</p>
  
  <p>index.jsp</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt;
  &lt;%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %&gt;
  &lt;%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %&gt;
  &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Hello WebSocket&lt;/title&gt;
      &lt;script type="text/javascript" src="./js/sockjs-0.3.4.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="./js/stomp.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
          var stompClient = null;
  
          function setConnected(connected) {
              document.getElementById('connect').disabled = connected;
              document.getElementById('disconnect').disabled = !connected;
              document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';
              document.getElementById('response').innerHTML = '';
          }
  
          function connect() {
  
               var socket = new SockJS("&lt;c:url value='/hello'/&gt;");
              stompClient = Stomp.over(socket);
              stompClient.connect({}, function(frame) {
                  setConnected(true);
                  console.log('Connected: ' + frame);
                  window.alert('Connected: ' + frame);
                  stompClient.subscribe('/topic/greetings', function(greeting){
                      showGreeting(JSON.parse(greeting.body).content);
                  });
              });
          }
  
          function disconnect() {
              if (stompClient != null) {
                  stompClient.disconnect();
              }
              setConnected(false);
              console.log("Disconnected");
          }
  
          function sendName() {
              var name = document.getElementById('name').value;
              stompClient.send("/app/hello", {}, JSON.stringify({ 'name': name }));
          }
  
          function showGreeting(message) {
              var response = document.getElementById('response');
              var p = document.createElement('p');
              p.style.wordWrap = 'break-word';
              p.appendChild(document.createTextNode(message));
              response.appendChild(p);
          }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;noscript&gt;&lt;h2 style="color: #ff0000"&gt;Seems your browser doesn't support Javascript! Websocket relies on Javascript being enabled. Please enable
      Javascript and reload this page!&lt;/h2&gt;&lt;/noscript&gt;
  &lt;div&gt;
      &lt;div&gt;
          &lt;button id="connect" onclick='connect();'&gt;Connect&lt;/button&gt;
          &lt;button id="disconnect" disabled="disabled" onclick="disconnect();"&gt;Disconnect&lt;/button&gt;
      &lt;/div&gt;
      &lt;div id="conversationDiv"&gt;
          &lt;label&gt;What is your name?&lt;/label&gt;&lt;input type="text" id="name" /&gt;
          &lt;button id="sendName" onclick="sendName();"&gt;Send&lt;/button&gt;
          &lt;p id="response"&gt;&lt;/p&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>WebSocketConfig.xml</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:websocket="http://www.springframework.org/schema/websocket"
      xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/websocket
          http://www.springframework.org/schema/websocket/spring-websocket.xsd"&gt;
  
     &lt;websocket:message-broker application-destination-prefix="/app"&gt;
         &lt;websocket:stomp-endpoint path="/hello"&gt;
              &lt;websocket:sockjs/&gt;
         &lt;/websocket:stomp-endpoint&gt;
         &lt;websocket:simple-broker prefix="/topic"/&gt;
  &lt;/websocket:message-broker&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>HelloMessage.java</p>
  
  <pre><code>package com.springws.test;
  
  public class HelloMessage {
  
      private String name;
  
      public String getName() {
          return name;
      }
  
  }
  </code></pre>
  
  <p>Greeting.java</p>
  
  <pre><code>package com.springws.test;
  
  public class Greeting {
  
      private String content;
  
      public Greeting(String content) {
          this.content = content;
      }
  
      public String getContent() {
          return content;
      }
  
  }
  </code></pre>
  
  <p>GreetingController.java</p>
  
  <pre><code>package com.springws.test;
  
  import org.springframework.messaging.handler.annotation.MessageMapping;
  import org.springframework.messaging.handler.annotation.SendTo;
  import org.springframework.stereotype.Controller;
  
  @Controller
  public class GreetingController {
  
      @MessageMapping("/hello")
      @SendTo("/topic/greetings")
      public Greeting greeting(HelloMessage message) throws Exception {
  
          System.out.println(message.getName());
          return new Greeting("Hello, " + message.getName() + "!");
      }
  
  }
  </code></pre>
  
  <p>web.xml</p>
  
  <pre><code>&lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           id="WebApp_ID"
           version="3.0"&gt;
    &lt;display-name&gt;CRUDWebAppMavenized&lt;/display-name&gt;
  
     &lt;listener&gt;
          &lt;listener-class&gt;
              org.springframework.web.context.ContextLoaderListener
          &lt;/listener-class&gt;
      &lt;/listener&gt;
  
     &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt; /WEB-INF/WebSocketConfig.xml&lt;/param-value&gt;
  
  &lt;/context-param&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  
    &lt;welcome-file-list&gt;
  
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
  
    &lt;/welcome-file-list&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>spring-servlet.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:p="http://www.springframework.org/schema/p"
         xmlns:jee="http://www.springframework.org/schema/jee"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:util="http://www.springframework.org/schema/util"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xsi:schemaLocation="
              http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
              http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd
              http://www.springframework.org/schema/jee       http://www.springframework.org/schema/jee/spring-jee.xsd
              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
              http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
              http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"&gt;
  
      &lt;mvc:annotation-driven/&gt;
      &lt;mvc:default-servlet-handler/&gt;
  
      &lt;context:annotation-config /&gt;
  
      &lt;context:component-scan base-package="com.springws.test" /&gt; 
  
      &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt;
          &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/&gt;
          &lt;property name="prefix" value="/"/&gt;
          &lt;property name="suffix" value=".jsp"/&gt;
      &lt;/bean&gt;
      &lt;tx:annotation-driven /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>WebSocketConfig.java</p>
  
  <pre><code>package com.springws.test;
  
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.messaging.simp.config.MessageBrokerRegistry;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
  import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
  import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
  
  @Configuration
  @ComponentScan(basePackages = {"com.springws.test"})
  @EnableWebSocketMessageBroker
  @EnableWebMvc
  @Controller
  public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
  
      @Override
      public void configureMessageBroker(MessageBrokerRegistry config) {
          config.enableSimpleBroker("/topic");
          config.setApplicationDestinationPrefixes("/app");
      }
  
      @Override
      public void registerStompEndpoints(StompEndpointRegistry registry) {
          registry.addEndpoint("/hello").withSockJS();
      }
  
  
  }
  </code></pre>
  
  <p>Libraries</p>
  
  <p><img src="https://i.stack.imgur.com/Re48l.jpg" alt="Libraries"></p>
  
  <p>But in browser console </p>
  
  <p>its giving the following output </p>
  
  <pre><code>Opening Web Socket...
  stomp.js:145 Web Socket Opened...
  stomp.js:145 &gt;&gt;&gt; CONNECT
  accept-version:1.1,1.0
  heart-beat:10000,10000
  
  Whoops! Lost connection to undefined
  </code></pre>
  
  <p>why its losing connection ? </p>