mirror of
https://github.com/amark/gun.git
synced 2025-11-24 14:35:55 +00:00
revert lib/multicast.js - chrome extensions can't use it after all
This commit is contained in:
parent
5b50931b87
commit
cb3af7e24d
@ -1,33 +1,29 @@
|
|||||||
var Gun = (typeof window !== 'undefined')? window.Gun : require('../gun');
|
var Gun = (typeof window !== "undefined")? window.Gun : require('../gun');
|
||||||
|
|
||||||
var MULTICAST_ADDR = '233.255.255.255';
|
Gun.on('create', function(root){
|
||||||
|
this.to.next(root);
|
||||||
|
var opt = root.opt;
|
||||||
|
if(typeof window !== "undefined"){
|
||||||
|
return; // do nothing for now - Chrome extensions could use multicast though
|
||||||
|
}
|
||||||
|
if(false === opt.multicast){ return }
|
||||||
|
opt.multicast = opt.multicast || {};
|
||||||
|
|
||||||
|
var MULTICAST_ADDR = "233.255.255.255";
|
||||||
var MULTICAST_INTERVAL = 1000;
|
var MULTICAST_INTERVAL = 1000;
|
||||||
var PORT = 20000;
|
var PORT = 20000;
|
||||||
var DEFAULT_GUN_PORT = 8765;
|
var DEFAULT_GUN_PORT = 8765;
|
||||||
var ENC = 'utf8';
|
var ENC = 'utf8';
|
||||||
|
|
||||||
Gun.on('create', function(root){
|
var dgram = require("dgram");
|
||||||
this.to.next(root);
|
var process = require("process");
|
||||||
var opt = root.opt, u;
|
|
||||||
if(false === opt.multicast){ return }
|
|
||||||
opt.multicast = opt.multicast || {};
|
|
||||||
if(typeof window === 'undefined'){
|
|
||||||
nodeMulticast(root, opt);
|
|
||||||
} else if (chrome && chrome.sockets && chrome.sockets.udp) {
|
|
||||||
chromeMulticast(root, opt);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
function nodeMulticast(root, opt) {
|
socket = dgram.createSocket({ type: "udp4", reuseAddr: true });
|
||||||
var dgram = require('dgram');
|
|
||||||
var process = require('process');
|
|
||||||
|
|
||||||
socket = dgram.createSocket({ type: 'udp4', reuseAddr: true });
|
|
||||||
|
|
||||||
socket.bind(PORT);
|
socket.bind(PORT);
|
||||||
|
|
||||||
var address;
|
var address;
|
||||||
socket.on('listening', function() {
|
socket.on("listening", function() {
|
||||||
socket.addMembership(MULTICAST_ADDR);
|
socket.addMembership(MULTICAST_ADDR);
|
||||||
if (opt.multicast && opt.multicast.port) { // if port is specified, advertise our node
|
if (opt.multicast && opt.multicast.port) { // if port is specified, advertise our node
|
||||||
console.log(`Advertising this node (port ${opt.multicast.port}) on multicast (${MULTICAST_ADDR})`);
|
console.log(`Advertising this node (port ${opt.multicast.port}) on multicast (${MULTICAST_ADDR})`);
|
||||||
@ -45,11 +41,11 @@ function nodeMulticast(root, opt) {
|
|||||||
};
|
};
|
||||||
var message = Buffer.from(JSON.stringify(msgObj), ENC);
|
var message = Buffer.from(JSON.stringify(msgObj), ENC);
|
||||||
socket.send(message, 0, message.length, PORT, MULTICAST_ADDR, function() {
|
socket.send(message, 0, message.length, PORT, MULTICAST_ADDR, function() {
|
||||||
// console.info(`Sending message '${message}'`);
|
// console.info(`Sending message "${message}"`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.on('message', function(message, rinfo) {
|
socket.on("message", function(message, rinfo) {
|
||||||
try {
|
try {
|
||||||
var msgObj = JSON.parse(message.toString(ENC));
|
var msgObj = JSON.parse(message.toString(ENC));
|
||||||
if (!(msgObj.gun && msgObj.gun.port)) { return }
|
if (!(msgObj.gun && msgObj.gun.port)) { return }
|
||||||
@ -62,40 +58,4 @@ function nodeMulticast(root, opt) {
|
|||||||
// console.error(`Received multicast from ${rinfo.address}:${rinfo.port} but failed to connect:`, e);
|
// console.error(`Received multicast from ${rinfo.address}:${rinfo.port} but failed to connect:`, e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
function chromeMulticast(root, opt) {
|
|
||||||
chrome.sockets.udp.create({bufferSize: 1024 * 1024}, function (createInfo) {
|
|
||||||
var socketId = createInfo.socketId;
|
|
||||||
var ttl = 12;
|
|
||||||
chrome.sockets.udp.setMulticastTimeToLive(socketId, ttl, function (result) {
|
|
||||||
if (result != 0) {
|
|
||||||
console.error('Set TTL Error: ', 'Unknown error');
|
|
||||||
}
|
|
||||||
chrome.sockets.udp.bind(socketId, '0.0.0.0', PORT, function (result) {
|
|
||||||
if (result != 0) {
|
|
||||||
chrome.sockets.udp.close(socketId, function () {
|
|
||||||
console.error('Error on bind(): ', result);
|
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
chrome.sockets.udp.joinGroup(socketId, MULTICAST_ADDR, function (result) {
|
|
||||||
if (result != 0) {
|
|
||||||
chrome.sockets.udp.close(socketId, function () {
|
|
||||||
console.error('Error on joinGroup(): ', result);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
me.socketId = socketId;
|
|
||||||
chrome.sockets.udp.onReceive.addListener(info => {
|
|
||||||
console.log('msg received', info);
|
|
||||||
});
|
|
||||||
chrome.sockets.udp.onReceiveError.addListener(info => {
|
|
||||||
console.error('receive error');
|
|
||||||
});
|
|
||||||
console.log('Chrome multicast socket listening');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "gun",
|
"name": "gun",
|
||||||
"version": "0.9.101",
|
"version": "0.9.102",
|
||||||
"description": "A realtime, decentralized, offline-first, graph data synchronization engine.",
|
"description": "A realtime, decentralized, offline-first, graph data synchronization engine.",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"browser": "gun.min.js",
|
"browser": "gun.min.js",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user