b430667e by Lorenzo Mangani Committed by GitHub

Update server.js

1 parent 2dc3b673
...@@ -8,6 +8,7 @@ const url = require('url'); ...@@ -8,6 +8,7 @@ const url = require('url');
8 var rimraf = require("rimraf"); 8 var rimraf = require("rimraf");
9 const Gun = require('gun/gun'); 9 const Gun = require('gun/gun');
10 require('./gun-ws.js'); 10 require('./gun-ws.js');
11 require('./memdisk.js');
11 const http = require('http'); 12 const http = require('http');
12 const WebSocket = require('ws'); 13 const WebSocket = require('ws');
13 var server = http.createServer(); 14 var server = http.createServer();
...@@ -15,9 +16,8 @@ var server = http.createServer(); ...@@ -15,9 +16,8 @@ var server = http.createServer();
15 // LRU with last used sockets 16 // LRU with last used sockets
16 const QuickLRU = require('quick-lru'); 17 const QuickLRU = require('quick-lru');
17 var evict = function(key,value){ 18 var evict = function(key,value){
18 console.log('!!!!!!!!!!!!!!! Garbage Collect',key); 19 console.log('Garbage Collect',key);
19 var uuid = value.gun.opt()._.opt.ws.uuid; 20 if(key) rimraf("/tmp/"+key, function () { console.log("Cleaned up ID",key); });
20 if(uuid) rimraf("/tmp/"+uuid, function () { console.log("Cleaned up ID",uuid); });
21 } 21 }
22 const lru = new QuickLRU({maxSize: 10, onEviction: evict }); 22 const lru = new QuickLRU({maxSize: 10, onEviction: evict });
23 23
...@@ -34,14 +34,12 @@ server.on('upgrade', async function (request, socket, head) { ...@@ -34,14 +34,12 @@ server.on('upgrade', async function (request, socket, head) {
34 // Create Node 34 // Create Node
35 console.log('Create id',pathname); 35 console.log('Create id',pathname);
36 // 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);
38 gun.server = new WebSocket.Server({ noServer: true, path: pathname}); 37 gun.server = new WebSocket.Server({ noServer: true, path: pathname});
39 console.log('set peer',request.headers.host+pathname); 38 console.log('set peer',request.headers.host+pathname);
40 gun.gun = new Gun({ 39 gun.gun = new Gun({
41 peers:[], // should we use self as peer? 40 peers:[], // should we use self as peer?
42 localStorage: false, 41 localStorage: false,
43 uuid: uuid, 42 file: "tmp/"+pathname,
44 file: "tmp/"+uuid,
45 multicast: false, 43 multicast: false,
46 ws: { noServer: true, path: pathname, web: gun.server }, 44 ws: { noServer: true, path: pathname, web: gun.server },
47 web: gun.server 45 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!