前言
交流群:195866844
上周发表了
这篇文章,得到了很多帮助和鼓励,小弟在此真心的感谢大家的支持..
这周继续系列2,实现聊天室的功能.
开发环境
开发工具:VS2013 旗舰版
数据库:未用
操作系统:WIN7旗舰版
正文开始
首先我们来看看最终效果:
正式开始:
SignalR作为一个强大的集线器,已经在hub里面集成了Gorups,也就是分组管理,使用方法如下:
//作用:将连接ID加入某个组//Context.ConnectionId 连接ID,每个页面连接集线器即会产生唯一ID//roomName分组的名称Groups.Add(Context.ConnectionId, roomName);//作用:将连接ID从某个分组移除Groups.Remove(Context.ConnectionId, roomName);//作用:调用分组内连接对象注册的本地JS//XXX:本地JS名称//Room:分组名称// new string[0]:过滤(不发送)的连接ID数组 Clients.Group(Room, new string[0]).XXXX
其实SignalR已经帮我们封装的很好了,关键代码其实就这三句..
废话不多说,下面开始讲我的实现.
首先实体类(参考微软Demo):
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data.Entity;using System.ComponentModel.DataAnnotations;using Microsoft.AspNet.SignalR;namespace SignalRTest{ public class UserContext { public UserContext() { Users = new List(); Connections = new List (); Rooms = new List (); } //用户集合 public List Users { get; set; } //连接集合 public List Connections { get; set; } //房间集合 public List Rooms { get; set; } } public class User { [Key] //用户名 public string UserName { get; set; } //用户的连接 public List Connections { get; set; } //用户房间集合 public virtual List Rooms { get; set; } public User() { Connections = new List (); Rooms = new List (); } } public class Connection { //连接ID public string ConnectionID { get; set; } //用户代理 public string UserAgent { get; set; } //是否连接 public bool Connected { get; set; } } /// /// 房间类 /// public class ConversationRoom { //房间名称 [Key] public string RoomName { get; set; } //用户集合 public virtual ListUsers { get; set; } public ConversationRoom() { Users = new List (); } }}
然后聊天室的Hub(这里我就不解释了,每句话我都加了注释,注释+代码方便大家理解..):
using System;using System.Collections.Generic;using System.Linq;using System.Web;using Microsoft.AspNet.SignalR;using System.Threading.Tasks;using Microsoft.AspNet.SignalR.Hubs;using Newtonsoft.Json;namespace SignalRTest{ [HubName("groupsHub")] public class GroupsHub : Hub { public static UserContext db = new UserContext(); public void Hello() { Clients.All.hello(); } ////// 重写Hub连接事件 /// ///public override Task OnConnected() { // 查询用户。 var user = db.Users.SingleOrDefault(u => u.UserName == Context.ConnectionId); //判断用户是否存在,否则添加 if (user == null) { user = new User() { UserName = Context.ConnectionId }; db.Users.Add(user); } //发送房间列表 var itme = from a in db.Rooms select new { a.RoomName }; Clients.Client(this.Context.ConnectionId).getRoomlist(JsonConvert.SerializeObject(itme.ToList())); return base.OnConnected(); } /// /// 更新所有用户的房间列表 /// private void GetRoomList() { var itme = from a in db.Rooms select new { a.RoomName }; string jsondata = JsonConvert.SerializeObject(itme.ToList()); Clients.All.getRoomlist(jsondata); } ////// 重写Hub连接断开的事件 /// ///public override Task OnDisconnected() { var user = db.Users.Where(u => u.UserName == Context.ConnectionId).FirstOrDefault(); //判断用户是否存在,存在则删除 if (user != null) { //删除用户 db.Users.Remove(user); // 循环用户的房间,删除用户 foreach (var item in user.Rooms) { RemoveFromRoom(item.RoomName); } } return base.OnDisconnected(); } /// /// 加入聊天室 /// /// public void AddToRoom(string roomName) { //查询聊天室 var room = db.Rooms.Find(a=>a.RoomName==roomName); //存在则加入 if (room != null) { //查找房间中是否存在此用户 var isuser = room.Users.Where(a => a.UserName == Context.ConnectionId).FirstOrDefault(); //不存在则加入 if (isuser == null) { var user = db.Users.Find(a => a.UserName == Context.ConnectionId); user.Rooms.Add(room); room.Users.Add(user); Groups.Add(Context.ConnectionId, roomName); //调用此连接用户的本地JS(显示房间) Clients.Client(Context.ConnectionId).addRoom(roomName); } else { Clients.Client(Context.ConnectionId).showMessage("请勿重复加入房间!"); } } } ////// 创建聊天室 /// /// public void CreatRoom(string roomName) { var room = db.Rooms.Find(a => a.RoomName == roomName); if (room == null) { ConversationRoom cr = new ConversationRoom() { RoomName = roomName }; //将房间加入列表 db.Rooms.Add(cr); AddToRoom(roomName); Clients.Client(Context.ConnectionId).showMessage("房间创建完成!"); GetRoomList(); } else { Clients.Client(Context.ConnectionId).showMessage("房间名重复!"); } } ////// 退出聊天室 /// /// public void RemoveFromRoom(string roomName) { //查找房间是否存在 var room = db.Rooms.Find(a => a.RoomName == roomName); //存在则进入删除 if (room != null) { //查找要删除的用户 var user = room.Users.Where(a => a.UserName == Context.ConnectionId).FirstOrDefault(); //移除此用户 room.Users.Remove(user); //如果房间人数为0,则删除房间 if (room.Users.Count <= 0) { db.Rooms.Remove(room); } Groups.Remove(Context.ConnectionId, roomName); //提示客户端 Clients.Client(Context.ConnectionId).removeRoom("退出成功!"); } } ////// 给分组内所有的用户发送消息 /// /// 分组名 /// 信息 public void SendMessage(string Room, string Message) { Clients.Group(Room, new string[0]).sendMessage(Room,Message+" "+DateTime.Now.ToString()); } }}
前端HTML+JS:
名称:输入房间名:房间列表
至此就完成了基本的聊天室功能,有许多逻辑写的不到位的情况,请大家海涵.
我会坚持写完本系列..
源码下载地址:
http://pan.baidu.com/s/1pJLxwF5