Opening a page containing a socket.io connection with CasperJs / PhantomJs

I have a page that opens a websocket (socket.io) connection on a node.js server.

For testing purposes, I want to open a page with a non-browser browser using CasperJs (I also tried pure PhantomJs with the same result).

Summary:
Open socket.io connection using Chrome always works. An open socket.io connection using CasperJs always works (at least the corresponding callbacks are executed on the client and server).
Messaging (socket.emit) using Chrome always works.
Messaging (socket.emit) using CasperJs sometimes works.

I get the same behavior when I configure socket.io to use poll only instead of websocket. I know that "sometimes" is not very accurate, but I haven't found the "patter" yet when it happens.

Have you used a non-browser browser to successfully open the page with socket.io/websockets? Do you have any hints what might be the reason?

Additional Information:

My client performs

var socket = io.connect('http://localhost:3000');

      

After that, the connection method is called on the server (it prints the transport type and socket id for debugging):

io.on('connection', function (socket) {
     console.log('Client connect ('+ socket.client.conn.transport.constructor.name +'): ' + socket.id);
     ...
}

      

The result is the same as expected (socket.io uses XHR first and then updates to websocket):

Client connect (XHR): d-moyZ_D6Z6eG7SNAAAA

      

Also the connection callback is executed on the client side:

socket.on('connect', function () {
   console.log("Client successfully connected to data server. Transport type: " + socket.io.engine.transport.constructor.name);
   ...
}

      

The output on the client console is expected:

Client successfully connected to data server. Transport type: XHR

      

If I am using a regular browser, I can exchange messages without problems. If I use my casper script then messaging works sometimes . Most of the time nothing happens when I call socket.emit. The cash register script is pretty simple. I had a feeling it might be a sync issue, so I created a wait for the javascript resource that makes the connection, unchanged:

casper = require('casper').create({
    verbose: true,
    logLevel: "debug"
});

casper.start('page.html', function() {
    this.echo(this.getTitle());
});

casper.waitForResource("dataConnection.js", function() {
    this.echo('socket.io has been loaded.');
    this.wait(14000, function() {
        this.echo("I've waited for some seconds.");
    });
    this.capture('casper.png');
});

casper.on('remote.message', function(message) {
   console.log(message);
});
casper.run();

      

Disable callbacks on the client / server also run as expected. You can see that upgrading to websocket worked:

Client disconnect (WebSocket): d-moyZ_D6Z6eG7SNAAAA connectionCount: 0

      

Thank!

+3


source to share





All Articles