Thread: [Phpfreechat-svn] SF.net SVN: phpfreechat: [1067] branches/pfc-comet
Status: Beta
Brought to you by:
kerphi
From: <ke...@us...> - 2007-08-01 14:05:21
|
Revision: 1067 http://phpfreechat.svn.sourceforge.net/phpfreechat/?rev=1067&view=rev Author: kerphi Date: 2007-08-01 07:05:22 -0700 (Wed, 01 Aug 2007) Log Message: ----------- add some tests for the future comet implementation Modified Paths: -------------- branches/pfc-comet/themes/default/chat.js.tpl.php Added Paths: ----------- branches/pfc-comet/misc/comet-tests/ branches/pfc-comet/misc/comet-tests/basic/ branches/pfc-comet/misc/comet-tests/basic/comet.php branches/pfc-comet/misc/comet-tests/basic/cometbackend.php branches/pfc-comet/misc/comet-tests/chat/ branches/pfc-comet/misc/comet-tests/chat/comet-chat.php branches/pfc-comet/misc/comet-tests/chat/comet-getdata.php branches/pfc-comet/misc/comet-tests/chat/comet-postdata.php branches/pfc-comet/misc/comet-tests/chat/data.txt branches/pfc-comet/misc/comet-tests/pfccomet.js Removed Paths: ------------- branches/pfc-comet/data/public/js/pfccomet.js Deleted: branches/pfc-comet/data/public/js/pfccomet.js =================================================================== --- branches/pfc-comet/data/public/js/pfccomet.js 2007-08-01 13:38:51 UTC (rev 1066) +++ branches/pfc-comet/data/public/js/pfccomet.js 2007-08-01 14:05:22 UTC (rev 1067) @@ -1,87 +0,0 @@ -/** - * 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(req) { alert('id:'+req['id']+' response:'+req['data']); }; - * comet.onConnect = function(comet) { alert('connected'); }; - * comet.onDisconnect = function(comet) { alert('disconnected'); }; - */ -var pfcComet = Class.create(); -pfcComet.prototype = { - - url: null, - id: 0, - timeout: 5000, - - _noerror: false, - _ajax: null, - _isconnected: false, - - initialize: function(params) { - if (!params) params = {}; - if (!params['url']) alert('error: url parameter is mandatory'); - this.url = params['url']; - if (params['id']) this.id = params['id']; - if (params['timeout']) this.timeout = params['timeout']; - }, - - connect: function() - { - if (this._isconnected) return; - this._isconnected = true; - this.onConnect(this); - this.waitForData(); - }, - - disconnect: function() - { - if (!this._isconnected) return; - this._isconnected = false; - this.onDisconnect(this); - // remove the registred callbacks in order to ignore the next response - this._ajax.options.onSuccess = null; - this._ajax.options.onComplete = null; - }, - - waitForData: function() - { - if (!this._isconnected) return; - - this._ajax = new Ajax.Request(this.url, { - method: 'get', - parameters: { 'id' : this.id }, - onSuccess: function(transport) { - // handle the server response - var response = transport.responseText.evalJSON(); - this.comet.id = response['id']; - this.comet.onResponse(response); - this.comet._noerror = true; - }, - onComplete: function(transport) { - // send a new ajax request when this request is finished - if (!this.comet._noerror) - // if a connection problem occurs, try to reconnect periodicaly - setTimeout(function(){ this.comet.waitForData(); }.bind(this), this.comet.timeout); - else - // of wait for the next data - this.comet.waitForData(); - this.comet._noerror = false; - } - }); - this._ajax.comet = this; - }, - - /** - * User's callbacks - */ - onResponse: function(response) - { - }, - onConnect: function(comet) - { - }, - onDisconnect: function(comet) - { - }, -} Added: branches/pfc-comet/misc/comet-tests/basic/comet.php =================================================================== --- branches/pfc-comet/misc/comet-tests/basic/comet.php (rev 0) +++ branches/pfc-comet/misc/comet-tests/basic/comet.php 2007-08-01 14:05:22 UTC (rev 1067) @@ -0,0 +1,21 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> +<html> + <head> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <title>pfcComet example</title> + <script type="text/javascript" src="../../../data/public/js/prototype.js"></script> + <script type="text/javascript" src="../pfccomet.js"></script> + </head> + <body> + + +<script type="text/javascript"> +var comet = new pfcComet({'url': './cometbackend.php', 'id': 1}); +comet.onResponse = function(req) { /*alert('id:'+req['id']+' response:'+req['data']);*/ }; +</script> + +<p onclick="comet.connect()">login</p> +<p onclick="comet.disconnect()">logout</p> + + </body> +</html> \ No newline at end of file Added: branches/pfc-comet/misc/comet-tests/basic/cometbackend.php =================================================================== --- branches/pfc-comet/misc/comet-tests/basic/cometbackend.php (rev 0) +++ branches/pfc-comet/misc/comet-tests/basic/cometbackend.php 2007-08-01 14:05:22 UTC (rev 1067) @@ -0,0 +1,11 @@ +<?php + +// return a json array +$response = array(); +$response['data'] = 'test'; +$response['id'] = time(); +echo json_encode($response); +flush(); +sleep(5); + +?> \ No newline at end of file Added: branches/pfc-comet/misc/comet-tests/chat/comet-chat.php =================================================================== --- branches/pfc-comet/misc/comet-tests/chat/comet-chat.php (rev 0) +++ branches/pfc-comet/misc/comet-tests/chat/comet-chat.php 2007-08-01 14:05:22 UTC (rev 1067) @@ -0,0 +1,55 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> +<html> + <head> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <title>pfcComet example</title> + <script type="text/javascript" src="../../../data/public/js/prototype.js"></script> + <script type="text/javascript" src="../pfccomet.js"></script> + </head> + <body> + +<div id="chat"> +<script type="text/javascript"> +var comet = new pfcComet({'read_url': './comet-getdata.php'}); +comet.onDisconnect = function(comet) { + $('btn_connect').show(); + $('btn_disconnect').hide(); + $('ajaxlogo').hide(); +}; +comet.onConnect = function(comet) { + $('btn_disconnect').show(); + $('btn_connect').hide(); + $('ajaxlogo').show(); +}; +comet.onResponse = function(req) { new Insertion.Top($('content'),'<p>'+req['data']+'</p>'); }; +$('btn_connect').show(); +$('btn_disconnect').hide(); +$('ajaxlogo').hide(); +</script> + +<p> + <form action="" method="get" onsubmit="new Ajax.Request('./comet-postdata.php', { method: 'get', parameters: { 'msg' : $('nick').value + ': ' + $('word').value } } ); $('word').value = ''; return false;"> + <input type="button" value="Connect" id="btn_connect" onclick="comet.connect()" /> + <input type="button" value="Disconnect" id="btn_disconnect" onclick="comet.disconnect()" /> + <br/> + <input type="text" name="nick" id="nick" value="" /> + <input type="text" name="word" id="word" value="" /> + <input type="submit" name="submit" value="Send" /> + </form> +</p> + +<div id="content"> +</div> + +<p><img id="ajaxlogo" src="http://bulletproofajax.com/code/chapter06/loading/people/loading.gif" alt="" /></p> + +<script type="text/javascript"> +$('btn_connect').show(); +$('btn_disconnect').hide(); +$('ajaxlogo').hide(); +</script> + +</div> + + </body> +</html> \ No newline at end of file Added: branches/pfc-comet/misc/comet-tests/chat/comet-getdata.php =================================================================== --- branches/pfc-comet/misc/comet-tests/chat/comet-getdata.php (rev 0) +++ branches/pfc-comet/misc/comet-tests/chat/comet-getdata.php 2007-08-01 14:05:22 UTC (rev 1067) @@ -0,0 +1,32 @@ +<?php + +$filename = dirname(__FILE__).'/data.txt'; + +// infinite loop until the data file is not modified +$lastmodif = isset($_GET['id']) ? $_GET['id'] : 0; +$currentmodif = filemtime($filename); +while ($currentmodif <= $lastmodif) // check if the data file has been modified +{ + usleep( 10000); // sleep 10ms to unload the CPU + //usleep(1010000); // sleep 1sec10ms to unload the CPU + clearstatcache(); + $currentmodif = filemtime($filename); +} + + +$lines = file($filename); +$data = ''; +foreach($lines as $l) +{ + $l = explode("\t",$l); + if ($l[0] > $lastmodif) $data .= $l[1]; +} + +// return a json array +$response = array(); +$response['data'] = $data; +$response['id'] = $currentmodif; +echo json_encode($response); +flush(); + +?> \ No newline at end of file Added: branches/pfc-comet/misc/comet-tests/chat/comet-postdata.php =================================================================== --- branches/pfc-comet/misc/comet-tests/chat/comet-postdata.php (rev 0) +++ branches/pfc-comet/misc/comet-tests/chat/comet-postdata.php 2007-08-01 14:05:22 UTC (rev 1067) @@ -0,0 +1,16 @@ +<?php + +$filename = dirname(__FILE__).'/data.txt'; + +// store new message in the file +$msg = isset($_GET['msg']) ? $_GET['msg'] : ''; +if ($msg != '') +{ + $lines = file($filename); + $lines = array_slice($lines, -5); + $lines[] = time()."\t".$msg."\n"; + file_put_contents($filename,implode("",$lines)); + die(); +} + +?> \ No newline at end of file Added: branches/pfc-comet/misc/comet-tests/chat/data.txt =================================================================== --- branches/pfc-comet/misc/comet-tests/chat/data.txt (rev 0) +++ branches/pfc-comet/misc/comet-tests/chat/data.txt 2007-08-01 14:05:22 UTC (rev 1067) @@ -0,0 +1,6 @@ +1185975889 azd: +1185975890 azd: +1185975890 azd: +1185975890 azd: +1185976080 azd: eee +1185976081 azd: Added: branches/pfc-comet/misc/comet-tests/pfccomet.js =================================================================== --- branches/pfc-comet/misc/comet-tests/pfccomet.js (rev 0) +++ branches/pfc-comet/misc/comet-tests/pfccomet.js 2007-08-01 14:05:22 UTC (rev 1067) @@ -0,0 +1,96 @@ +/** + * 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.onRead = function(req) { alert('id:'+req['id']+' response:'+req['data']); }; + * comet.onConnect = function(comet) { alert('connected'); }; + * comet.onDisconnect = function(comet) { alert('disconnected'); }; + */ +var pfcComet = Class.create(); +pfcComet.prototype = { + + read_url: null, + id: 0, + timeout: 5000, + + _noerror: false, + _ajax: null, + _isconnected: false, + + initialize: function(params) { + if (!params) params = {}; + if (!params['read_url']) alert('error: read_url parameter is mandatory'); +// if (!params['write_url']) alert('error: write_url parameter is mandatory'); + this.read_url = params['read_url']; + if (params['id']) this.id = params['id']; + if (params['timeout']) this.timeout = params['timeout']; + }, + + connect: function() + { + if (this._isconnected) return; + this._isconnected = true; + this.onConnect(this); + this._waitForData(); + }, + + disconnect: function() + { + if (!this._isconnected) return; + this._isconnected = false; + this.onDisconnect(this); + // remove the registred callbacks in order to ignore the next response + this._ajax.transport.abort(); +// this._ajax.options.onSuccess = null; +// this._ajax.options.onComplete = null; + }, + + write: function(data) + { + }, + + _waitForData: function() + { + if (!this._isconnected) return; + + this._ajax = new Ajax.Request(this.read_url, { + method: 'get', + parameters: { 'id' : this.id }, + onSuccess: function(transport) { + // handle the server response + var response = transport.responseText.evalJSON(); + this.comet.id = response['id']; + this.comet.onResponse(response); + this.comet._noerror = true; + }, + onComplete: function(transport) { + // send a new ajax request when this request is finished + if (!this.comet._noerror) + // if a connection problem occurs, try to reconnect periodicaly + setTimeout(function(){ this.comet._waitForData(); }.bind(this), this.comet.timeout); + else + // of wait for the next data + this.comet._waitForData(); + this.comet._noerror = false; + } + }); + this._ajax.comet = this; + }, + + /** + * User's callbacks + */ + onRead: function(data) + { + }, + onWrite: function(data) + { + }, + onConnect: function(comet) + { + }, + onDisconnect: function(comet) + { + }, +} Modified: branches/pfc-comet/themes/default/chat.js.tpl.php =================================================================== --- branches/pfc-comet/themes/default/chat.js.tpl.php 2007-08-01 13:38:51 UTC (rev 1066) +++ branches/pfc-comet/themes/default/chat.js.tpl.php 2007-08-01 14:05:22 UTC (rev 1067) @@ -128,7 +128,6 @@ <script type="text/javascript" src="<?php echo $c->data_public_url; ?>/js/pfcgui.js"></script> <script type="text/javascript" src="<?php echo $c->data_public_url; ?>/js/pfcresource.js"></script> <script type="text/javascript" src="<?php echo $c->data_public_url; ?>/js/pfcprompt.js"></script> -<script type="text/javascript" src="<?php echo $c->data_public_url; ?>/js/pfccomet.js"></script> <div id="pfc_notloading" style="width:270px;background-color:#FFF;color:#000;border:1px solid #000;text-align:center;margin:5px auto 0 auto;font-size:10px;background-image:url('http://img402.imageshack.us/img402/3766/stopcc3.gif');background-repeat:no-repeat;background-position:center center;"> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |