Search code examples
javascriptjqueryatmosphereatmosphere.js

Atmosphere jQuery responseBody always starts with length pipe "5|"


I'm trying to work with the atmosphere-framework. Sending and receiving data works pretty well but i think there is some incompatibility or other problem when jquery reads the received message:

When the server sends back "Text" like in the code of my TestService.java the jQuery-client receives a responseBody "4|Text" (=> length of the text|text). Of course i could just cut out the number and the pipe but i'm trying to figure out what's the problem in the first place.

So when i load the html-page at first i get an alert saying just "|". Then, everytime i click the send-button i get an alert "4|Text". Sometimes after a timeout of 2 minutes i even get an alert "1|X".

atmosphere-runtime version is: 2.2.7
jQuery-Version is: 2.1.4
jquery.atmosphere-version is: 2.2.7-jquery from https://github.com/Atmosphere/atmosphere-javascript

I also tried other combinations of versions but it never fixed the problem.

I got the same problem on Glassfish, but at the moment i am using WildFly 8.1. The problem is the same for Opera, Firefox and Chromium.

My TestService class looks like this, i think it is working correctly:

@ManagedService(path="/")
public class TestService {

private final Logger logger = LoggerFactory.getLogger(TestService.class);

@Ready
public void onReady(AtmosphereResource r) {
    logger.info("Resource {} connected", r.uuid());
}

@Message(decoders = {JacksonDecoder.class})
public String onMessage(AtmosphereResource r, Test t) throws IOException {
    logger.info("Message received = " + t.toString());
    return "Text";
}   
}

The code for sending and receiving on the jQuery-client looks like this:

var socket = $.atmosphere;
var usersUpdateReq = {
    url: '/NewQuiz-web/quiz',
    transport: 'websocket',
    fallbackTransport: 'long-polling',
    contentType: "application/javascript"
};
usersUpdateReq.onMessage = function(resp) {
    if(resp.status === 200) {
    alert("received: >>" + resp.responseBody + "<<");
    console.log(resp.responseBody);
    eval(resp.responseBody);
    }
};
usersUpdateReq.onError = function(resp) {
    alert('Error');
    console.log(resp);
};

var usersUpdateSubSock = socket.subscribe(usersUpdateReq);

Sending an example-request:

$("#sendb").click(function() {
    usersUpdateSubSock.push($.stringifyJSON({ name : "TestTest" }));
});

Please let me know if you need additional information. Thank you people!


Solution

  • Both behaviors are expected.

    The first one (strings like 4|text) is to track the length of your response (see more info here). In that way the client will validate that the response content received is complete. But, to have this fully configured, you need to add the following property to your request object in the client side (in your case usersUpdateReq):

    trackMessageLength : true
    

    The second one (strings like 1|X) represents the heartbeat the keep alive the connection between the server and client. See more info here. And of course you can configure it in the server and client side.