WikiStrycoreMain Page | About | Help | FAQ | Special pages | Log in

Printable version | Disclaimers | Privacy policy

Ajax Push Engine

From WikiStrycore

http://www.ape-project.org/wiki/index.php/How_to_write_an_application_with_APE

Contents

Set up

MySQL

How To

This sample code will show you  :

Server side :

    function MySQLConnect(ip, user, password, database) {
        var sql = new Ape.MySQL(ip + ":3306", user, password, database);
 
        //onConnect callback
        sql.onConnect = function() {
            Ape.log('You are now connected to MySQL server');
        }
 
        //onError callback
        sql.onError = function(errorNo) {
            Ape.log('Connection error ' + errorNo +' '+ this.errorString());
        }   
 
        return sql;
    }
 
    //connect to MySQL Server
    /**
     * /!\ You must specify a user and password, mysql module does not support yet connecting with a user without password. /!\
     */
    var sql = MySQLConnect('127.0.0.1', 'toor', 'root', 'ape');
 
 
    //Set up a pooller to send keep alive request each 2minutes
    (function() {
        sql.query('SELECT 1', function(res, errorNo) {
            if (errorNo == 8) {//Something went wrong, connection has been closed
                sql =  MySQLConnect('127.0.0.1', 'root', 'toor', 'ape'); //Reconnect to MySQL Server
            }
        }.bind(this));
    }).periodical(1000*60*2);
 
    //Register getInfo command
    Ape.registerCmd('getInfo', true, function(params, cmd) {
        //Get data from mysql table
        sql.query('SELECT age, city FROM users WHERE user = "' + Ape.MySQL.escape(params.user) + '" LIMIT 1', function(res, errorNo) {
             if (errorNo) {
                Ape.log('Request error : ' + errorNo + ' : '+ this.errorString());
                return ['101', 'MYSQL_ERROR'];
            } else {
                //Display to logs data received from mysql
                Ape.log('Fetching ' + res.length + ' result(s)');
 
                Ape.log('- Age : ' + res[0].age + '\n- City : ' + res[0].city);
 
                cmd.sendResponse('info', res[0]);//Send first result to client
             }
        });
    })

Copy this code in a file named MySQLDemo.js put it in the scripts directory of APE. Then edit main.ape.js and add :

include('MySQLDemo.js');

Client side :

        var client = new APE.Client();
 
        //Load APE client
        client.load();
 
        client.addEvent('load', function() {
            client.core.start({'name': '' + Date.now() + ''});//Start with a random name
        });
 
        client.addEvent('ready', function() {
            //send getInfo command
            client.core.request.send('getInfo', {'user': 'efyx'});
 
            //onRaw info callback
            client.onRaw('info', function(params) {
                console.log(params);
            });
        });

Mysql schema for this demo :

--
-- Table structure for table `users`
--
 
CREATE TABLE IF NOT EXISTS `users` (
  `user` varchar(32) NOT NULL,
  `age` int(11) NOT NULL,
  `city` varchar(100) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
--
-- Dumping data for table `users`
--
 
INSERT INTO `users` (`user`, `age`, `city`) VALUES
('efyx', 24, 'Montpellier'),
('korri', 20, 'Montpellier');

MySQL.query

sql.query("SELECT * FROM table", function(res, errorNo) {
    if (errorNo) Ape.log('Request error : ' + errorNo + ' : '+ this.errorString()); 
    else {
        Ape.log('Fetching ' + res.length);
	for(var i = 0; i < res.length; i++) {
            Ape.log(res[i].title);//res[i].<column name>
        }); 
    }
});
sql.query("INSERT INTO table VALUES('a','b','c')", function(res, errorNo) {
    if (errorNo) Ape.log('Request error : ' + errorNo + ' : '+ this.errorString()); 
    else Ape.log('Inserted');
});

Inline push

http://www.ape-project.org/wiki/index.php/Libape-controller
http://github.com/APE-Project/APE_JSF/blob/master/Demos/Controller/test.php
http://www.ifc0nfig.com/using-jquery-with-ape-change-the-background-color-with-php/

http://www.ifc0nfig.com/esenape-send-and-receive-sms-in-real-time-using-ape-jquery-php-and-libape_controller/
http://ape.ifc0nfig.com/Demos/EsenAPE/


Misc

http://www.ifc0nfig.com/diving-into-ape-modules-and-the-jsf-creating-topics-for-channels/

Retrieved from "http://wiki.strycore.com/index.php/Ajax_Push_Engine"

This page has been accessed 2,834 times. This page was last modified on 9 August 2010, at 14:26. Content is available under Attribution-Noncommercial-Share Alike 3.0 Unported.


Find

Browse
Main page
Community portal
Current events
Recent changes
Random page
Help
Edit
View source
Editing help
This page
Discuss this page
New section
Printable version
Context
Page history
What links here
Related changes
My pages
Log in / create account
Special pages
New pages
File list
Statistics
More...