|
Create a PrismServer Client ApplicationThis article explains how to use the PrismServer component suite to quickly integrate multi-user chat and communication features into a Silverlight application. Click here to return to the PrismServer home page. SCG.Prism The SCG.Prism Silverlight class library contains all of the classes required to connect your Silverlight application to a running PrismServer. Once connected, users of your application can chat, create and enter chat rooms or game subjects, and pass application-specific data amongst themselves. In order to use the PrismServer class library, add a Reference to SCG.Prism.dll in your project's References section.
PrismConnection Class The PrismConnection class encapsulates the connection to the PrismServer, and provides simple properties, methods, and events that let you connect to the server, send information, and handle responses from clients. You can create an instance of the PrismConnection component in your code-behind, or you can actually define the object in your XAML. For Silverlight applications, I've found that I can include PrismConnection in XAML as long as its enclosed in a standard container such as a ContentControl. Here's a sample:
Connecting to a PrismServer It probably goes without saying, but in order to use the PrismConnection component effectively in your Silverlight application, you need have a server to connect to. The PrismServer Server package contains a fully functional server application that you can use as is, or enhance and customize to meet your needs. In order to connect to a PrismServer, you first need to set the following properties of the PrismConnection instance: Host -
Specify the host name or IP address where the PrismServer is running. After the properties are set, you can call the PrismConnection Connect method to attempt to establish a connection. The connection attempt occurs asynchronously, and PrismConnection will respond back by triggering either the ConnectSuccessful or ConnectFailed events. The ConnectFailed event returns a string that describes the reason that the connection attempt failed. Logging into a PrismServer Once a connection has been successfully made, you should proceed to log the user into the server. There are two login methods, one for existing users and one for new users. In your client application you'll typically provide a way for users to indicate whether they are new or already have an account, as well as a Login button. When the user presses Login, call Connect. Then, in ConnectSuccessful, you can call one of the login methods depending on whether the user indicated they were new, or already have an account. The login method for existing users is Login(string userName, string password). This method simply takes the user name and password of the user. The login method for new users is LoginNew(PrismUser user). This method takes a PrismUser parameter. The PrismUser class contains all of the information describing a user, such as user name, password, location, email, and other informational fields. When the user has indicated that they're a new user, you'll create an instance of the PrismUser class, populate the fields with the information the user entered into the user interface, and pass this instance in the LoginNew call. The login methods both execute asynchronously, and respond back to your application with either the LoginSuccessful or LoginFailed events. Responding to Events After your client has successfully logged in, the PrismConnection component will begin receiving events that you can handle. These events are already marshaled to the Silverlight user interface thread. Handle these events to update your user interface in response to new users entering and leaving, and new rooms being created and removed. The primary events you'll be interested are: JoinedRoom - Triggered when the client has entered a new chat room. The event provides the name of the room entered. After a client first logs in, they will receive this event since they've entered the PrismServer "Lobby". RoomAdded/RoomRemoved - Triggered when new rooms are added and removed from the server. You can display the current collection of rooms in a list, tree, or some other type of control in your user interface. UserAddedToRoom/UserLeftRoom - Triggered when a user enters or leaves the current room; that is, the room that the client is currently occupying. The event passes a PrismUser object representing the user that has entered or left the room. Handle these events to display a list of user's in the current room. RoomCountChanged - Triggered when the number of users in a room has changed. Unlike the UserAddedToRoom and UserLeftRoom events, this event occurs for all rooms in the PrismConnection's SubjectName. You can respond to this event to update your user interface to reflect how many users are in each room. Integrating Chat It's very easy to integrate chat into a PrismServer powered Silverlight application. The PrismConnection class offers a SendChat method that you can call to send a string of chat to other clients in the same room. When another users has submitted chat, PrismConnection responds with a ChatMessageRecieved event which contains the string of chat itself, and the PrismUser object representing the user who sent the chat. PrismConnection also offers a parallel method for sending information to clients. The SendData and DataMessageRecieved support sending and receiving string data messages. This mechanism is intended to be used for game or application specific data other than chat. ChatNDraw The PrismServer Client Package includes a fully functional demo application, ChatNDraw. This Silverlight project allows users to login to a PrismServer with an existing or a new account, chat with other users, and draw with a shared blackboard. The source code for ChatNDraw is included in the Package, and you can also try it right now by clicking the link below: http://www.siliconcommandergames.com/ChatNDraw/TestPage.html Download
|
|
|