silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Silverlight+WCF双工通信开发聊天室精简版服务器端代码大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

一.创建WCF应用程序        接口文件:   [serviceContract(Namespace = "Silverlight", CallBACkContract = typeof(IDuplexClient))]       public interface IDuplexservice       {            /// 

一.创建WCF应用程序

       接口文件

 

 
 
  1. [serviceContract(Namespace = "Silverlight", CallBACkContract = typeof(IDuplexClient))]  
  2.     public interface IDuplexservice  
  3.     {  
  4.          /// <sumMary>  
  5.         /// 登陆的时候调用  
  6.         /// </sumMary>  
  7.         /// <param NAME="name">名字</param>  
  8.         /// <param NAME="quantity">id</param>  
  9.         [OperationContract(IsOneWay = true)]  
  10.         void Login(String NAMEint 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 = true)]  
  18.         void Say(String message, int id,int uid);  
  19.     }  
  20.  
  21.    
  22.  
  23.    
  24.  
  25. ///客服端调用  
  26.  
  27.     //[serviceContract]  
  28.     public interface IDuplexClient  
  29.     {  
  30.  
  31.          //返回登陆人员  
  32.          [OperationContract(IsOneWay = true)]  
  33.         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 = true)]  
  40.         void rSysmessage(String message, int type,int id);  
  41.     }  
  42.  
  43. //封装要传送的数据  
  44.  
  45.  [DataContract]  
  46.     public class UserInfo  
  47.     {  
  48.         public UserInfo(String NAME,int id)  
  49.         {  
  50.             _id = id;  
  51.             _name = NAME;  
  52.         }  
  53.         [DataMember]  
  54.        public  int _id { getset; }  
  55.         [DataMember]  
  56.        public   String _name { getset; }  
  57.     }  

 二. 实现接口

 

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

 

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

大佬总结

以上是大佬教程为你收集整理的Silverlight+WCF双工通信开发聊天室精简版服务器端代码全部内容,希望文章能够帮你解决Silverlight+WCF双工通信开发聊天室精简版服务器端代码所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。