/static/iframe.html
http://github.com/biilmann/eventsource-broker · HTML · 54 lines · 47 code · 7 blank · 0 comment · 0 complexity · cc7deb361cadddb32223a0ef3ae8c285 MD5 · raw file
- <!doctype html>
- <head><title>EventSource IFrame</title></head>
- <script>
- if (typeof(window.addEventListener) == "undefined") {
- window.addEventListener = function(name, fn) {
- window.attachEvent("on" + name, fn);
- };
- }
- </script>
- <script src="/static/eventsource.polyfill.js"></script>
- <script>
- var channel = document.location.search.match(/channel=([^&]+)/)[1];
- var socket = document.location.search.match(/socket=([^&]+)/)[1];
- var post = function(path, data, callback) {
- var xhr = new XMLHttpRequest();
- xhr.open('POST', path, true);
- xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- if (callback) {
- xhr.onreadystatechange = callback;
- }
- xhr.send(data);
- };
- setTimeout(function() {
- // on success
- var evtSrc = new EventSource( "/eventsource?socket=" + socket );
- if (evtSrc.readyState > 0) {
- parent.postMessage('{"eshqEvent":"open", "channel": "' + channel +'"}', "*");
- } else {
- evtSrc.onopen = function(e) {
- parent.postMessage('{"eshqEvent":"open", "channel": "' + channel +'"}', "*");
- };
- }
- evtSrc.onerror = function(e) {
- parent.postMessage('{"eshqEvent":"error", "channel": "' + channel +'"}', "*");
- };
- evtSrc.onmessage = function(e) {
- parent.postMessage(JSON.stringify({
- eshqEvent: "message",
- originalEvent: e,
- channel: channel
- }), "*");
- };
- }, 0);
- window.addEventListener("message", function(e) {
- post("/socket/" + socket, "data=" + encodeURIComponent(e.data));
- }, false);
- </script>