[Phpfreechat-svn] SF.net SVN: phpfreechat: [1053] trunk
Status: Beta
Brought to you by:
kerphi
From: <ke...@us...> - 2007-07-19 14:13:09
|
Revision: 1053 http://svn.sourceforge.net/phpfreechat/?rev=1053&view=rev Author: kerphi Date: 2007-07-19 07:13:08 -0700 (Thu, 19 Jul 2007) Log Message: ----------- Add the pfcComet javascript class that will be used to retrive data with ajax/comet system See http://www.zeitoun.net/index.php?2007/06/22/46-how-to-implement-comet-with-php Modified Paths: -------------- trunk/themes/default/chat.js.tpl.php Added Paths: ----------- trunk/data/public/js/pfccomet.js Added: trunk/data/public/js/pfccomet.js =================================================================== --- trunk/data/public/js/pfccomet.js (rev 0) +++ trunk/data/public/js/pfccomet.js 2007-07-19 14:13:08 UTC (rev 1053) @@ -0,0 +1,87 @@ +/** + * 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) + { + }, +} Modified: trunk/themes/default/chat.js.tpl.php =================================================================== --- trunk/themes/default/chat.js.tpl.php 2007-07-17 18:34:54 UTC (rev 1052) +++ trunk/themes/default/chat.js.tpl.php 2007-07-19 14:13:08 UTC (rev 1053) @@ -128,6 +128,7 @@ <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. |