e1dab4df by Lorenzo Mangani Committed by GitHub

Update server.js

1 parent 40ac9c72
Showing 1 changed file with 11 additions and 5 deletions
......@@ -5,6 +5,7 @@
*/
const url = require('url');
var rimraf = require("rimraf");
const Gun = require('gun/gun');
require('./gun-ws.js');
const http = require('http');
......@@ -13,9 +14,12 @@ var server = http.createServer();
// LRU with last used sockets
const QuickLRU = require('quick-lru');
const lru = new QuickLRU({maxSize: 10});
//var wss_event = new WebSocket.Server({ noServer: true});
var evict = function(key,value){
console.log('!!!!!!!!!!!!!!! Garbage Collect',key);
var uuid = value.gun.opt()._.opt.ws.uuid;
if(uuid) rimraf("/tmp/"+uuid, function () { console.log("Cleaned up ID",uuid); });
}
const lru = new QuickLRU({maxSize: 10, onEviction: evict });
server.on('upgrade', async function (request, socket, head) {
var pathname = url.parse(request.url).pathname || '/gun';
......@@ -30,12 +34,14 @@ server.on('upgrade', async function (request, socket, head) {
// Create Node
console.log('Create id',pathname);
// NOTE: Only works with lib/ws.js shim allowing a predefined WS as ws.web parameter in Gun constructor
var uuid = Math.random().toString(36).substring(7);
gun.server = new WebSocket.Server({ noServer: true, path: pathname});
console.log('set peer',request.headers.host+pathname);
gun.gun = new Gun({
peers:[], // should we use self as peer?
localStorage: false,
file: "tmp/"+Math.random().toString(36).substring(7), // neesa cleanup or better mechanism to isolate
localStorage: false,
uuid: uuid,
file: "tmp/"+uuid,
multicast: false,
ws: { noServer: true, path: pathname, web: gun.server },
web: gun.server
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!