mardi 24 mars 2015

Whoops! Lost connection to undefined - Connection Lost Just After The Connection Established

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.


Here are my snippets


index.jsp



<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://ift.tt/18bwTB1" prefix="spring" %>
<%@ taglib uri="http://ift.tt/IED0jK" prefix="form" %>
<%@ taglib uri="http://ift.tt/QfKAz6" prefix="c" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://ift.tt/kTyqzh">
<html>
<head>
<title>Hello WebSocket</title>
<script type="text/javascript" src="./js/sockjs-0.3.4.js"></script>
<script type="text/javascript" src="./js/stomp.js"></script>
<script type="text/javascript">
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("<c:url value='/hello'/>");
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);
}
</script>
</head>
<body>
<noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websocket relies on Javascript being enabled. Please enable
Javascript and reload this page!</h2></noscript>
<div>
<div>
<button id="connect" onclick='connect();'>Connect</button>
<button id="disconnect" disabled="disabled" onclick="disconnect();">Disconnect</button>
</div>
<div id="conversationDiv">
<label>What is your name?</label><input type="text" id="name" />
<button id="sendName" onclick="sendName();">Send</button>
<p id="response"></p>
</div>
</div>
</body>
</html>


WebSocketConfig.xml



<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:websocket="http://ift.tt/1heCnO0"
xsi:schemaLocation="
http://ift.tt/GArMu6
http://ift.tt/1jdM0fG
http://ift.tt/1heCnO0
http://ift.tt/1CzG6NS">

<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/hello">
<websocket:sockjs/>
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic"/>
</websocket:message-broker>

</beans>


HelloMessage.java



package com.springws.test;

public class HelloMessage {

private String name;

public String getName() {
return name;
}

}


Greeting.java



package com.springws.test;

public class Greeting {

private String content;

public Greeting(String content) {
this.content = content;
}

public String getContent() {
return content;
}

}


GreetingController.java



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() + "!");
}

}


web.xml



<web-app xmlns:xsi="http://ift.tt/ra1lAU"
xmlns="http://ift.tt/nSRXKP"
xmlns:web="http://ift.tt/LU8AHS"
xsi:schemaLocation="http://ift.tt/nSRXKP http://ift.tt/1eWqHMP"
id="WebApp_ID"
version="3.0">
<display-name>CRUDWebAppMavenized</display-name>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value> /WEB-INF/WebSocketConfig.xml</param-value>

</context-param>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>
</web-app>


spring-servlet.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:p="http://ift.tt/1jdM0fE"
xmlns:jee="http://ift.tt/OpNaZ5"
xmlns:aop="http://ift.tt/OpNdV1"
xmlns:tx="http://ift.tt/OGfeU2"
xmlns:context="http://ift.tt/GArMu7"
xmlns:util="http://ift.tt/OGfeTW"
xmlns:mvc="http://ift.tt/1bHqwjR"
xsi:schemaLocation="
http://ift.tt/GArMu6 http://ift.tt/QEDs1e
http://ift.tt/GArMu7 http://ift.tt/QEDs1k
http://ift.tt/OpNaZ5 http://ift.tt/1feTnjL
http://ift.tt/OGfeU2 http://ift.tt/1cQrvTl
http://ift.tt/OpNdV1 http://ift.tt/QEDs1g
http://ift.tt/OGfeTW http://ift.tt/1bFKsJT
http://ift.tt/1bHqwjR http://ift.tt/1bVJL9q">

<mvc:annotation-driven/>
<mvc:default-servlet-handler/>

<context:annotation-config />

<context:component-scan base-package="com.springws.test" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<tx:annotation-driven />

</beans>


WebSocketConfig.java



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();
}


}


Libraries


Libraries


But in browser console


its giving the following output



Opening Web Socket...
stomp.js:145 Web Socket Opened...
stomp.js:145 >>> CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000

Whoops! Lost connection to undefined


why its losing connection ?


Aucun commentaire:

Enregistrer un commentaire