Thread: [Phpfreechat-svn] SF.net SVN: phpfreechat: [1069] branches/pfc-comet/misc/comet-tests
Status: Beta
Brought to you by:
kerphi
From: <ke...@us...> - 2007-08-01 14:37:04
|
Revision: 1069 http://phpfreechat.svn.sourceforge.net/phpfreechat/?rev=1069&view=rev Author: kerphi Date: 2007-08-01 07:37:04 -0700 (Wed, 01 Aug 2007) Log Message: ----------- basic comet implement using iframe (from www.zeitoun.net) Added Paths: ----------- branches/pfc-comet/misc/comet-tests/iframe/ branches/pfc-comet/misc/comet-tests/iframe/backend.php branches/pfc-comet/misc/comet-tests/iframe/client.html Added: branches/pfc-comet/misc/comet-tests/iframe/backend.php =================================================================== --- branches/pfc-comet/misc/comet-tests/iframe/backend.php (rev 0) +++ branches/pfc-comet/misc/comet-tests/iframe/backend.php 2007-08-01 14:37:04 UTC (rev 1069) @@ -0,0 +1,44 @@ +<?php + +header("Cache-Control: no-cache, must-revalidate"); +header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); +flush(); + +?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml"> +<head> + <title>Comet php backend</title> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> +</head> +<body> + +<script type="text/javascript"> + // KHTML browser don't share javascripts between iframes + var is_khtml = navigator.appName.match("Konqueror") || navigator.appVersion.match("KHTML"); + if (is_khtml) + { + var prototypejs = document.createElement('script'); + prototypejs.setAttribute('type','text/javascript'); + prototypejs.setAttribute('src','../../../data/public/js/prototype.js'); + var head = document.getElementsByTagName('head'); + head[0].appendChild(prototypejs); + } + // load the comet object + var comet = window.parent.comet; +</script> + +<?php + +while(1) { + echo '<script type="text/javascript">'; + echo 'comet.printServerTime('.time().');'; + echo '</script>'; + flush(); // used to send the echoed data to the client + sleep(1); // a little break to unload the server CPU +} + +?> + + +</body> +</html> \ No newline at end of file Added: branches/pfc-comet/misc/comet-tests/iframe/client.html =================================================================== --- branches/pfc-comet/misc/comet-tests/iframe/client.html (rev 0) +++ branches/pfc-comet/misc/comet-tests/iframe/client.html 2007-08-01 14:37:04 UTC (rev 1069) @@ -0,0 +1,80 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <title>Comet demo</title> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <script type="text/javascript" src="../../../data/public/js/prototype.js"></script> + </head> + <body> + <div id="content">The server time will be shown here</div> + +<script type="text/javascript"> +var comet = { + connection : false, + iframediv : false, + + initialize: function() { + if (navigator.appVersion.indexOf("MSIE") != -1) { + + // For IE browsers + comet.connection = new ActiveXObject("htmlfile"); + comet.connection.open(); + comet.connection.write("<html>"); + comet.connection.write("<script>document.domain = '"+document.domain+"'"); + comet.connection.write("</html>"); + comet.connection.close(); + comet.iframediv = comet.connection.createElement("div"); + comet.connection.appendChild(comet.iframediv); + comet.connection.parentWindow.comet = comet; + comet.iframediv.innerHTML = "<iframe id='comet_iframe' src='./backend.php'></iframe>"; + + } else if (navigator.appVersion.indexOf("KHTML") != -1) { + + // for KHTML browsers + comet.connection = document.createElement('iframe'); + comet.connection.setAttribute('id', 'comet_iframe'); + comet.connection.setAttribute('src', './backend.php'); + with (comet.connection.style) { + position = "absolute"; + left = top = "-100px"; + height = width = "1px"; + visibility = "hidden"; + } + document.body.appendChild(comet.connection); + + } else { + + // For other browser (Firefox...) + comet.connection = document.createElement('iframe'); + comet.connection.setAttribute('id', 'comet_iframe'); + with (comet.connection.style) { + left = top = "-100px"; + height = width = "1px"; + visibility = "hidden"; + display = 'none'; + } + comet.iframediv = document.createElement('iframe'); + comet.iframediv.setAttribute('src', './backend.php'); + comet.connection.appendChild(comet.iframediv); + document.body.appendChild(comet.connection); + + } + }, + + // this function will be called from backend.php + printServerTime: function (time) { + $('content').innerHTML = time; + }, + + onUnload: function() { + if (comet.connection) { + comet.connection = false; // release the iframe to prevent problems with IE when reloading the page + } + } +} +Event.observe(window, "load", comet.initialize); +Event.observe(window, "unload", comet.onUnload); +</script> + +</body> +</html> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ke...@us...> - 2007-08-01 20:06:52
|
Revision: 1073 http://phpfreechat.svn.sourceforge.net/phpfreechat/?rev=1073&view=rev Author: kerphi Date: 2007-08-01 13:06:54 -0700 (Wed, 01 Aug 2007) Log Message: ----------- develope a more generic way to communicate with iframe/comet system Added Paths: ----------- branches/pfc-comet/misc/comet-tests/iframe2/ branches/pfc-comet/misc/comet-tests/iframe2/pfccomet.class.php branches/pfc-comet/misc/comet-tests/iframe2/pfccomet.js branches/pfc-comet/misc/comet-tests/iframe2/tester.php Added: branches/pfc-comet/misc/comet-tests/iframe2/pfccomet.class.php =================================================================== --- branches/pfc-comet/misc/comet-tests/iframe2/pfccomet.class.php (rev 0) +++ branches/pfc-comet/misc/comet-tests/iframe2/pfccomet.class.php 2007-08-01 20:06:54 UTC (rev 1073) @@ -0,0 +1,100 @@ +<?php + +class pfcComet { + var $pfccometjs_url = './pfccomet.js'; + var $prototypejs_url = './prototype.js'; + var $backend_url = ''; + var $backend_param = 'backend'; + var $backend_callback = null; + var $backend_loop = false; + var $backend_loop_sleep = 1; + + function pfcComet() + { + if ($this->backend_url == '') + $this->backend_url = $_SERVER['PHP_SELF']; + } + + function run() + { + if (isset($_REQUEST[$this->backend_param])) + { + header("Cache-Control: no-cache, must-revalidate"); + header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); + flush(); + echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml"> +<head> + <title>pfcComet backend iframe</title> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> +</head> +<script type="text/javascript"> + // KHTML browser don\'t share javascripts between iframes + var is_khtml = navigator.appName.match("Konqueror") || navigator.appVersion.match("KHTML"); + if (is_khtml) + { + var prototypejs = document.createElement("script"); + prototypejs.setAttribute("type","text/javascript"); + prototypejs.setAttribute("src","'.$this->prototypejs_url.'"); + var head = document.getElementsByTagName("head"); + head[0].appendChild(prototypejs); + } + // load the comet object + var pfccomet = window.parent.pfccomet; +</script>'; + flush(); + + // trigger the onConnect callback + echo '<script type="text/javascript">pfccomet._onConnect();</script>'; + flush(); + + // trigger the backend callback + do { + if (is_callable($this->backend_callback)) + { + $func = $this->backend_callback; + if ( is_array($func) ){ + echo $func[0]->$func[1]($this); + } else { + echo $func($this); + } + } + flush(); + sleep($this->backend_loop_sleep); + } while($this->backend_loop); + + // trigger the onDisconnect callback + echo '<script type="text/javascript">pfccomet._onDisconnect();</script>'; + flush(); + + die(); + } + } + + function formatResponse($data) + { + return '<script type="text/javascript">pfccomet._onResponse(\''.addslashes($data).'\');</script>'; + } + + function printJavascript($return = false) + { + $output = '<script type="text/javascript" src="'.$this->prototypejs_url.'"></script>'."\n"; + $output .= '<script type="text/javascript" src="'.$this->pfccometjs_url.'"></script>'."\n"; + $output .= '<script type="text/javascript"> +Event.observe(window, "load", function() { + pfccomet = new pfcComet({"url":"'.$this->backend_url.'?'.$this->backend_param.'"}); + pfccomet.onConnect = function(comet) { alert("connected"); }; + pfccomet.onDisconnect = function(comet) { alert("disconnected"); }; + pfccomet.onResponse = function(comet,data) { alert("response:"+data); }; + pfccomet.connect(); +}); +</script>'."\n"; + if ($return) + return $output; + else + echo $output; + } + +} + +?> \ No newline at end of file Added: branches/pfc-comet/misc/comet-tests/iframe2/pfccomet.js =================================================================== --- branches/pfc-comet/misc/comet-tests/iframe2/pfccomet.js (rev 0) +++ branches/pfc-comet/misc/comet-tests/iframe2/pfccomet.js 2007-08-01 20:06:54 UTC (rev 1073) @@ -0,0 +1,118 @@ +/** + * This class is used to get data from server using a persistent connexion + * thus the clients are informed in realtime from the server changes (very usefull for a chat application) + * Usage: + * var comet = new pfcComet({'url': './cometbackend.php', 'id': 1}); + * comet.onResponse = function(comet,data) { alert('response:'+data); }; + * comet.onConnect = function(comet) { alert('connected'); }; + * comet.onDisconnect = function(comet) { alert('disconnected'); }; + */ +var pfcComet = Class.create(); +pfcComet.prototype = { + + url: null, + _isconnected: false, + + initialize: function(params) { + if (!params) params = {}; + if (!params['url']) alert('error: url parameter is mandatory'); + this.url = params['url']; + }, + + connect: function() + { + if (this._isconnected) return; + this._openPersistentConnexion(); + }, + + disconnect: function() + { + if (!this._isconnected) return; + this._onDisconnect(); + }, + + _openPersistentConnexion: function() + { + this._iframe = null; + this._iframediv = null; + Event.observe(window, "unload", this._onUnload); + + if (navigator.appVersion.indexOf("MSIE") != -1) { + + // For IE browsers + this._iframe = new ActiveXObject("htmlfile"); + this._iframe.open(); + this._iframe.write("<html>"); + this._iframe.write("<script>document.domain = '"+document.domain+"'"); + this._iframe.write("</html>"); + this._iframe.close(); + this._iframediv = this._iframe.createElement("div"); + this._iframe.appendChild(this._iframediv); + this._iframe.parentWindow.comet = comet; + this._iframediv.innerHTML = '<iframe id="comet_iframe" src="' + this.url + '"></iframe>'; + + } else if (navigator.appVersion.indexOf("KHTML") != -1) { + + // for KHTML browsers + this._iframe = document.createElement('iframe'); + this._iframe.setAttribute('id', 'comet_iframe'); + this._iframe.setAttribute('src', this.url); + with (this._iframe.style) { + position = "absolute"; + left = top = "-100px"; + height = width = "1px"; + visibility = "hidden"; + } + document.body.appendChild(this._iframe); + + } else { + + // For other browser (Firefox...) + this._iframe = document.createElement('iframe'); + this._iframe.setAttribute('id', 'comet_iframe'); + with (this._iframe.style) { + left = top = "-100px"; + height = width = "1px"; + visibility = "hidden"; + display = 'none'; + } + this._iframediv = document.createElement('iframe'); + this._iframediv.setAttribute('src', this.url); + this._iframe.appendChild(this._iframediv); + document.body.appendChild(this._iframe); + + } + }, + + _onConnect: function() + { + this._isconnected = true; + this.onConnect(this); + }, + + _onDisconnect: function() + { + this._iframe.remove(); + this._onUnload(); + this._isconnected = false; + this.onDisconnect(this); + }, + + _onResponse: function(data) + { + this.onResponse(this,data); + }, + + _onUnload: function() { + if (this._iframe) { + this._iframe = false; // release the iframe to prevent problems with IE when reloading the page + } + }, + + /** + * User's callbacks + */ + onResponse: function(pfccomet, data) {}, + onConnect: function(pfccomet) {}, + onDisconnect: function(pfccomet) {} +} Added: branches/pfc-comet/misc/comet-tests/iframe2/tester.php =================================================================== --- branches/pfc-comet/misc/comet-tests/iframe2/tester.php (rev 0) +++ branches/pfc-comet/misc/comet-tests/iframe2/tester.php 2007-08-01 20:06:54 UTC (rev 1073) @@ -0,0 +1,25 @@ +<?php + +function macallback($pfccomet) { + return $pfccomet->formatResponse('test macallback'); +} + +require_once 'pfccomet.class.php'; +$pfccomet = new pfcComet(); +$pfccomet->pfccometjs_url = './pfccomet.js'; +$pfccomet->prototypejs_url = '../../../data/public/js/prototype.js'; +$pfccomet->backend_url = './tester.php'; +$pfccomet->backend_callback = 'macallback'; +$pfccomet->run(); + +?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <title>pfcComet tester</title> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <?php $pfccomet->printJavascript(); ?> + </head> + <body> +tester + </body> +</html> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |