This sample shows how to create a simple Chat Room application with DataSnap. Chat Room allows public and private chatting through HTTP.
You can find the Chat Room sample project at:
- Start | Programs | Appmethod 1.14 | Samples and then navigate to Object Pascal\DataSnap\ChatRoomDemo
- Subversion Repository for Object Pascal: http://sourceforge.net/p/radstudiodemos/code/HEAD/tree/branches/RadStudio_XE6/Object%20Pascal/DataSnap/ChatRoomDemo/
The Chat Room application is used to:
- Create a Chat Room Server from where the administrator can control user behavior and can send global messages.
- Provide an easy way to use an HTTP web interface from where users can simply forward messages to each other, or exchange global messages.
How to Use the Sample
- Navigate to Start | Programs | Appmethod 1.14 | Samples, go to Object Pascal\DataSnap\ChatRoomDemo, and open ChatRoomServerProject.dproj.
- Build ChatRoomServerProject.dproj.
- Run the application by selecting the Run > Run menu from within the IDE, or by pressing F9.
- Browse to the machine running the Chat Room application -- for instance, http://mymachine.mydomain.net:8087/ChatRoom.html, or http://localhost:8087/ChatRoom.html
- Supply a user name and start chatting in the network.
- From the running Chat Room Server application you can remove users or send global messages, as administrator.
The server container that holds the DataSnap Server components.
The server methods container holding source code for the server methods used in the Chat Room Sample.
The project itself.
The main form that constitutes the user interface for the Chat Room Sample application.
- TUserNotifyThread implements a thread for broadcasting a message to all users, notifying them that a user has logged in or out.
- TBootUserThread boots the user with the given name.
- TChatRoomUser represents a single logged in user of the chat room. The ID of the user's session is held here, as logging in and out of the chat room is done by activing sessions with the server.
- TChatRoomUsers is a singleton class for managing a list of logged in users.
- TChatRoomServerMethods is declared inside the ChatRoomServerMethodsUnit.pas file and implements the server methods for the Chat Room application.
- TUIHookCallback is a no-argument procedure that will tell the user interface to update its user list.
- A TSQLConnection object provides the proxy DataSnap HTTP connection.
- The main user interface contains a list of users and an edit box in which you can type in a message to broadcast to all users. The message you type in is wrapped into a JSON object. The list of users has a pop-up menu associated. This menu contains only one menu item used to throw the user out the chat.
- The SendMessage and SendMessageToUser functions are used to send a message to all logged in users or to a single user.
- The ChatRoomServerContainer.dfm form contains a chat room server (TDSServer component), a chat room transport (TDSTCPServerTransport component), a chat room HTTP service (TDSHTTPService component), a chat room authentication manager (TDSAuthenticationManager component), a chat room server class (TDSServerClass component), and a chat room file dispatcher (TDSHTTPServiceFileDispatcher component).