DataSnap.ChatRoom Sample

From Appmethod Code Examples
Jump to: navigation, search

This sample shows how to create a simple Chat Room application with DataSnap. Chat Room allows public and private chatting through HTTP.

Location

You can find the Chat Room sample project at:

Description

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

  1. Navigate to Start | Programs | Appmethod 1.14 | Samples, go to Object Pascal\DataSnap\ChatRoomDemo, and open ChatRoomServerProject.dproj.
  2. Build ChatRoomServerProject.dproj.
  3. Run the application by selecting the Run > Run menu from within the IDE, or by pressing F9.
  4. Browse to the machine running the Chat Room application -- for instance, http://mymachine.mydomain.net:8087/ChatRoom.html, or http://localhost:8087/ChatRoom.html
  5. Supply a user name and start chatting in the network.
  6. From the running Chat Room Server application you can remove users or send global messages, as administrator.

Files

File Contains

ChatRoomServerContainer

The server container that holds the DataSnap Server components.

ChatRoomServerMethodsUnit

The server methods container holding source code for the server methods used in the Chat Room Sample.

ChatRoomServerProject

The project itself.

ChatRoomServerUnit

The main form that constitutes the user interface for the Chat Room Sample application.

Classes

  • 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.

Procedural Types

  • TUIHookCallback is a no-argument procedure that will tell the user interface to update its user list.

Implementation

  • A TSQLConnection object provides the proxy DataSnap HTTP connection.
  • The Generate JS Proxy button generates the JavaScript proxy information though a TDSJavaScriptProxyWriter.
  • 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).

Uses

See Also