0

I am trying to create a websocket using this tutorial. Everything is good except that when I try to open websocket connection I get the following error on my browser console and nothing happens at all. Please help me out.

Error:

Opening web socket... GET http://localhost:8080/hello/info?t=1445510124841 404 (Not Found) Whoops! Lost connection to http://localhost:8080/hello 

TestController.java

package com.test.controller; import java.util.Set; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.test.dto.EmployeeDTO; import com.test.dto.Greeting; import com.test.dto.HelloMessage; import com.test.service.EmployeeService; @Controller public class TestController { @MessageMapping("/hello") @SendTo("/topic/greetings") public Greeting greeting(HelloMessage message) throws Exception { Thread.sleep(3000); // simulated delay System.out.println(message.getName()); return new Greeting("Hello, " + message.getName() + "!"); } @RequestMapping(value = "/", method = RequestMethod.GET) public ModelAndView home(HttpServletRequest request, HttpServletResponse response){ return new ModelAndView("test"); } } 

WebSocketConfig.java

package com.test.websocket; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; 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 @EnableWebSocketMessageBroker public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{ @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic"); config.setApplicationDestinationPrefixes("/app"); } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { // TODO Auto-generated method stub registry.addEndpoint("/hello").withSockJS(); } } 

Test.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <title>Hello WebSocket</title> <script src="${pageContext.servletContext.contextPath}/js/sockjs-0.3.4.js"></script> <script src="${pageContext.servletContext.contextPath}/js/stomp.min.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('${pageContext.servletContext.contextPath}/hello'); stompClient = Stomp.over(socket); stompClient.connect({}, function(frame) { setConnected(true); console.log('Connected: ' + frame); stompClient.subscribe('/topic/greetings', function(greeting){ showGreeting(JSON.parse(greeting.body).content); }); }); } function disconnect() { 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> 
4
  • Some relevant code would be nice. Commented Oct 22, 2015 at 11:54
  • I hope the added information is enough Commented Oct 22, 2015 at 12:13
  • Where is your controller mapping for /hello/info which you have shown in your tiny error log. Commented Oct 22, 2015 at 12:14
  • I have not created any mapping for /hello/info. In fact I am not calling this url. It is called automatically when stompClient.connect method is called Commented Oct 22, 2015 at 12:20

1 Answer 1

2

The issue has been resolved. I had not added the WebSocket configuration file to component scan package in my spring configuration file and hence no bean for websocket was getting created.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.