lundi 13 avril 2015

Cometd not publishing messages

I am working on Cometd since last few days to implement chat functionality inside spring-mvc application. However, I am having some troubles sending messages from the server and get them till the backend. I am not sure what is going wrong, as Java-Script is not showing me alerts once inside the methods, so is it that I have configured Cometd incorrectly, or some mistake in Java-script. Kindly let me know. Thanks a lot :


Code : index.jsp :



<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/main.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/cometd.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/jquery.cometd.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/application.js"></script>

<script type="text/javascript">
var config = {
contextPath: '${pageContext.request.contextPath}'
};
</script>
</head>
<body>

<div id="body"></div>
<button id="greeter">
Send Hello to Server
</button>
</body>
</html>


Code : application.js :



(function($)
{
var cometd = $.cometd;

$(document).ready(function()
{

function _connectionEstablished()
{
alert("Connection established");

$('#body').append('<div>CometD Connection Established</div>');
}

function _connectionBroken()
{
alert("Connection broken");

$('#body').append('<div>CometD Connection Broken</div>');
}

function _connectionClosed()
{
alert("Connection closed");

$('#body').append('<div>CometD Connection Closed</div>');
}

// Function that manages the connection status with the Bayeux server
var _connected = false;
function _metaConnect(message)
{

alert("Meta Connect");

if (cometd.isDisconnected())
{
alert("Cometd disconnected");
_connected = false;
_connectionClosed();
return;
}

var wasConnected = _connected;
_connected = message.successful === true;
if (!wasConnected && _connected)
{
alert("Was Connected");

_connectionEstablished();
}
else if (wasConnected && !_connected)
{
alert("Not Connected");

_connectionBroken();
}
}

// Function invoked when first contacting the server and
// when the server has lost the state of this client
function _metaHandshake(handshake)
{

alert("Handshake successful 1");

if (handshake.successful === true)
{
alert("Handshake successful");
cometd.batch(function()
{
cometd.subscribe('/hello', function(message)
{
alert("Subscription successful");

$('#body').append('<div>Server Says: ' + message.data.greeting + '</div>');
});
// Publish on a service channel since the message is for the server only
cometd.publish('/service/hello', { name: 'World' });
});
}
}

// Disconnect when the page unloads
$(window).unload(function()
{
cometd.disconnect(true);
});

var cometURL = location.protocol + "//" + location.host + config.contextPath + "/cometd";
alert("Cometd url is "+cometURL);
cometd.configure({
url: cometURL,
logLevel: 'debug'
});

cometd.addListener('/meta/handshake', _metaHandshake);
cometd.addListener('/meta/connect', _metaConnect);


// For below line, it also warns me that it expects 2 parameters



cometd.handshake();
});
})(jQuery);


MessageReciever :



@Service
public class MessageReceiver {

@Listener("/service/hello")
public void processClientMessage(ServerSession serverSession, ServerMessage serverMessage){


System.out.println("Recieved message is "+serverMessage.getData()+" Session id is "+serverSession.getId());


MessageSender.Update update = new MessageSender.Update(Long.valueOf(serverMessage.getChannel()),
serverMessage.getData().toString(),String.valueOf(serverMessage.getClientId()).toLowerCase(),
new Timestamp(System.currentTimeMillis()));
MessageSender messageSender = new MessageSender();
messageSender.sendMessage(update);
System.out.println("Recieved message is "+serverMessage.getData()+" Session id is "+serverSession.getId());
}
}


My code never reaches the System.out.println.


web.xml :



<!-- Cometd servlet mappings -->
<servlet-mapping>
<servlet-name>cometd</servlet-name>
<url-pattern>/cometd</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>cometd</servlet-name>
<servlet-class>org.cometd.server.CometDServlet</servlet-class>
<!-- <init-param>
<param-name>transports</param-name>
<param-value>org.cometd.websocket.server.WebSocketTransport</param-value>
</init-param>-->
<load-on-startup>1</load-on-startup>
</servlet>


Any help would be nice. Thanks a lot. :-)


Aucun commentaire:

Enregistrer un commentaire