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? // TODO maybe use HTTP POST instead?
return getConnection(opts).then(function (ws) { return getConnection(opts).then(function (ws) {
if (ws.masterClient) { if (ws.masterClient) {
console.log('[MASTER CLIENT] found');
return ws.masterClient; return ws.masterClient;
} }
var db = {}; var db = {};
var proto = sqlite3real.Database.prototype; var proto = sqlite3real.Database.prototype;
var messages = [];
function rpc(fname, args) { function rpc(fname, args) {
var id; var id;
@ -86,31 +88,52 @@ function create(opts) {
cb = args.pop(); cb = args.pop();
} }
ws.send({ console.log('fname, args');
console.log(fname, args);
ws.send(JSON.stringify({
type: 'rpc' type: 'rpc'
, func: fname , func: fname
, args: args , args: args
, filename: opts.filename , filename: opts.filename
, id: id , id: id
}); }));
if (!cb) { if (!cb) {
return; return;
} }
function onMessage(data) { 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; return;
} }
if (data.id !== id) { if (cmd.id !== id) {
return; 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; 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 // serialize
// parallel // parallel

View File

@ -24,12 +24,18 @@ function createApp(server, options) {
ws.__session_id = location.query.session_id || Math.random(); ws.__session_id = location.query.session_id || Math.random();
ws.on('message', function (buffer) { ws.on('message', function (buffer) {
console.log('[SERVER MESSAGE]', buffer);
var cmd; var cmd;
try { try {
cmd = JSON.parse(buffer.toString('utf8')); cmd = JSON.parse(buffer.toString('utf8'));
} catch(e) { } 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" } })); ws.send(JSON.stringify({ type: 'error', value: { message: e.message, code: "E_PARSE_JSON" } }));
return;
} }
switch(cmd.type) { switch(cmd.type) {
@ -37,6 +43,17 @@ function createApp(server, options) {
break; break;
case 'rpc': 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; break;
default: default: