Skip to content

Instantly share code, notes, and snippets.

@AdamMagaluk
Last active August 29, 2015 14:24
Show Gist options
  • Save AdamMagaluk/e68c988c87bb8e89d2cf to your computer and use it in GitHub Desktop.
Save AdamMagaluk/e68c988c87bb8e89d2cf to your computer and use it in GitHub Desktop.
var spdy = require('spdy');
var http = require('http');
var agent = spdy.createAgent({
host: 'localhost',
port: 3000,
spdy: {
plain: true,
ssl: false
}
});
function startStream() {
http.get({
host: 'localhost',
port: 3000,
agent: agent,
path: '/start/push'
}, function(response) {
}).end();
}
for (var i=0; i<75; i++) {
startStream();
}
function printStreams() {
var streams = agent._spdyState.connection._spdyState.streams;
console.log('Streams:', Object.keys(streams).length);
}
setInterval(printStreams, 1000);
agent.on('push', function(stream) {
var length = Number(stream.headers['content-length']);
var data = new Buffer(length);
var idx = 0;
var d = null;
stream.on('readable', function() {
while (d = stream.read()) {
for (var i=0; i<d.length;i++) {
data[idx++] = d[i];
}
};
});
stream.on('error', function(err) {
console.log('stream error:', err)
});
stream.on('end', function() {
var body = null;
body = JSON.parse(data.toString());
// stream.connection.close();
stream.destroy();
});
});
var http = require('http');
var spdy = require('spdy');
var heapdump = require('heapdump');
var argo = require('argo');
var titan = require('titan');
var WebSocketServer = require('ws').Server;
var SpdyAgent = require('./spdy_agent');
var WebSocket = require('./web_socket');
var infoStarted = false;
var server = spdy.createServer({
windowSize: 1024 * 1024,
plain: true,
ssl: false
}, function(req, res) {
var send = function() {
var data = new Buffer(JSON.stringify({ timestamp: new Date().getTime() }));
var stream = res.push('/event', { 'Host': 'fog.argo.cx',
'Content-Length': data.length
});
stream.on('error', function(err) {
if (err.code !== 'RST_STREAM') {
console.error('stream err:', err);
}
});
stream.end(data);
};
res.writeHead(200);
var timer = setInterval(send, 5);
req.on('close', function() {
clearInterval(timer);
});
if (!infoStarted) {
infoStarted = true;
function printStreams() {
var streams = req.connection.connection._spdyState.streams;
console.log('Streams:', Object.keys(streams).length);
}
setInterval(printStreams, 1000);
}
}).listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment