Skip to content

Instantly share code, notes, and snippets.

@jmyrland
Last active December 11, 2015 18:09
Show Gist options
  • Save jmyrland/4639758 to your computer and use it in GitHub Desktop.
Save jmyrland/4639758 to your computer and use it in GitHub Desktop.
socket.io xhr-polling test.
var i = 0,
io = require('socket.io'),
http = require('http');
var server = http.createServer().listen(80);
io = io.listen(server);
io.set('transports', [ 'xhr-polling' ]);
io.sockets.on('connection', function(socket){
i++;
socket.log.info('Client ' + i + ' connected.');
});
var io = require("socket.io-client")
var connectionInterval = 500;
var numberOfUsersToConnect = 100;
var connections = 0;
var connectionTimestampSum = 0;
var options = {
transports: ['xhr-polling'],
'force new connection': true
};
// UPDATE: This fixes the problem!
var http = require('http');
http.globalAgent.maxSockets = 100;
http.Agent.maxSockets = 100;
for (var i = 1; i <= numberOfUsersToConnect; i++) {
(function (index) {
setTimeout(function () {
console.log(index + "/" + numberOfUsersToConnect + " tries to connect!");
var time = Date.now(),
socket = io.connect("http://localhost", options)
socket.on('connect', function () {
connections++;
connectionTimestampSum += Date.now() - time;
console.log((connections) + "/" + numberOfUsersToConnect + " connected! (" + (Date.now() - time )+ "ms)");
if (connections == numberOfUsersToConnect) {
console.log("Average connect time: " + Math.round(connectionTimestampSum / connections) + " ms!");
console.log("");
}
});
}, index * connectionInterval);
})(i);
}
@jmyrland
Copy link
Author

@tolgaytoklar
Copy link

How can I use require command on the browser ? I am getting require is not defined error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment