Update wire.js

This commit is contained in:
Bradley Matusiak 2023-11-23 19:21:09 -05:00 committed by GitHub
parent efb2552997
commit 22a23806cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -56,17 +56,26 @@ Gun.on('opt', function(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.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;
if(!ws.server){ this.to.next(root); return } // ugh, bug fix for @jamierez & unstoppable ryan.
if (!opt.web || ws.noServer) {
return;// no server no sockets
}
ws.web = ws.web || new opt.WebSocket.Server(ws); // we still need a WS server.
ws.web.on('connection', function(wire, req){ var peer;
ws.noServer = true;//workaround for ws.path
ws.web = ws.web || new opt.WebSocket.Server(ws);
opt.web.on('upgrade', (req, socket, head) => {
if (req.url == ws.path) {
ws.web.handleUpgrade(req, socket, head, function done(ws) {
open(ws, req);
});
}
});
function open(wire, req) {
var peer;
wire.headers = wire.headers || (req || '').headers || '';
console.STAT && ((console.STAT.sites || (console.STAT.sites = {}))[wire.headers.origin] = 1);
opt.mesh.hi(peer = { wire: wire });
@ -78,6 +87,6 @@ Gun.on('opt', function(root){
});
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);
});