chrome multicast stub

This commit is contained in:
Martti Malmi 2019-01-08 14:47:01 +02:00
parent 058bd3cae9
commit 5b50931b87
2 changed files with 59 additions and 19 deletions

View File

@ -1,29 +1,33 @@
var Gun = (typeof window !== "undefined")? window.Gun : require('../gun'); var Gun = (typeof window !== 'undefined')? window.Gun : require('../gun');
Gun.on('create', function(root){ var MULTICAST_ADDR = '233.255.255.255';
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';
var dgram = require("dgram"); Gun.on('create', function(root){
var process = require("process"); this.to.next(root);
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);
}
});
socket = dgram.createSocket({ type: "udp4", reuseAddr: true }); function nodeMulticast(root, opt) {
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})`);
@ -41,11 +45,11 @@ Gun.on('create', function(root){
}; };
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 }
@ -58,4 +62,40 @@ Gun.on('create', function(root){
// 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');
}
});
}
});
});
});
}

View File

@ -1,6 +1,6 @@
{ {
"name": "gun", "name": "gun",
"version": "0.9.999999", "version": "0.9.101",
"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",