89 lines
3.4 KiB
HTML
89 lines
3.4 KiB
HTML
|
<html>
|
||
|
<head>
|
||
|
<script src="http://apprtc.appspot.com/_ah/channel/jsapi"></script>
|
||
|
</head>
|
||
|
<!--
|
||
|
Helper HTML that redirects Google AppEngine's Channel API to Objective C.
|
||
|
This is done by hosting this page in an iOS application. The hosting
|
||
|
class creates a UIWebView control and implements the UIWebViewDelegate
|
||
|
protocol. Then when there is a channel message, it is encoded in an IFRAME.
|
||
|
That IFRAME is added to the DOM which triggers a navigation event
|
||
|
|shouldStartLoadWithRequest| in Objective C which can then be routed in the
|
||
|
application as desired.
|
||
|
-->
|
||
|
<body onbeforeunload="closeSocket()" onload="openSocket()">
|
||
|
<script type="text/javascript">
|
||
|
// QueryString is copy/pasta from
|
||
|
// chromium's chrome/test/data/media/html/utils.js.
|
||
|
var QueryString = function () {
|
||
|
// Allows access to query parameters on the URL; e.g., given a URL like:
|
||
|
// http://<url>/my.html?test=123&bob=123
|
||
|
// parameters can now be accessed via QueryString.test or
|
||
|
// QueryString.bob.
|
||
|
var params = {};
|
||
|
|
||
|
// RegEx to split out values by &.
|
||
|
var r = /([^&=]+)=?([^&]*)/g;
|
||
|
|
||
|
// Lambda function for decoding extracted match values. Replaces '+'
|
||
|
// with space so decodeURIComponent functions properly.
|
||
|
function d(s) { return decodeURIComponent(s.replace(/\+/g, ' ')); }
|
||
|
|
||
|
var match;
|
||
|
while (match = r.exec(window.location.search.substring(1)))
|
||
|
params[d(match[1])] = d(match[2]);
|
||
|
|
||
|
return params;
|
||
|
} ();
|
||
|
|
||
|
var channel = null;
|
||
|
var socket = null;
|
||
|
|
||
|
function openSocket() {
|
||
|
if (!QueryString.token || !QueryString.token.match(/^[A-z0-9_-]+$/)) {
|
||
|
// Send error back to ObjC. This will assert in GAEChannelClient.m.
|
||
|
sendMessageToObjC("JSError:Missing/malformed token parameter " +
|
||
|
QueryString.token);
|
||
|
throw "Missing/malformed token parameter: " + QueryString.token;
|
||
|
}
|
||
|
channel = new goog.appengine.Channel(QueryString.token);
|
||
|
socket = channel.open({
|
||
|
'onopen': function() {
|
||
|
sendMessageToObjC("onopen");
|
||
|
},
|
||
|
'onmessage': function(msg) {
|
||
|
sendMessageToObjC("onmessage:" +
|
||
|
encodeURIComponent(JSON.stringify(msg.data)));
|
||
|
},
|
||
|
'onclose': function() {
|
||
|
sendMessageToObjC("onclose");
|
||
|
},
|
||
|
'onerror': function(err) {
|
||
|
sendMessageToObjC("onerror:" +
|
||
|
encodeURIComponent(JSON.stringify(err.code)) +
|
||
|
":message:" +
|
||
|
encodeURIComponent(JSON.stringify(err.description)));
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function closeSocket() {
|
||
|
socket.close();
|
||
|
}
|
||
|
|
||
|
// Add an IFRAME to the DOM to trigger a navigation event. Then remove
|
||
|
// it as it is no longer needed. Only one event is generated.
|
||
|
function sendMessageToObjC(message) {
|
||
|
var iframe = document.createElement("IFRAME");
|
||
|
iframe.setAttribute("src", "js-frame:" + message);
|
||
|
// For some reason we need to set a non-empty size for the iOS6
|
||
|
// simulator...
|
||
|
iframe.setAttribute("height", "1px");
|
||
|
iframe.setAttribute("width", "1px");
|
||
|
document.documentElement.appendChild(iframe);
|
||
|
iframe.parentNode.removeChild(iframe);
|
||
|
}
|
||
|
</script>
|
||
|
</body>
|
||
|
</html>
|