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 @@ ...@@ -5,6 +5,7 @@
5 */ 5 */
6 6
7 const url = require('url'); 7 const url = require('url');
8 var rimraf = require("rimraf");
8 const Gun = require('gun/gun'); 9 const Gun = require('gun/gun');
9 require('./gun-ws.js'); 10 require('./gun-ws.js');
10 const http = require('http'); 11 const http = require('http');
...@@ -13,9 +14,12 @@ var server = http.createServer(); ...@@ -13,9 +14,12 @@ var server = http.createServer();
13 14
14 // LRU with last used sockets 15 // LRU with last used sockets
15 const QuickLRU = require('quick-lru'); 16 const QuickLRU = require('quick-lru');
16 const lru = new QuickLRU({maxSize: 10}); 17 var evict = function(key,value){
17 18 console.log('!!!!!!!!!!!!!!! Garbage Collect',key);
18 //var wss_event = new WebSocket.Server({ noServer: true}); 19 var uuid = value.gun.opt()._.opt.ws.uuid;
20 if(uuid) rimraf("/tmp/"+uuid, function () { console.log("Cleaned up ID",uuid); });
21 }
22 const lru = new QuickLRU({maxSize: 10, onEviction: evict });
19 23
20 server.on('upgrade', async function (request, socket, head) { 24 server.on('upgrade', async function (request, socket, head) {
21 var pathname = url.parse(request.url).pathname || '/gun'; 25 var pathname = url.parse(request.url).pathname || '/gun';
...@@ -30,12 +34,14 @@ server.on('upgrade', async function (request, socket, head) { ...@@ -30,12 +34,14 @@ server.on('upgrade', async function (request, socket, head) {
30 // Create Node 34 // Create Node
31 console.log('Create id',pathname); 35 console.log('Create id',pathname);
32 // NOTE: Only works with lib/ws.js shim allowing a predefined WS as ws.web parameter in Gun constructor 36 // NOTE: Only works with lib/ws.js shim allowing a predefined WS as ws.web parameter in Gun constructor
37 var uuid = Math.random().toString(36).substring(7);
33 gun.server = new WebSocket.Server({ noServer: true, path: pathname}); 38 gun.server = new WebSocket.Server({ noServer: true, path: pathname});
34 console.log('set peer',request.headers.host+pathname); 39 console.log('set peer',request.headers.host+pathname);
35 gun.gun = new Gun({ 40 gun.gun = new Gun({
36 peers:[], // should we use self as peer? 41 peers:[], // should we use self as peer?
37 localStorage: false, 42 localStorage: false,
38 file: "tmp/"+Math.random().toString(36).substring(7), // neesa cleanup or better mechanism to isolate 43 uuid: uuid,
44 file: "tmp/"+uuid,
39 multicast: false, 45 multicast: false,
40 ws: { noServer: true, path: pathname, web: gun.server }, 46 ws: { noServer: true, path: pathname, web: gun.server },
41 web: gun.server 47 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!