PageRenderTime 40ms CodeModel.GetById 23ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/static/iframe.html

http://github.com/biilmann/eventsource-broker
HTML | 54 lines | 47 code | 7 blank | 0 comment | 0 complexity | cc7deb361cadddb32223a0ef3ae8c285 MD5 | raw file
 1<!doctype html>
 2<head><title>EventSource IFrame</title></head>
 3<script>
 4	if (typeof(window.addEventListener) == "undefined") {
 5		window.addEventListener = function(name, fn) {
 6			window.attachEvent("on" + name, fn);
 7		};
 8	}
 9</script>
10<script src="/static/eventsource.polyfill.js"></script>
11<script>
12var channel = document.location.search.match(/channel=([^&]+)/)[1];
13var socket  = document.location.search.match(/socket=([^&]+)/)[1];
14
15
16var post = function(path, data, callback) {
17	var xhr = new XMLHttpRequest();
18	xhr.open('POST', path, true);
19	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
20  if (callback) {
21  	xhr.onreadystatechange = callback;
22  }
23	xhr.send(data);
24};
25
26setTimeout(function() {
27  // on success
28  var evtSrc  = new EventSource( "/eventsource?socket=" + socket );
29
30  if (evtSrc.readyState > 0) {
31    parent.postMessage('{"eshqEvent":"open", "channel": "' + channel +'"}', "*");
32  } else {
33    evtSrc.onopen = function(e) {
34      parent.postMessage('{"eshqEvent":"open", "channel": "' + channel +'"}', "*");
35    };
36  }
37
38  evtSrc.onerror = function(e) {
39    parent.postMessage('{"eshqEvent":"error", "channel": "' + channel +'"}', "*");
40  };
41
42  evtSrc.onmessage = function(e) {
43    parent.postMessage(JSON.stringify({
44      eshqEvent: "message",
45      originalEvent: e,
46      channel: channel
47    }), "*");
48  };
49}, 0);
50
51window.addEventListener("message", function(e) {
52  post("/socket/" + socket, "data=" + encodeURIComponent(e.data));
53}, false);
54</script>