大佬教程收集整理的这篇文章主要介绍了Silverlight+WCF双工通信开发聊天室精简版服务器端代码,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
一.创建WCF应用程序
接口文件:
- [serviceContract(Namespace = "Silverlight", CallBACkContract = typeof(IDuplexClient))]
- public interface IDuplexservice
- {
- /// <sumMary>
- /// 登陆的时候调用
- /// </sumMary>
- /// <param NAME="name">名字</param>
- /// <param NAME="quantity">id</param>
- [OperationContract(IsOneWay = true)]
- void Login(String NAME, int id);
- /// <sumMary>
- /// 发送消息
- /// </sumMary>
- /// <param NAME="message">消息内容</param>
- /// <param NAME="id">大于0为私聊</param>
- /// <param NAME="id">自己的ID</param>
- [OperationContract(IsOneWay = true)]
- void Say(String message, int id,int uid);
- }
- ///客服端调用
- //[serviceContract]
- public interface IDuplexClient
- {
- //返回登陆人员
- [OperationContract(IsOneWay = true)]
- void rFriendList(List<UserInfo> friendList);
- /// <sumMary>
- /// 系统消息
- /// </sumMary>
- /// <param NAME="message">返回消息</param>
- /// <param NAME="type">0代表异地登陆,-1代表用户异常,-2用户上线,-3用户下线</param>
- [OperationContract(IsOneWay = true)]
- void rSysmessage(String message, int type,int id);
- }
- //封装要传送的数据
- [DataContract]
- public class UserInfo
- {
- public UserInfo(String NAME,int id)
- {
- _id = id;
- _name = NAME;
- }
- [DataMember]
- public int _id { get; set; }
- [DataMember]
- public String _name { get; set; }
- }
二. 实现接口
- public class orderservice : IDuplexservice
- {
- IDuplexClient client = OperationContext.Current.GetCallBACkChAnnel<IDuplexClient>();//构建用户通道
- List<UserInfo> lst = new List<UserInfo>();//存放登录用户
- private static Dictionary<IDuplexClient, UserInfo> onlineUser = new Dictionary<IDuplexClient, UserInfo>();//用每个用户通道和用户信息组成字典存储当前用户列表
- #region IDuplexservice 成员
- public void Login(String NAME, int id)
- {
- //判断是否已经登陆
- if (onlineUser.Values.Where(h => h._name == Name).Count() > 0)
- {
- IDuplexClient repertClient = onlineUser.Where(f => f.Value._name == Name).First().Key;
- //返回给此客户端重复登陆信息,强迫其下线
- onlineUser.Remove(repertClient);
- repertClient.rSysmessage("帐号在异地登录,被迫下线。", -1,0);
- //通知好友你上线了
- foreach (var ou in onlineUser)
- {
- ou.Key.rSysmessage(name + ":下线了", -3, id);//回调接收方
- }
- }
- loginChat(name, id);
- }
- public void Say(String message,int uid)
- {
- IDuplexClient client = null;
- if (id == 0)//群聊
- {
- foreach (var ou in onlineUser)
- {
- ou.Key.rSysmessage(message, id, uid);//回调接收方
- }
- }
- else
- {
- client = chAnnel(id);//获取接收方通道
- if (client != null)//如果接收方在线
- {
- client.rSysmessage(message, uid);//回调接收方
- }
- }
- }
- #endregion
- public void loginChat(String userName, int id)//登陆聊天模块
- {
- lst.Add(new UserInfo(userName, id));
- client.rFriendList(lst);//返回好友列表
- //通知好友你上线了
- foreach (var ou in onlineUser)
- {
- ou.Key.rSysmessage(userName + ":上线了", -2, id);//回调接收方
- }
- onlineUser.Add(client, new UserInfo(userName, id));//新用户添加到在线用户列表
- }
- private IDuplexClient chAnnel(int id)//获取通道
- {
- IDuplexClient client = null;
- //确定接收人在线
- if (onlineUser.Values.Where(f => f._id == id).Count() > 0)
- {
- return onlineUser.Where(f => f.Value._id == id).First().Key;//找到此通道
- }
- return client;
- }
- }
参考文献:http://codeadmin.blog.163.com/blog/static/1158046532011626111624369/
以上是大佬教程为你收集整理的Silverlight+WCF双工通信开发聊天室精简版服务器端代码全部内容,希望文章能够帮你解决Silverlight+WCF双工通信开发聊天室精简版服务器端代码所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。