sqlite3-cluster.js/server.js

103 lines
2.6 KiB
JavaScript
Raw Normal View History

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) {
console.log('Create App');
if (wsses[options.filename]) {
return Promise.resolve(wsses[options.filename]);
}
return require('./wrapper').create(options).then(function (db) {
var url = require('url');
var express = require('express');
var app = express();
var wss = server.wss;
2015-07-22 00:58:34 +00:00
2015-07-23 02:58:18 +00:00
wss.on('connection', function (ws) {
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) {
2015-07-24 08:57:17 +00:00
console.log('[SERVER MESSAGE]', buffer);
2015-07-23 02:58:18 +00:00
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':
break;
2015-07-22 00:58:34 +00:00
2015-07-23 02:58:18 +00:00
case 'rpc':
2015-07-24 08:57:17 +00:00
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);
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:
break;
}
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) {
console.log('Create Server');
2015-07-22 00:58:34 +00:00
return new Promise(function (resolve) {
2015-07-23 02:58:18 +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;
console.log('options.sock');
console.log(options.sock);
var fs = require('fs');
fs.unlink(options.sock, function () {
// ignore error when socket doesn't exist
server.listen(options.sock, function () {
console.log('Listening');
});
});
createApp({ server: server, wss: wss }, options).then(function (app) {
server.on('request', app);
resolve({ masterClient: app.masterClient });
});
2015-07-22 00:58:34 +00:00
});
}
module.exports.create = create;