Making the Client React

It's clear now that the client will also need to hook the receive data event since you've updated the server to actually send data as well. Add the same hook you used for the server into your client application. The actual event handler for the client will be similar as well. Add the event handler found in Listing 19.7 to your client application.

Listing 19.7 Handling Received Data on the Client
 private void OnDataReceive(object sender, ReceiveEventArgs e) {     NetworkMessages msg = (NetworkMessages)e.Message.ReceiveData.Read         (typeof(NetworkMessages));     string newtext = string.Empty;     int playerID = 0;     switch (msg)     {         case NetworkMessages.ChangeName:             playerID = (int)e.Message.ReceiveData.Read(typeof(int));             string newname = e.Message.ReceiveData.ReadString();             newtext = string.Format                 ("DPlay UserId 0x{0} changed name to {1}",                 playerID.ToString("x"), newname);             break;         case NetworkMessages.CheckPlayers:             int count = (int)e.Message.ReceiveData.Read(typeof(int));             newtext = string.Format                 ("Server reports {0} users on the server currently.",                 count);             break;         case NetworkMessages.RunAway:             playerID = (int)e.Message.ReceiveData.Read(typeof(int));             newtext = string.Format                 ("Server reports DPlay UserId 0x{0} has ran away.",                 playerID.ToString("x"));             break;         case NetworkMessages.Wave:             playerID = (int)e.Message.ReceiveData.Read(typeof(int));             newtext = string.Format                 ("Server reports DPlay UserId 0x{0} has waved.",                 playerID.ToString("x"));             break;     }     // We received some data, update our UI     this.BeginInvoke(new AddTextCallback(AddText),         new object[] { newtext }); } 

You can see that this is essentially a simplified version of the server's handler. All you ever do is get the message and update the UI to notify the user about what's going on. You also don't even bother listening for the SendData action, since the server will never send that message.



Managed DirectX 9 Graphics and Game Programming, Kick Start
Managed DirectX 9 Kick Start: Graphics and Game Programming
ISBN: B003D7JUW6
EAN: N/A
Year: 2002
Pages: 180
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net