fix ws noServer

This commit is contained in:
Mark Nadal 2020-05-01 18:54:16 -07:00
parent ff99a4639d
commit c93305ae11
2 changed files with 19 additions and 23 deletions

View File

@ -51,22 +51,19 @@ var Gun = require('../gun');
Gun.on('opt', function(root){ Gun.on('opt', function(root){
var opt = root.opt; var opt = root.opt;
if(false === opt.ws){ if(false === opt.ws || opt.once){
this.to.next(root); this.to.next(root);
return; return;
} }
var url = require('url'); var url = require('url');
opt.mesh = opt.mesh || Gun.Mesh(root);
opt.WebSocket = opt.WebSocket || require('ws'); opt.WebSocket = opt.WebSocket || require('ws');
var ws = opt.ws = opt.ws || {}; var ws = opt.ws = opt.ws || {};
ws.server = ws.server || opt.web;
if(ws.server && !ws.web){
opt.mesh = opt.mesh || Gun.Mesh(root);
ws.path = ws.path || '/gun'; ws.path = ws.path || '/gun';
ws.maxPayload = ws.maxPayload; // || opt.pack || (opt.memory? (opt.memory * 1000 * 1000) : 1399000000) * 0.3; // if we DO need an HTTP server, then choose ws specific one or GUN default one.
ws.web = new opt.WebSocket.Server(ws); if(!ws.noServer){ ws.server = ws.server || opt.web }
ws.web = ws.web || new opt.WebSocket.Server(ws); // we still need a WS server.
ws.web.on('connection', function(wire){ var peer; ws.web.on('connection', function(wire){ var peer;
wire.upgradeReq = wire.upgradeReq || {}; wire.upgradeReq = wire.upgradeReq || {};
wire.url = url.parse(wire.upgradeReq.url||'', true); wire.url = url.parse(wire.upgradeReq.url||'', true);
@ -80,7 +77,6 @@ Gun.on('opt', function(root){
wire.on('error', function(e){}); wire.on('error', function(e){});
setTimeout(function heart(){ if(!opt.peers[peer.id]){ return } try{ wire.send("[]") }catch(e){} ;setTimeout(heart, 1000 * 20) }, 1000 * 20); // Some systems, like Heroku, require heartbeats to not time out. // TODO: Make this configurable? // TODO: PERF: Find better approach than try/timeouts? setTimeout(function heart(){ if(!opt.peers[peer.id]){ return } try{ wire.send("[]") }catch(e){} ;setTimeout(heart, 1000 * 20) }, 1000 * 20); // Some systems, like Heroku, require heartbeats to not time out. // TODO: Make this configurable? // TODO: PERF: Find better approach than try/timeouts?
}); });
}
this.to.next(root); this.to.next(root);
}); });

View File

@ -1,6 +1,6 @@
{ {
"name": "gun", "name": "gun",
"version": "0.2020.421", "version": "0.2020.430",
"description": "A realtime, decentralized, offline-first, graph data synchronization engine.", "description": "A realtime, decentralized, offline-first, graph data synchronization engine.",
"types": "index.d.ts", "types": "index.d.ts",
"main": "index.js", "main": "index.js",