一.创建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 id,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, id, 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/
转载于:https://blog.51cto.com/zhaojie/1194246