Last active
August 29, 2015 14:16
-
-
Save rektide/36c5ec5301fb17f37ea6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a broadcast-channel-ification of https://github.com/mkruisselbrink/navigator-connect/issues/1#issuecomment-62989902 | |
var discoveryService = new BroadcastChannel("discoveryservice"); | |
// service discovery | |
var serviceChannelName = "appservice" | |
var notifyService = new BroadcastChannel(serviceChannelName); | |
setTimeout(function(){ | |
discoveryService.postMessage({ service: "net.yoyodyne.Notify", channel: serviceChannelName }); | |
},100) | |
// notify service | |
setTimeout(function(){ | |
if (Math.random() < 0.5) notifyService.postMessage({notice:"notice"}); | |
},1000); | |
// client | |
var discoveryConsumer = new BroadcastChannel("discoveryservice") | |
var discoverNotify = function(e) { | |
if(e.data && e.data.service === "net.yoyodyne.Notify" && e.data.channel) { | |
discoveryConsumer.removeListener(discoverNotify) | |
var notifyChannel = new BroadcastChannel(e.data.channel) | |
notifyChannel.addEventListener("message", function(f) { | |
console.log("Important message from " + f.origin + ": " + JSON.stringify(f.data)) | |
}) | |
} | |
}) | |
discoveryConsumer.addEventListener("discoveryservice", discoverNotify) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment