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,36 +51,32 @@ var Gun = require('../gun');
Gun.on('opt', function(root){
var opt = root.opt;
if(false === opt.ws){
if(false === opt.ws || opt.once){
this.to.next(root);
return;
}
var url = require('url');
opt.mesh = opt.mesh || Gun.Mesh(root);
opt.WebSocket = opt.WebSocket || require('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.maxPayload = ws.maxPayload; // || opt.pack || (opt.memory? (opt.memory * 1000 * 1000) : 1399000000) * 0.3;
ws.web = new opt.WebSocket.Server(ws);
ws.web.on('connection', function(wire){ var peer;
wire.upgradeReq = wire.upgradeReq || {};
wire.url = url.parse(wire.upgradeReq.url||'', true);
opt.mesh.hi(peer = {wire: wire});
wire.on('message', function(msg){
opt.mesh.hear(msg.data || msg, peer);
});
wire.on('close', function(){
opt.mesh.bye(peer);
});
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?
ws.path = ws.path || '/gun';
// if we DO need an HTTP server, then choose ws specific one or GUN default one.
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;
wire.upgradeReq = wire.upgradeReq || {};
wire.url = url.parse(wire.upgradeReq.url||'', true);
opt.mesh.hi(peer = {wire: wire});
wire.on('message', function(msg){
opt.mesh.hear(msg.data || msg, peer);
});
}
wire.on('close', function(){
opt.mesh.bye(peer);
});
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?
});
this.to.next(root);
});

View File

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