微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

silverlight中Wcf双工通信

一.创建WCF应用程序

       接口文件

 

 
 
  1. [ServiceContract(Namespace = "Silverlight", CallbackContract = @H_404_22@typeof(IDuplexClient))]  
  2.     @H_404_22@public @H_404_22@interface IDuplexService  
  3.     {  
  4.          /// <summary>  
  5.         /// 登陆的时候调用  
  6.         /// </summary>  
  7.         /// <param name="name">名字</param>  
  8.         /// <param name="quantity">id</param>  
  9.         [OperationContract(IsOneWay = @H_404_22@true)]  
  10.         @H_404_22@void Login(@H_404_22@string name, @H_404_22@int id);  
  11.         /// <summary>  
  12.         /// 发送消息  
  13.         /// </summary>  
  14.         /// <param name="message">消息内容</param>  
  15.         /// <param name="id">大于0为私聊</param>  
  16.         /// <param name="id">自己的ID</param>  
  17.         [OperationContract(IsOneWay = @H_404_22@true)]  
  18.         @H_404_22@void Say(@H_404_22@string message, @H_404_22@int id,@H_404_22@int uid);  
  19.     }  
  20.  
  21.    
  22.  
  23.    
  24.  
  25. ///客服端调用  
  26.  
  27.     //[ServiceContract]  
  28.     @H_404_22@public @H_404_22@interface IDuplexClient  
  29.     {  
  30.  
  31.          //返回登陆人员  
  32.          [OperationContract(IsOneWay = @H_404_22@true)]  
  33.         @H_404_22@void rFriendList(List<UserInfo> friendList);  
  34.         /// <summary>  
  35.         /// 系统消息  
  36.         /// </summary>  
  37.         /// <param name="message">返回消息</param>  
  38.         /// <param name="type">0代表异地登陆,-1代表用户异常,-2用户上线,-3用户下线</param>  
  39.         [OperationContract(IsOneWay = @H_404_22@true)]  
  40.         @H_404_22@void rSysMessage(@H_404_22@string message, @H_404_22@int type,@H_404_22@int id);  
  41.     }  
  42.  
  43. //封装要传送的数据  
  44.  
  45.  [DataContract]  
  46.     @H_404_22@public @H_404_22@class UserInfo  
  47.     {  
  48.         @H_404_22@public UserInfo(@H_404_22@string name,@H_404_22@int id)  
  49.         {  
  50.             _id = id;  
  51.             _name = name;  
  52.         }  
  53.         [DataMember]  
  54.        @H_404_22@public  @H_404_22@int _id { @H_404_22@get@H_404_22@set; }  
  55.         [DataMember]  
  56.        @H_404_22@public   @H_404_22@string _name { @H_404_22@get@H_404_22@set; }  
  57.     }  

 二. 实现接口

 

 
 
  1. @H_404_22@public @H_404_22@class OrderService : IDuplexService  
  2.     {  
  3.         IDuplexClient client = OperationContext.Current.GetCallbackChannel<IDuplexClient>();//构建用户通道  
  4.         List<UserInfo> lst = @H_404_22@new List<UserInfo>();//存放登录用户  
  5.         @H_404_22@private @H_404_22@static Dictionary<IDuplexClient, UserInfo> onlineUser = @H_404_22@new Dictionary<IDuplexClient, UserInfo>();//用每个用户通道和用户信息组成字典存储当前用户列表  
  6.          
  7.          #region IDuplexService 成员  
  8.  
  9.  
  10.         @H_404_22@public @H_404_22@void Login(@H_404_22@string name, @H_404_22@int id)  
  11.         {  
  12.             //判断是否已经登陆  
  13.  
  14.             @H_404_22@if (onlineUser.Values.Where(h => h._name == name).Count() > 0)  
  15.             {  
  16.                 IDuplexClient repertClient = onlineUser.Where(f => f.Value._name == name).First().Key;  
  17.                 //返回给此客户端重复登陆信息,强迫其下线  
  18.                 onlineUser.Remove(repertClient);  
  19.                 repertClient.rSysMessage("帐号在异地登录,被迫下线。", -1,0);  
  20.                 //通知好友你上线了  
  21.                 @H_404_22@foreach (var ou @H_404_22@in onlineUser)  
  22.                 {  
  23.                     ou.Key.rSysMessage(name + ":下线了", -3, id);//回调接收方  
  24.                 }      
  25.             }  
  26.             loginChat(name, id);  
  27.  
  28.         }  
  29.  
  30.         @H_404_22@public @H_404_22@void Say(@H_404_22@string message,@H_404_22@int uid)  
  31.         {  
  32.             IDuplexClient client = @H_404_22@null;  
  33.             @H_404_22@if (id == 0)//群聊        
  34.             {  
  35.                 @H_404_22@foreach (var ou @H_404_22@in onlineUser)  
  36.                 {  
  37.                     ou.Key.rSysMessage(message, id, uid);//回调接收方  
  38.                 }  
  39.             }  
  40.             @H_404_22@else 
  41.             {  
  42.                 client = channel(id);//获取接收方通道  
  43.  
  44.                 @H_404_22@if (client != @H_404_22@null)//如果接收方在线  
  45.                 {  
  46.                     client.rSysMessage(message, uid);//回调接收方  
  47.                 }  
  48.             }  
  49.         }  
  50.  
  51.         #endregion  
  52.         @H_404_22@public @H_404_22@void loginChat(@H_404_22@string userName, @H_404_22@int id)//登陆聊天模块  
  53.         {  
  54.             lst.Add(@H_404_22@new UserInfo(userName, id));  
  55.  
  56.             client.rFriendList(lst);//返回好友列表  
  57.             //通知好友你上线了  
  58.             @H_404_22@foreach (var ou @H_404_22@in onlineUser)  
  59.             {  
  60.                  ou.Key.rSysMessage(userName + ":上线了", -2, id);//回调接收方  
  61.             }      
  62.             onlineUser.Add(client, @H_404_22@new UserInfo(userName, id));//新用户添加到在线用户列表  
  63.         }  
  64.         @H_404_22@private IDuplexClient channel(@H_404_22@int id)//获取通道  
  65.         {  
  66.             IDuplexClient client = @H_404_22@null;  
  67.             //确定接收人在线  
  68.             @H_404_22@if (onlineUser.Values.Where(f => f._id == id).Count() > 0)  
  69.             {  
  70.                 @H_404_22@return onlineUser.Where(f => f.Value._id == id).First().Key;//找到此通道  
  71.             }  
  72.             @H_404_22@return client;  
  73.         }  
  74.     }  

 

参考文献:http://codeadmin.blog.163.com/blog/static/1158046532011626111624369/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐