Phantomjs 2.0 SocketIO takes 1000ms for each connection

201 views
Skip to first unread message

Công Thắng

unread,
Jan 11, 2015, 9:39:35 PM1/11/15
to phan...@googlegroups.com

I'm using PhantomJS 2.0 and trying to test socket.io between PhantomJS and NodeJS. It works but it takes alot of time for each connection (about 1000ms). I have tried on Chrome and it takes only 1-2ms. It's seems that socket.io in page.evaluate of PhantomJS is not socket.io! Here is my testing code using CasperJS:

var casper = require('casper').create({
    clientScripts: ['jquery-1.11.1.min.js',["socket.io.js"]]
});
casper.start('http://localhost:8080');
casper.page.onConsoleMessage = function(msg) {
  console.log(msg);
};

};
casper.then(function(){
    this.evaluate(function(){
        var socket = io.connect('http://localhost:8080');
        socket.on('news', function (data) {
        socket.emit('senddata', { time: new Date().getTime() });
      });
    })
})
casper.wait(5000);
casper.run();

On NodeJS server I use this code:

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(8080);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('senddata', function (data) {
    console.log(new Date().getTime()-data.time);
  });
});

The result of time measuring is always >= 1000ms. I need to tranfer data in serveral ms. Can anyone help me. Thanks

James M. Greene

unread,
Jan 13, 2015, 10:03:10 PM1/13/15
to phan...@googlegroups.com
Is there a reason your Casper setup's "clientScripts" array has the "socket.io.js" file path reference in a nested array?

var casper = require('casper').create({
    clientScripts: ['jquery-1.11.1.min.js',["socket.io.js"]]
});

I'm honestly surprised that it is working at all, given that oddity.

Sincerely,
    James Greene


--
You received this message because you are subscribed to the Google Groups "phantomjs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phantomjs+...@googlegroups.com.
Visit this group at http://groups.google.com/group/phantomjs.
For more options, visit https://groups.google.com/d/optout.

Công Thắng

unread,
Jan 15, 2015, 11:18:38 PM1/15/15
to phan...@googlegroups.com
Actually there is no reason, I just forget to remove it. Thanks

Vào 10:03:10 UTC+7 Thứ Tư, ngày 14 tháng 1 năm 2015, James Greene đã viết:
Reply all
Reply to author
Forward
0 new messages