Silverlight + WCF novice chess instances get a room status update list (21)

Online Demo: Silverlight + WCF novice examples chess online demo

 

The festival is when a user enters a room, a room of my state to be notified, and notification updates.

When this section to talk about the first time the hall into the room, creating our own room N, the default is the initial state, then we need to get all the rooms have been updated status of the server,

Then down to the local, batch update status.

 

So began, starting with the service side, we have to get all the rooms have been updated, so to add a WCF service method ends:

Add Method to Interface IService.cs

[OperationContract]
Dictionary
< int , Room >  GetRoomList();

 

Then the method to achieve, to go Service.cs

The sun, this approach is too simple, because we have a global roomList before the object directly back on it.

public  Dictionary < int , Room >  GetRoomList()
{
  
return  roomList;
}

 

 

Two or three lines of code to get the server up. So we quietly back to the client

Of course, still have to unravel, update the service reference, say this much, everyone consciously point.

We go back to the room page Room.xaml.cs

We add two lines of code in the constructor, the default request room list:

Because some of the code previously, everyone see the comment that two lines:

05233754_mrNW.gif
public  Room ()
{
  InitializeComponent ();
  Game 
= new new  Game ();   game.CreateGameRoom ( 30 );   game.DrawIn (the LayoutRoot); // look here to see here, these two lines are newly added, get room list App. client.GetRoomListCompleted  + = new new  the EventHandler < GameService.GetRoomListCompletedEventArgs > (client_GetRoomListCompleted); App.client.GetRoomListAsync (); // here implemented method ICallBack App.client.NotifyRoomUpdateReceived  + = new new  the EventHandler < GameService.NotifyRoomUpdateReceivedEventArgs >  



 


  (client_NotifyRoomUpdateReceived);
}
 
// here also depends on where the room is to obtain a list of events void  client_GetRoomListCompleted ( Object  SENDER, GameService.GetRoomListCompletedEventArgs E) { // room obtaining finished, to be achieved }


   

 

See, after the acquisition, we want to do with that? When the real is to achieve a status update room.

05233754_mrNW.gif
// here also depends on where the event is to obtain a list of the room void  client_GetRoomListCompleted ( Object  SENDER, GameService.GetRoomListCompletedEventArgs E)         { // room obtaining finished, to achieve the following IF  (e.Result  == null )             { return ;             } // changing room list             the Dictionary < int , GameService.Room >  RoomList  =  e.Result  AS  the Dictionary < int , GameService.Room > ; IF  (roomList.Count  > 0 )
        

            

              

                


            


            
 
            {
                
foreach  (KeyValuePair < int , GameService.Room >  item  in  roomList)
                {
                    
if  (item.Key  >   0   &&  item.Key  <   31 )
                    {
                        UpdateRoomState(item.Value, game.GameRoomList[item.Key 
-   1 ]);
                    }
                }
            }
        }

 

See that UpdateRoomState function, not to mention do not know, on the last section when we notice the room updates, special Piyang package together.

Get, this particular section of the code is simple, especially simple, then press F5 to see the effect:

The beginning of the landing, entered the room and not the shots, the series cut too much.

Here is the Index page plus a user to display text, blank-free, first go in the following figure:

Well, more than a browser to open the second user login:

Once inside, the default position of the first user on a bright blue light

 OK, this section to this end.

Phase IV source code provided here: Download

 

Reproduced in: https: //my.oschina.net/secyaher/blog/274359

Guess you like

Origin blog.csdn.net/weixin_33751566/article/details/91966987