dimanche 12 avril 2015

Unable to send message to websocket from another service

I followed the tutorial here to setup websockets within my web application, and below is the resultant code. Now I thought if I write to the topic from another class (LocationManagerImpl.java) in my application, I would be able to send the message to all the clients who are subscribed to the topic. But, it seems the client isn't getting the message. If I debug and also check the logs, I get indications that my message has been written to the topic, but it isn't being read by the client. I am unable to figure out what I am doing wrong here. Please help.


HelloController.java



@Controller
public class HelloController {

@MessageMapping("/hello")
@SendTo("/topic/greetings")
public HelloResponse hello(HelloRequest request) throws Exception {
Thread.sleep(3000);
HelloResponse response = new HelloResponse();
response.setContent(request.getContent() + " ... appending response");
return response;
}
}


WebSocketConfiguration.java



@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/hello").withSockJS();
}
}


hello.js



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('/aegis/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() {
if (stompClient != null) {
stompClient.disconnect();
}
setConnected(false);
console.log("Disconnected");
}

function sendName() {
var name = document.getElementById('name').value;
stompClient.send("/app/hello", {}, JSON.stringify({ 'content': 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);
}


sample.jsp



<%@ include file="/common/taglibs.jsp"%>

<head>
<title><fmt:message key="sample.title"/></title>
<meta name="menu" content="SampleMenu"/>
<script src="//code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="//cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js"></script>
<script src="scripts/stomp.js"></script>
<script src="scripts/hello.js"></script>
<script>
$(document).ready(function(){
disconnect();
});
</script>
</head>

<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>


LocationManagerImpl.java



@Service("locationManager")
public class LocationManagerImpl extends GenericManagerImpl<TripTrack,Long> implements LocationManager {

@Autowired
private SimpMessageSendingOperations messagingTemplate;

@Override
public Response locations(JsonRequest loc) {

//... some code here

// Send to UI
messagingTemplate.convertAndSend("/topic/greetings", "{\"content\": \"hello\"}");

//... some more code here
}
}

Aucun commentaire:

Enregistrer un commentaire