a5927ea8 by Jabis Sevón

merge marks multigun patch

1 parent b2c715bd
1 ![image](https://user-images.githubusercontent.com/1423657/79577881-b6ae2480-80c5-11ea-925c-8d69c2168613.png) 1 ![image](https://user-images.githubusercontent.com/1423657/80729669-deab7800-8b08-11ea-88a5-13cdbcaee38c.png)
2 2
3 # GunDB MultiSocket 3 #### Gun MultiSocket
4 Single `HTTP/S` server providing `WebSocket` Path based routing to ephemeral [GunDB](https://gun.eco) instances in mesh isolation. 4 Single `HTTP/S` server providing multiple ephemeral [GunDB](https://gun.eco) `WebSocket` instances with path based routing and mesh isolation.
5 5
6 ### Notes 6 ### Notes
7 * uses its own [mem](https://github.com/meething/gundb-multisocket/blob/master/mem.js) storage adaptor to avoid any disk writes 7 * uses its own [mem](https://github.com/meething/gundb-multisocket/blob/master/mem.js) storage adaptor to avoid any disk writes
......
...@@ -22,8 +22,8 @@ ...@@ -22,8 +22,8 @@
22 return; 22 return;
23 } 23 }
24 to = setTimeout(flush, opt.wait || 1); 24 to = setTimeout(flush, opt.wait || 1);
25 //var id = at['#'] 25 // var id = at['#']
26 //ctx.on('in', {"@": id, ok:1}) 26 // ctx.on('in', {"@": id, ok:1})
27 }); 27 });
28 ctx.on('get', function(at){ 28 ctx.on('get', function(at){
29 //console.log("get at",at,disk); 29 //console.log("get at",at,disk);
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
9 }, 9 },
10 "dependencies": { 10 "dependencies": {
11 "express": "^4.17.1", 11 "express": "^4.17.1",
12 "gun": "^0.2020.401", 12 "gun": "^0.2020.430",
13 "quick-lru": "^5.1.0" 13 "quick-lru": "^5.1.0"
14 }, 14 },
15 "engines": { 15 "engines": {
......
...@@ -4,11 +4,12 @@ ...@@ -4,11 +4,12 @@
4 * MIT Licensed (C) QXIP 2020 4 * MIT Licensed (C) QXIP 2020
5 */ 5 */
6 6
7 var no = require('gun/lib/nomem')(); // no-memory storage adapter for RAD
7 const fs = require("fs"); 8 const fs = require("fs");
8 const url = require("url"); 9 const url = require("url");
9 const Gun = require("gun/gun"); // do not load storage adaptors by default 10 const Gun = require("gun"); // load defaults
10 require("./gun-ws.js"); // required to allow external websockets into gun constructor 11 //require("./gun-ws.js"); // required to allow external websockets into gun constructor
11 require("./mem.js"); // disable to allow file writing for debug 12 //require("./mem.js"); // disable to allow file writing for debug
12 require("gun/sea"); 13 require("gun/sea");
13 require("gun/lib/then"); 14 require("gun/lib/then");
14 const SEA = Gun.SEA; 15 const SEA = Gun.SEA;
...@@ -17,10 +18,9 @@ const https = require("https"); ...@@ -17,10 +18,9 @@ const https = require("https");
17 const WebSocket = require("ws"); 18 const WebSocket = require("ws");
18 var debug = process.env.DEBUG || false; 19 var debug = process.env.DEBUG || false;
19 var config = {}; 20 var config = {};
21
20 config.options = { 22 config.options = {
21 } 23 }
22
23
24 if (!process.env.hasOwnProperty('SSL')||process.env.SSL == false) { 24 if (!process.env.hasOwnProperty('SSL')||process.env.SSL == false) {
25 var server = http.createServer(); 25 var server = http.createServer();
26 server.listen(process.env.PORT || 8767); 26 server.listen(process.env.PORT || 8767);
...@@ -50,6 +50,7 @@ server.on("upgrade", async function(request, socket, head) { ...@@ -50,6 +50,7 @@ server.on("upgrade", async function(request, socket, head) {
50 var gun = { gun: false, server: false }; 50 var gun = { gun: false, server: false };
51 if (pathname) { 51 if (pathname) {
52 let roomname = pathname.split("").slice(1).join(""); 52 let roomname = pathname.split("").slice(1).join("");
53 console.log("roomname",roomname);
53 if (lru.has(pathname)) { 54 if (lru.has(pathname)) {
54 // Existing Node 55 // Existing Node
55 if (debug) console.log("Recycle id", pathname); 56 if (debug) console.log("Recycle id", pathname);
...@@ -65,7 +66,7 @@ server.on("upgrade", async function(request, socket, head) { ...@@ -65,7 +66,7 @@ server.on("upgrade", async function(request, socket, head) {
65 if(debug) console.log("stored sig ",sig,"to pathname",pathname); 66 if(debug) console.log("stored sig ",sig,"to pathname",pathname);
66 } 67 }
67 //console.log("gunsea",Gun.SEA); 68 //console.log("gunsea",Gun.SEA);
68 SEA.throw = 1; 69 //SEA.throw = 1;
69 /*Gun.on('opt',function(ctx){ 70 /*Gun.on('opt',function(ctx){
70 if(ctx.once) return; 71 if(ctx.once) return;
71 ctx.on('in',function(msg){ 72 ctx.on('in',function(msg){
...@@ -82,34 +83,34 @@ server.on("upgrade", async function(request, socket, head) { ...@@ -82,34 +83,34 @@ server.on("upgrade", async function(request, socket, head) {
82 ws: { noServer: true, path: pathname, web: gun.server }, 83 ws: { noServer: true, path: pathname, web: gun.server },
83 web: gun.server 84 web: gun.server
84 }); 85 });
86 gun.server = gun.gun.back('opt.ws.web'); // this is the websocket server
85 lru.set(pathname, gun); 87 lru.set(pathname, gun);
86 let obj = {roomname:roomname,creator:creator,socket:{}}; 88 let obj = {roomname:roomname,creator:creator,socket:{}};
87 if(sig) { 89 if(sig) {
88 let user = g.user(); 90 let user = g.user();
89 user.create(roomname,sig,async function(ack){ 91 user.create(roomname,sig,async function(dack){
90 console.log("We've got create ack",ack); 92 console.log("We've got create ack",dack,roomname,sig);
91 if(ack.err){ console.log("error in user.create",ack.err); } 93 if(dack.err){ console.log("error in user.create",dack.err); }
92 let auth = await new Promise ((res,rej)=>{ 94 user.auth(roomname,sig,function(auth){
93 return user.auth(roomname,sig,res); 95
94 });
95 if(auth.err){ console.log('error in auth',auth.err); } 96 if(auth.err){ console.log('error in auth',auth.err); }
96 console.log("auth",auth); 97 console.log("auth",auth,roomname,sig);
97 Object.assign(obj,{ 98 Object.assign(obj,{
98 pub:ack.pub, 99 pub:dack.pub,
99 passwordProtected:true 100 passwordProtected:true
100 }) 101 })
101 let roomnode = user.get(roomname).put(obj); 102 let roomnode = user.get(roomname).put(obj);
102 let putnode = g.get('rtcmeeting').get(roomname).put(roomnode); 103 let putnode = g.get('rtcmeeting').get(roomname);
103 let rack= await putnode.then(); 104 let rack= putnode.put(roomnode);
104 console.log("room created",rack); 105 console.log("room created");
106 rack.once(Gun.log);
107 });
105 }); 108 });
106 } else { 109 } else {
107 ;(async ()=>{
108 Object.assign(obj,{passwordProtected:false}); 110 Object.assign(obj,{passwordProtected:false});
109 let roomnode = g.get("rtcmeeting").get(roomname).put(obj); 111 let roomnode = g.get("rtcmeeting").get(roomname).put(obj,function(rack){
110 let rack = await roomnode.then();
111 console.log("room created",rack); 112 console.log("room created",rack);
112 })() 113 });
113 } 114 }
114 } 115 }
115 } 116 }
...@@ -120,6 +121,7 @@ server.on("upgrade", async function(request, socket, head) { ...@@ -120,6 +121,7 @@ server.on("upgrade", async function(request, socket, head) {
120 gun.server.emit("connection", ws, request); 121 gun.server.emit("connection", ws, request);
121 }); 122 });
122 } else { 123 } else {
124 if (debug) console.log("destroying socket", pathname);
123 socket.destroy(); 125 socket.destroy();
124 } 126 }
125 }); 127 });
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!