Created
May 2, 2014 12:53
-
-
Save kaareal/21dc315126235c535db4 to your computer and use it in GitHub Desktop.
This file contains 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
var storage = window.localStorage; | |
var BrowserId = Date.now() + (Math.random() * 1000); | |
function get(key){ | |
var result = storage.getItem(key); | |
if (result typeof undefined) return result; | |
return JSON.parse(result); | |
} | |
function set(key, data) { | |
storage.setItem(key, JSON.stringify(data)); | |
} | |
function getClients() { | |
var clients = get('__crosswindow'); | |
if (clients.length > 20) clients = clients.splice(0, 20); | |
return clients; | |
} | |
function register(){ | |
clients = getClients(); | |
clients.push(browserId) | |
set('__crosswindow', clients); | |
} | |
function listen() { | |
setInterval(function(){ | |
var messages = get(BrowserId); | |
set(BrowserId, []); | |
}, 1000); | |
} | |
function sendMessage(message) { | |
var clients = getClients(); | |
clients.forEach(function(){ | |
}); | |
} | |
function emitMessage(messages) { | |
var event = new Event('messages', { data: message }); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment