Skip to content
Browse files

initial import

  • Loading branch information...
0 parents commit b27ad13f0256d6a36be3622c71dde5b5705c9ebd @gonzalo123 committed Mar 11, 2011
Showing with 192 additions and 0 deletions.
  1. +74 −0 NovComet.js
  2. +83 −0 NovComet.php
  3. +35 −0 index.php
74 NovComet.js
@@ -0,0 +1,74 @@
+/**
+ * Lazy Comet. JS client (publisher and subscriber)
+ *
+ * THIS PROGRAM COMES WITH ABSOLUTELY NO WARANTIES !
+ * USE IT AT YOUR OWN RISKS !
+ *
+ * @author Gonzalo Ayuso <gonzalo123@gmail.com>
+ * @copyright under GPL 2 licence
+ */
+
+NovComet = {
+ sleepTime: 1000,
+ _subscribed: {},
+ _timeout: undefined,
+ _baseurl: "comet.php",
+ _args: '',
+ _urlParam: 'subscribed',
+
+ subscribe: function(id, callback) {
+ NovComet._subscribed[id] = {
+ cbk: callback,
+ timestamp: NovComet._getCurrentTimestamp()
+ };
+ return NovComet;
+ },
+
+ _refresh: function() {
+ NovComet._timeout = setTimeout(function() {
+ NovComet.run()
+ }, NovComet.sleepTime);
+ },
+
+ init: function(baseurl) {
+ if (baseurl!=undefined) {
+ NovComet._baseurl = baseurl;
+ }
+ },
+
+ _getCurrentTimestamp: function() {
+ return Math.round(new Date().getTime() / 1000);
+ },
+
+ run: function() {
+ var cometCheckUrl = NovComet._baseurl + '?' + NovComet._args;
+ for (var id in NovComet._subscribed) {
+ var currentTimestamp = NovComet._subscribed[id]['timestamp'];
+
+ cometCheckUrl += '&' + NovComet._urlParam+ '[' + id + ']=' +
+ currentTimestamp;
+ }
+ cometCheckUrl += '&' + NovComet._getCurrentTimestamp();
+ $.getJSON(cometCheckUrl, function(data){
+ switch(data.s) {
+ case 0: // sin cambios
+ NovComet._refresh();
+ break;
+ case 1: // trigger
+ for (var id in data['k']) {
+ NovComet._subscribed[id]['timestamp'] = data['k'][id];
+ NovComet._subscribed[id].cbk(data.k);
+ }
+ NovComet._refresh();
+ break;
+ }
+ });
+
+ },
+
+ publish: function(id) {
+ var cometPublishUrl = NovComet._baseurl + '?' + NovComet._args;
+ cometPublishUrl += '&publish=' + id;
+ $.getJSON(cometPublishUrl);
+ }
+};
83 NovComet.php
@@ -0,0 +1,83 @@
+<?php
+/**
+ * Lazy Comet with PHP (publisher and subscriber)
+ *
+ * THIS PROGRAM COMES WITH ABSOLUTELY NO WARANTIES !
+ * USE IT AT YOUR OWN RISKS !
+ *
+ * @author Gonzalo Ayuso <gonzalo123@gmail.com>
+ * @copyright under GPL 2 licence
+ */
+class NovComet {
+ const COMET_OK = 0;
+ const COMET_CHANGED = 1;
+
+ private $_tries;
+ private $_var;
+ private $_sleep;
+ private $_ids = array();
+ private $_callback = null;
+
+ public function __construct($tries = 20, $sleep = 2)
+ {
+ $this->_tries = $tries;
+ $this->_sleep = $sleep;
+ }
+
+ public function setVar($key, $value)
+ {
+ $this->_vars[$key] = $value;
+ }
+
+ public function setTries($tries)
+ {
+ $this->_tries = $tries;
+ }
+
+ public function setSleepTime($sleep)
+ {
+ $this->_sleep = $sleep;
+ }
+
+ public function setCallbackCheck($callback)
+ {
+ $this->_callback = $callback;
+ }
+
+ const DEFAULT_COMET_PATH = "/dev/shm/%s.comet";
+
+ public function run() {
+ if (is_null($callback)) {
+ $defaultCometPAth = self::DEFAULT_COMET_PATH;
+ $callback = function($id) use ($defaultCometPAth) {
+ $cometFile = sprintf($defaultCometPAth, $id);
+ return (is_file($cometFile)) ? filemtime($cometFile) : 0;
+ };
+ } else {
+ $callback = $this->_callback;
+ }
+
+ for ($i = 0; $i < $this->_tries; $i++) {
+ foreach ($this->_vars as $id => $timestamp) {
+ if ((integer) $timestamp == 0) {
+ $timestamp = time();
+ }
+ $fileTimestamp = $callback($id);
+ if ($fileTimestamp > $timestamp) {
+ $out[$id] = $fileTimestamp;
+ }
+ clearstatcache();
+ }
+ if (count($out) > 0) {
+ return json_encode(array('s' => self::COMET_CHANGED, 'k' => $out));
+ }
+ sleep($this->_sleep);
+ }
+ return json_encode(array('s' => self::COMET_OK));
+ }
+
+ public function publish($id)
+ {
+ return json_encode(touch(sprintf(self::DEFAULT_COMET_PATH, $id)));
+ }
+}
35 index.php
@@ -0,0 +1,35 @@
+<html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <title>Comet Test</title>
+ </head>
+ <body>
+ <p><a class='customAlert' href="#">publish customAlert</a></p>
+ <p><a class='customAlert2' href="#">publish customAlert2</a></p>
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" type="text/javascript"></script>
+ <script src="NovComet.js" type="text/javascript"></script>
+ <script type="text/javascript">
+NovComet.subscribe('customAlert', function(data){
+ console.log('customAlert');
+ //console.log(data);
+}).subscribe('customAlert2', function(data){
+ console.log('customAlert2');
+ //console.log(data);
+});
+
+$(document).ready(function() {
+ $("a.customAlert").click(function(event) {
+ NovComet.publish('customAlert');
+ });
+
+ $("a.customAlert2").click(function(event) {
+ NovComet.publish('customAlert2');
+ });
+
+ NovComet.run();
+
+});
+ </script>
+
+ </body>
+</html>

0 comments on commit b27ad13

Please sign in to comment.
Something went wrong with that request. Please try again.