原文網址:DataSnap.ChatRoom Sample
無責任翻譯:大匠之風 Eden Wu
DataSnap.ChatRoom Sample
展示如何以 DataSnap 建立簡單的【聊天室應用程式】。聊天室是利用 HTTP 技術進行公共和私人對話。
專案位置
你能夠在以下位置找到【聊天室】範例專案:
- Start | Programs | Embarcadero RAD Studio XE6 | Samples and then navigate to Object Pascal\DataSnap\ChatRoomDemo
- Subversion Repository for Delphi: http://sourceforge.net/p/radstudiodemos/code/HEAD/tree/branches/RadStudio_XE6/Object%20Pascal/DataSnap/ChatRoomDemo/
文章目錄
專案描述
聊天室應用程式會使用到:
- 建立聊天室伺服器,其中管理員能夠控制使用者行為和發送公共訊息(廣播)。
- 提供簡單方式使用 HTTP 網路介面,其中使用者能互相發訊息或交換公共訊息(廣播)。
如何使用範例
- 引導步驟為 Start | Programs | Embarcadero RAD Studio XE6 | Samples, 底下的 Object Pascal\DataSnap\ChatRoomDemo,然後開啟 ChatRoomServerProject.dproj.
- 編譯 ChatRoomServerProject.dproj.
- 在 IDE 中選擇 Run > Run 選項或按下 F9 來執行應用程式。
- 瀏覽到聊天室應用程式的機器,例如以下網址:http://mymachine.mydomain.net:8087/ChatRoom.html 或 http://localhost:8087/ChatRoom.html
- 提供使用者名稱然後開始進行網路聊天。
- 管理員可以在聊天室伺服器應用程式踼除使用者或發送公共訊息。
Files 檔案群
檔案 | 內容 |
---|---|
ChatRoomServerContainer |
伺服器容器,搭載 DataSnap Server 元件。 |
ChatRoomServerMethodsUnit |
伺服器函式容器,搭載聊天室範例中所使用到的伺服器函式。 |
ChatRoomServerProject |
專案本身。 |
ChatRoomServerUnit |
構成聊天室範例應用程式的使用者介面主表單。 |
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
沒有留言:
張貼留言