40ac9c72 by Lorenzo Mangani Committed by GitHub

Update server.js

1 parent 4828c671
Showing 1 changed file with 11 additions and 6 deletions
1 /* Gun Multi-WS Monster */
2 /* Spawn multiple Gun WebSockets from the same HTTP/HTTPS server
3 * Each Gun is scoped to its ws.path and intended for ephemeral usage
4 * MIT Licensed (C) QXIP 2020
5 */
6
1 const url = require('url'); 7 const url = require('url');
2 const Gun = require('gun/gun'); 8 const Gun = require('gun/gun');
3 require('./gun-ws.js'); 9 require('./gun-ws.js');
...@@ -7,7 +13,7 @@ var server = http.createServer(); ...@@ -7,7 +13,7 @@ var server = http.createServer();
7 13
8 // LRU with last used sockets 14 // LRU with last used sockets
9 const QuickLRU = require('quick-lru'); 15 const QuickLRU = require('quick-lru');
10 const lru = new QuickLRU({maxSize: 1}); 16 const lru = new QuickLRU({maxSize: 10});
11 17
12 //var wss_event = new WebSocket.Server({ noServer: true}); 18 //var wss_event = new WebSocket.Server({ noServer: true});
13 19
...@@ -29,7 +35,8 @@ server.on('upgrade', async function (request, socket, head) { ...@@ -29,7 +35,8 @@ server.on('upgrade', async function (request, socket, head) {
29 gun.gun = new Gun({ 35 gun.gun = new Gun({
30 peers:[], // should we use self as peer? 36 peers:[], // should we use self as peer?
31 localStorage: false, 37 localStorage: false,
32 file: Math.random().toString(36).substring(7), 38 file: "tmp/"+Math.random().toString(36).substring(7), // neesa cleanup or better mechanism to isolate
39 multicast: false,
33 ws: { noServer: true, path: pathname, web: gun.server }, 40 ws: { noServer: true, path: pathname, web: gun.server },
34 web: gun.server 41 web: gun.server
35 }); 42 });
...@@ -39,10 +46,8 @@ server.on('upgrade', async function (request, socket, head) { ...@@ -39,10 +46,8 @@ server.on('upgrade', async function (request, socket, head) {
39 } 46 }
40 if (gun.server){ 47 if (gun.server){
41 // Handle Request 48 // Handle Request
42 console.log('handle connection...');
43 //ws.emit('connection', socket);
44 gun.server.handleUpgrade(request, socket, head, function (ws) { 49 gun.server.handleUpgrade(request, socket, head, function (ws) {
45 console.log('connecting to gun.. ', gun.gun.opt()._.opt.ws.path ) 50 console.log('connecting to gun instance', gun.gun.opt()._.opt.ws.path )
46 gun.server.emit('connection', ws, request); 51 gun.server.emit('connection', ws, request);
47 }); 52 });
48 53
...@@ -51,5 +56,5 @@ server.on('upgrade', async function (request, socket, head) { ...@@ -51,5 +56,5 @@ server.on('upgrade', async function (request, socket, head) {
51 } 56 }
52 }); 57 });
53 58
54 59 //
55 server.listen(3000); 60 server.listen(3000);
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!