Skip to content

Instantly share code, notes, and snippets.

@maritz
Forked from anonymous/node-to-mem.js
Created November 12, 2010 04:05
Show Gist options
  • Save maritz/673710 to your computer and use it in GitHub Desktop.
Save maritz/673710 to your computer and use it in GitHub Desktop.
var http = require('http')
, sys = require('sys')
, url = require('url')
, path = require('path')
, fs = require('fs')
, net = require('net');
var crlf = "\r\n";
var crlf_len = crlf.length;
var mc_server = {
ms: ''
, buffer: ''
, conn: function (port, host, callback) {
var self = this;
if (!this.ms) {
this.ms = new net.createConnection(port, host);
this.ms.on('connect', function () {
sys.puts('Connected to server: localhost:' + port);
callback(self.ms);
});
this.ms.on('error', function (exception) {
sys.puts('Error: ' + exception);
});
this.ms.on('data', function (data) {
mc_server.buffer += data;
sys.puts(data);
});
this.ms.on('end', function () {
if (mc_server.ms && mc_server.ms.readyState) {
mc_server.ms = null;
sys.puts('Disconnected!');
}
});
} else {
this.ms.on('connect', funtion() {
callback(self.ms);
}
}
}
, query: function (stmt) {
this.conn(11211, 'localhost', function (ms) {
//sys.puts(sys.inspect(ms));
ms.write(stmt);
});
}
};
mc_server.query('get json_foo ' + crlf);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment