2015-07-22 00:58:34 +00:00
|
|
|
'use strict';
|
2015-07-23 02:58:18 +00:00
|
|
|
/*global Promise*/
|
2015-07-22 00:58:34 +00:00
|
|
|
|
2015-07-23 02:58:18 +00:00
|
|
|
var wsses = {};
|
|
|
|
|
|
|
|
function createApp(server, options) {
|
|
|
|
|
|
|
|
if (wsses[options.filename]) {
|
|
|
|
return Promise.resolve(wsses[options.filename]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return require('./wrapper').create(options).then(function (db) {
|
2015-07-24 22:11:10 +00:00
|
|
|
|
2015-07-23 02:58:18 +00:00
|
|
|
var url = require('url');
|
2015-07-24 22:11:10 +00:00
|
|
|
//var express = require('express');
|
|
|
|
//var app = express();
|
2015-07-23 02:58:18 +00:00
|
|
|
var wss = server.wss;
|
2015-07-22 00:58:34 +00:00
|
|
|
|
2015-07-24 22:11:10 +00:00
|
|
|
function app(req, res) {
|
|
|
|
res.end('NOT IMPLEMENTED');
|
|
|
|
}
|
|
|
|
|
2015-07-23 02:58:18 +00:00
|
|
|
wss.on('connection', function (ws) {
|
2015-07-24 22:11:10 +00:00
|
|
|
|
2015-07-23 02:58:18 +00:00
|
|
|
var location = url.parse(ws.upgradeReq.url, true);
|
|
|
|
// you might use location.query.access_token to authenticate or share sessions
|
|
|
|
// or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312
|
2015-07-22 00:58:34 +00:00
|
|
|
|
2015-07-23 02:58:18 +00:00
|
|
|
ws.__session_id = location.query.session_id || Math.random();
|
2015-07-22 00:58:34 +00:00
|
|
|
|
2015-07-23 02:58:18 +00:00
|
|
|
ws.on('message', function (buffer) {
|
|
|
|
var cmd;
|
2015-07-22 00:58:34 +00:00
|
|
|
|
2015-07-23 02:58:18 +00:00
|
|
|
try {
|
|
|
|
cmd = JSON.parse(buffer.toString('utf8'));
|
|
|
|
} catch(e) {
|
2015-07-24 08:57:17 +00:00
|
|
|
console.error('[ERROR] parse json');
|
|
|
|
console.error(e);
|
|
|
|
console.error(buffer);
|
|
|
|
console.error();
|
2015-07-23 02:58:18 +00:00
|
|
|
ws.send(JSON.stringify({ type: 'error', value: { message: e.message, code: "E_PARSE_JSON" } }));
|
2015-07-24 08:57:17 +00:00
|
|
|
return;
|
2015-07-23 02:58:18 +00:00
|
|
|
}
|
2015-07-22 00:58:34 +00:00
|
|
|
|
2015-07-23 02:58:18 +00:00
|
|
|
switch(cmd.type) {
|
|
|
|
case 'init':
|
2015-07-29 00:04:54 +00:00
|
|
|
db[cmd.func].apply(db, cmd.args).then(function () {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
var myself;
|
|
|
|
|
|
|
|
if (args[0] === db) {
|
|
|
|
args = [];
|
|
|
|
myself = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
id: cmd.id
|
|
|
|
, self: myself
|
|
|
|
, args: args
|
|
|
|
//, this: this
|
|
|
|
}));
|
|
|
|
});
|
2015-07-23 02:58:18 +00:00
|
|
|
break;
|
2015-07-22 00:58:34 +00:00
|
|
|
|
2015-07-23 02:58:18 +00:00
|
|
|
case 'rpc':
|
2015-07-29 00:04:54 +00:00
|
|
|
if (!db._initialized) {
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
type: 'error'
|
|
|
|
, id: cmd.id
|
|
|
|
, args: [{ message: 'database has not been initialized' }]
|
|
|
|
, error: { message: 'database has not been initialized' }
|
|
|
|
}));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-24 08:57:17 +00:00
|
|
|
cmd.args.push(function () {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
2015-07-29 00:04:54 +00:00
|
|
|
var myself;
|
|
|
|
|
|
|
|
if (args[0] === db) {
|
|
|
|
args = [];
|
|
|
|
myself = true;
|
|
|
|
}
|
2015-07-24 08:57:17 +00:00
|
|
|
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
this: this
|
|
|
|
, args: args
|
2015-07-29 00:04:54 +00:00
|
|
|
, self: myself
|
2015-07-24 08:57:17 +00:00
|
|
|
, id: cmd.id
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
db[cmd.func].apply(db, cmd.args);
|
2015-07-23 02:58:18 +00:00
|
|
|
break;
|
2015-07-22 00:58:34 +00:00
|
|
|
|
2015-07-23 02:58:18 +00:00
|
|
|
default:
|
2015-07-24 22:11:10 +00:00
|
|
|
throw new Error('UNKNOWN TYPE');
|
|
|
|
//break;
|
2015-07-23 02:58:18 +00:00
|
|
|
}
|
2015-07-22 00:58:34 +00:00
|
|
|
|
2015-07-23 02:58:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
ws.send(JSON.stringify({ type: 'session', value: ws.__session_id }));
|
2015-07-22 00:58:34 +00:00
|
|
|
});
|
|
|
|
|
2015-07-23 02:58:18 +00:00
|
|
|
app.masterClient = db;
|
|
|
|
wsses[options.filename] = app;
|
|
|
|
|
|
|
|
return app;
|
2015-07-22 00:58:34 +00:00
|
|
|
});
|
2015-07-23 02:58:18 +00:00
|
|
|
}
|
2015-07-22 00:58:34 +00:00
|
|
|
|
2015-07-23 02:58:18 +00:00
|
|
|
function create(options) {
|
2015-07-24 22:11:10 +00:00
|
|
|
var server = require('http').createServer();
|
|
|
|
var WebSocketServer = require('ws').Server;
|
|
|
|
var wss = new WebSocketServer({ server: server });
|
|
|
|
//var port = process.env.PORT || process.argv[0] || 4080;
|
2015-07-22 00:58:34 +00:00
|
|
|
|
2015-07-24 22:11:10 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var ps = [];
|
2015-07-23 02:58:18 +00:00
|
|
|
|
2015-07-24 22:11:10 +00:00
|
|
|
ps.push(new Promise(function (resolve) {
|
2015-07-23 02:58:18 +00:00
|
|
|
fs.unlink(options.sock, function () {
|
|
|
|
// ignore error when socket doesn't exist
|
|
|
|
|
2015-07-24 22:11:10 +00:00
|
|
|
server.listen(options.sock, resolve);
|
2015-07-23 02:58:18 +00:00
|
|
|
});
|
2015-07-24 22:11:10 +00:00
|
|
|
}));
|
2015-07-23 02:58:18 +00:00
|
|
|
|
2015-07-24 22:11:10 +00:00
|
|
|
ps.push(createApp({ server: server, wss: wss }, options).then(function (app) {
|
|
|
|
server.on('request', app);
|
|
|
|
return { masterClient: app.masterClient };
|
|
|
|
}));
|
|
|
|
|
|
|
|
return Promise.all(ps).then(function (results) {
|
|
|
|
return results[1];
|
2015-07-22 00:58:34 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.create = create;
|