more testing (almost usable)

This commit is contained in:
AJ ONeal 2015-07-24 02:57:17 -06:00
parent 696ac1ed1c
commit 39ecdfed00
2 changed files with 55 additions and 6 deletions

View File

@ -71,11 +71,13 @@ function create(opts) {
// TODO maybe use HTTP POST instead?
return getConnection(opts).then(function (ws) {
if (ws.masterClient) {
console.log('[MASTER CLIENT] found');
return ws.masterClient;
}
var db = {};
var proto = sqlite3real.Database.prototype;
var messages = [];
function rpc(fname, args) {
var id;
@ -86,31 +88,52 @@ function create(opts) {
cb = args.pop();
}
ws.send({
console.log('fname, args');
console.log(fname, args);
ws.send(JSON.stringify({
type: 'rpc'
, func: fname
, args: args
, filename: opts.filename
, id: id
});
}));
if (!cb) {
return;
}
function onMessage(data) {
if (!data || 'object' !== typeof data) {
var cmd;
try {
cmd = JSON.parse(data.toString('utf8'));
} catch(e) {
console.error('[ERROR] in client, from sql server parse json');
console.error(e);
console.error(data);
console.error();
//ws.send(JSON.stringify({ type: 'error', value: { message: e.message, code: "E_PARSE_JSON" } }));
return;
}
if (data.id !== id) {
if (cmd.id !== id) {
return;
}
cb.apply(data.this, data.args);
//console.log('onMessage data');
//console.log(cmd);
cb.apply(cmd.this, cmd.args);
if ('on' !== fname) {
var index = messages.indexOf(onMessage);
messages.splice(index, 1);
}
}
ws.on('message', onMessage);
messages.push(onMessage);
}
db.sanitize = require('./wrapper').sanitize;
@ -125,6 +148,15 @@ function create(opts) {
});
ws.on('message', function (data) {
messages.forEach(function (fn) {
try {
fn(data);
} catch(e) {
// ignore
}
});
});
// serialize
// parallel

View File

@ -24,12 +24,18 @@ function createApp(server, options) {
ws.__session_id = location.query.session_id || Math.random();
ws.on('message', function (buffer) {
console.log('[SERVER MESSAGE]', buffer);
var cmd;
try {
cmd = JSON.parse(buffer.toString('utf8'));
} catch(e) {
console.error('[ERROR] parse json');
console.error(e);
console.error(buffer);
console.error();
ws.send(JSON.stringify({ type: 'error', value: { message: e.message, code: "E_PARSE_JSON" } }));
return;
}
switch(cmd.type) {
@ -37,6 +43,17 @@ function createApp(server, options) {
break;
case 'rpc':
cmd.args.push(function () {
var args = Array.prototype.slice.call(arguments);
ws.send(JSON.stringify({
this: this
, args: args
, id: cmd.id
}));
});
db[cmd.func].apply(db, cmd.args);
break;
default: