Skip to content

Instantly share code, notes, and snippets.

@ozten
Created May 22, 2012 18:42
Show Gist options
  • Select an option

  • Save ozten/2770854 to your computer and use it in GitHub Desktop.

Select an option

Save ozten/2770854 to your computer and use it in GitHub Desktop.
A simple single connection proxy
var net = require('net');
var ops = 0;
var proxy = net.createServer(function(bid_conn) { //'connection' listener
console.log('BrowserID (re)-CONNected ', ops);
if (ops < 8 || ops > 10) {
ops = 0;
console.log('RESETTING OPS, allowing new connection');
var mysql_conn = net.connect(3306, 'localhost');
mysql_conn.on('connect', function () {
console.log('client CONNected');
});
mysql_conn.on('data', function (data) {
console.log('MySQL sent data');
console.log(data.toString('utf8'));
bid_conn.write(data);
});
mysql_conn.on('end', function () {
console.log('client connection ENDed');
bid_conn.end();
});
} else {
ops++;
console.log('BAIL');
}
bid_conn.on('data', function (data) {
console.log('BrowserID sent data ', ops);
console.log(data.toString('utf8'));
ops++;
if (ops > 8) {
bid_conn.emit('timeout');
// BrowserID will close and re-open connection, which causes ops to be reset to 0
} else {
mysql_conn.write(data);
}
});
bid_conn.on('end', function() {
console.log('BrowserID DISCONNected');
if (mysql_conn) mysql_conn.end();
});
});//createServer
proxy.listen(3307, function() { //'listening' listener
console.log('mysql proxy listening on 3307');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment