在线演示地址:
Silverlight+WCF 新手实例 象棋 在线演示
上节是当有用户进入某个房间时,我的某个房间状态被通知,并被通知更新。
这节说说首次进入房间大厅时,我们自己创建了N个房间,默认都是初始状态的,这时我们需要获取服务端的所有已更新的房间状态,
下到本地之后,进行批量更新状态。
于是开始了,首先从服务端开始,我们要获取所有已更新的房间,于是到WCF服务端添加一个方法:
[OperationContract]
Dictionary < int , Room > GetRoomList();
Dictionary < int , Room > GetRoomList();
接着要实现方法了,到Service.cs去
太阳,这方法太简单了,由于我们之前就有全局的roomList对象,直接返回就可以了。
public
Dictionary
<
int
, Room
>
GetRoomList()
{
return roomList;
}
{
return roomList;
}
两三行代码就搞定了服务端了。于是我们悄悄的回到客户端
当然了,还是得编绎,更新服务引用,这个说多了,大伙自觉点。
我们回到房间页面Room.xaml.cs里去
由于之前已有些代码,大伙看注释那两行:
public
Room()
{
InitializeComponent();
game = new Game();
game.CreateGameRoom( 30 );
game.DrawIn(LayoutRoot);
// 看这里看这里,这两行是新添加的,获取房间列表
App.client.GetRoomListCompleted += new EventHandler < GameService.GetRoomListCompletedEventArgs > (client_GetRoomListCompleted);
App.client.GetRoomListAsync();
// 这里实现ICallBack的方法
App.client.NotifyRoomUpdateReceived += new EventHandler < GameService.NotifyRoomUpdateReceivedEventArgs > (client_NotifyRoomUpdateReceived);
}
// 这里也要看,这里是获取房间列表的事件
void client_GetRoomListCompleted( object sender, GameService.GetRoomListCompletedEventArgs e)
{
// 房间获取完了,待实现
}
{
InitializeComponent();
game = new Game();
game.CreateGameRoom( 30 );
game.DrawIn(LayoutRoot);
// 看这里看这里,这两行是新添加的,获取房间列表
App.client.GetRoomListCompleted += new EventHandler < GameService.GetRoomListCompletedEventArgs > (client_GetRoomListCompleted);
App.client.GetRoomListAsync();
// 这里实现ICallBack的方法
App.client.NotifyRoomUpdateReceived += new EventHandler < GameService.NotifyRoomUpdateReceivedEventArgs > (client_NotifyRoomUpdateReceived);
}
// 这里也要看,这里是获取房间列表的事件
void client_GetRoomListCompleted( object sender, GameService.GetRoomListCompletedEventArgs e)
{
// 房间获取完了,待实现
}
看到了吧,获取完后,我们要干点什么呢?当实是实现更新房间状态了。
//
这里也要看,这里是获取房间列表的事件
void client_GetRoomListCompleted( object sender, GameService.GetRoomListCompletedEventArgs e)
{
// 房间获取完了,下面实现了
if (e.Result == null )
{
return ;
}
// 变化的房间列表
Dictionary < int , GameService.Room > roomList = e.Result as 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 ]);
}
}
}
}
void client_GetRoomListCompleted( object sender, GameService.GetRoomListCompletedEventArgs e)
{
// 房间获取完了,下面实现了
if (e.Result == null )
{
return ;
}
// 变化的房间列表
Dictionary < int , GameService.Room > roomList = e.Result as 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 ]);
}
}
}
}
看到那个UpdateRoomState函数,别说不认识,就上节我们在通知房间更新时,特别皮痒封装起来的。
搞定了,这节的代码都特别简洁,特别简单,接下来按F5看看效果:
一开始的登陆,和进入房间就不截图了,本系列截的太多了。
这里为Index页面加上一句用户显示文字,免的一片空白,第一个进去如下图:
OK,此节就到此结束了。
这里提供第四阶段源码:点击下载
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。