From 45b0fe42a979d444d547a5248eb2e9e915aaf16a Mon Sep 17 00:00:00 2001
From: wei <>
Date: Sun, 14 Jan 2007 02:10:24 +0000
Subject: Add "block-content" to allow user comments on block level elements in
quickstart docs.
---
.../protected/pages/Tutorial/AjaxChat.page | 264 +++++++++++----------
1 file changed, 134 insertions(+), 130 deletions(-)
(limited to 'demos/quickstart/protected/pages/Tutorial/AjaxChat.page')
diff --git a/demos/quickstart/protected/pages/Tutorial/AjaxChat.page b/demos/quickstart/protected/pages/Tutorial/AjaxChat.page
index 2d49c9f6..ec6fc149 100644
--- a/demos/quickstart/protected/pages/Tutorial/AjaxChat.page
+++ b/demos/quickstart/protected/pages/Tutorial/AjaxChat.page
@@ -1,67 +1,67 @@
This tutorial introduces the Prado web application framework's
+ This tutorial introduces the Prado web application framework's
ActiveRecord
- and Active Controls to build a Chat
- web application. It is assumed that you
+ and Active Controls to build a Chat
+ web application. It is assumed that you
are familiar with PHP and you have access to a web server that is able to serve PHP5 scripts.
This basic chat application will utilize the following ideas/components in Prado.
- Building an AJAX Chat Application
- Building an AJAX Chat Application
+
+
In this tutorial you will build an AJAX Chat web application that allows - multiple users to communicate through their web browser. +
In this tutorial you will build an AJAX Chat web application that allows
+ multiple users to communicate through their web browser.
The application consists of two pages: a login page
that asks the user to enter their nickname and the main application chat
page.
- You can try the application locally or at
+ You can try the application locally or at
Pradosoft.com.
- The main application chat page is shown bellow.
+ The main application chat page is shown bellow.
class="figure" />
The download and installation steps are similar to those in +
The download and installation steps are similar to those in
the Currency converter tutorial.
To create the application, we run from the command line the following.
- See the Command Line Tool
+ See the Command Line Tool
for more details.
-
The above command creates the necessary directory structure and minimal +
The above command creates the necessary directory structure and minimal files (including "index.php" and "Home.page") to run a Prado web application. Now you can point your browser's URL to the web server to serve up the index.php script in the chat directory. - You should see the message "Welcome to Prado!" + You should see the message "Welcome to Prado!"
-The first task for this application is to ensure that each user - of the chat application is assigned with a unique (chosen by the user) +
The first task for this application is to ensure that each user + of the chat application is assigned with a unique (chosen by the user) username. To achieve this, we can secure the main chat application page to deny access to anonymous users. First, let us create the Login page with the following code. We save the Login.php and Login.page in the chat/protected/pages/ directory (there should be a Home.page file created by the command line tool).
-The login page contains +
The login page contains
a
Now we wish that if the user is trying to access the main application +
Now we wish that if the user is trying to access the main application
page, Home.page, before they have logged in, the user is presented with
the Login.page first. We add a chat/protected/application.xml configuration
file to import some classes that we shall use later.
-
If you now try to access the Home page by pointing your browser +
If you now try to access the Home page by pointing your browser to the index.php you will be redirected to the Login page.
-The
The
To implement a custom user manager module class we just need +
To implement a custom user manager module class we just need to extends the TModule class and implement the IUserManager interface. The getGuestName(), getUser() and validateUser() methods are required by the IUserManager interface. We save the custom user manager class as App_Code/ChatUserManager.php. -
-+
The getGuestName() method simply returns the name for a guest user and is not used in our application. The getUser() method returns a TUser object if the username @@ -257,25 +257,25 @@ exists in the database, the TUser object is set with role of "normal" that corresponds to the <authorization> rules defined in our config.xml file.
-The addNewUser() and usernameExists() -method uses the ActiveRecord corresponding to the chat_users table to +
The addNewUser() and usernameExists() +method uses the ActiveRecord corresponding to the chat_users table to add a new user and to check if a username already exists, respectively.
-The next thing to do is change the config.xml configuration to use +
The next thing to do is change the config.xml configuration to use our new custom user manager class. We simply change the <module> configuration with id="users".
-To perform authentication, we just want the user to enter a unique -username. We add a +
To perform authentication, we just want the user to enter a unique
+username. We add a
-In the createNewUser method, when the validation passes (that is, +
+In the createNewUser method, when the validation passes (that is, when the user name is not taken) we add a new user. Afterward we perform -a manual login process: -
Finally, we redirect the client to the default Home page.
-If you try to perform a login now, you will receive an error message like
-"Property 'ChatUserRecord::$last_activity' must not be null as defined
+Default Values for ActiveRecord
+
If you try to perform a login now, you will receive an error message like
+"Property 'ChatUserRecord::$last_activity' must not be null as defined
by column 'last_activity' in table 'chat_users'.". This means that the $last_activity
property value was null when we tried to insert a new record. We need to either
define a default value in the corresponding column in the table and allow null values or set the default
-value in the ChatUserRecord class. We shall demonstrate the later by
+value in the ChatUserRecord class. We shall demonstrate the later by
altering the ChatUserRecord with the addition of a set getter/setter
methods for the last_activity property.
-
Now we are ready to build the main chat application. We use a simple +
Now we are ready to build the main chat application. We use a simple
layout that consist of one panel holding the chat messages, one panel
to hold the users list, a textarea for the user to enter the text message
and a button to send the message.
-Prado Chat Demo
+Prado Chat Demo
@@ -422,9 +424,9 @@ and a button to send the message.
<com:TPlaceHolder ID="userList" />
</com:TForm>
@@ -436,23 +438,23 @@ We added two Active Control components: a
We should have some fun before we proceeding with setting up the chat buffering. We want +
We should have some fun before we proceeding with setting up the chat buffering. We want
to see how we can update the current page when we receive a message. First, we add
an OnClick event handler for the Send button.
-
To append or add some content to the message list panel, we need to use -some methods in the +
To append or add some content to the message list panel, we need to use
+some methods in the
To send a message to all the connected users we need to buffer or store +
To send a message to all the connected users we need to buffer or store
the message for each user. We can use the database to buffer the messages. The
chat_buffer table is defined as follows.
-
We finally arrive at the guts of the chat application logic. First, we +
We finally arrive at the guts of the chat application logic. First, we
need to save a received message into the chat buffer for all the
current users. We add this logic in the ChatBufferRecord class.
-
The next piece of the logic is to retrieve the users' messages from the buffer. +
The next piece of the logic is to retrieve the users' messages from the buffer. We simply load all the messages for a particular username and format that message appropriately (remember to escape the output to prevent Cross-Site Scripting attacks). After we load the messages, we delete those loaded messages and any older -messages that may have been left in the buffer. +messages that may have been left in the buffer.
-Now comes to put the application flow together. In the Home.php we update +
Now comes to put the application flow together. In the Home.php we update
the Send buttons OnClick event handler to use the application
logic we just implemented.
-
At this point the application is actually already functional, just not very +
At this point the application is actually already functional, just not very user friendly. If you open two different browsers, you should be able to communicate between the two users whenever the Send button is clicked.
-The next part is perhaps the more tricker and fiddly than the other tasks. We +
The next part is perhaps the more tricker and fiddly than the other tasks. We
need to improve the user experience. First, we want a list of current users
as well. So we add the following method to Home.php, we can call
this method when ever some callback event is raised, e.g. when the Send
button is clicked.
-
Actually, we want to periodically update the messages and user list as new +
Actually, we want to periodically update the messages and user list as new users join in and new message may arrive from other users. So we need to refresh the message list as well.
-Next, we need to redirect the user back to the login page if the user has +
Next, we need to redirect the user back to the login page if the user has
been inactive for some time, say about 5 mins, we can add this check to any stage
of the page life-cycle. Lets add it to the onLoad() stage.
-
The last few details are to periodically check for new messages and +
The last few details are to periodically check for new messages and
refresh the user list. We can accomplish this by polling the server using a
The final piece requires us to use some javascript. We want that when the +
The final piece requires us to use some javascript. We want that when the
user type some text in the textarea and press the Enter key, we want it
to send the message without clicking on the Send button. We add to the
Home.page some javascript.
-
This completes the tutorial on making a basic chat web application using +
This completes the tutorial on making a basic chat web application using the Prado framework. Hope you have enjoyed it.
-- cgit v1.2.3