交流羣:195866844html
上週發表了前端
這篇文章,獲得了不少幫助和鼓勵,小弟在此真心的感謝你們的支持..jquery
這周繼續系列2,實現聊天室的功能.數據庫
開發工具:VS2013 旗艦版json
數據庫:未用數組
操做系統:WIN7旗艦版app
首先咱們來看看最終效果:ide
正式開始:工具
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<User>(); Connections = new List<Connection>(); Rooms = new List<ConversationRoom>(); } //用戶集合 public List<User> Users { get; set; } //鏈接集合 public List<Connection> Connections { get; set; } //房間集合 public List<ConversationRoom> Rooms { get; set; } } public class User { [Key] //用戶名 public string UserName { get; set; } //用戶的鏈接 public List<Connection> Connections { get; set; } //用戶房間集合 public virtual List<ConversationRoom> Rooms { get; set; } public User() { Connections = new List<Connection>(); Rooms = new List<ConversationRoom>(); } } public class Connection { //鏈接ID public string ConnectionID { get; set; } //用戶代理 public string UserAgent { get; set; } //是否鏈接 public bool Connected { get; set; } } /// <summary> /// 房間類 /// </summary> public class ConversationRoom { //房間名稱 [Key] public string RoomName { get; set; } //用戶集合 public virtual List<User> Users { get; set; } public ConversationRoom() { Users = new List<User>(); } } }
而後聊天室的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(); } /// <summary> /// 重寫Hub鏈接事件 /// </summary> /// <returns></returns> 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(); } /// <summary> /// 更新全部用戶的房間列表 /// </summary> private void GetRoomList() { var itme = from a in db.Rooms select new { a.RoomName }; string jsondata = JsonConvert.SerializeObject(itme.ToList()); Clients.All.getRoomlist(jsondata); } /// <summary> /// 重寫Hub鏈接斷開的事件 /// </summary> /// <returns></returns> 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(); } /// <summary> /// 加入聊天室 /// </summary> /// <param name="roomName"></param> 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("請勿重複加入房間!"); } } } /// <summary> /// 建立聊天室 /// </summary> /// <param name="roomName"></param> 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("房間名重複!"); } } /// <summary> /// 退出聊天室 /// </summary> /// <param name="roomName"></param> 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("退出成功!"); } } /// <summary> /// 給分組內全部的用戶發送消息 /// </summary> /// <param name="Room">分組名</param> /// <param name="Message">信息</param> public void SendMessage(string Room, string Message) { Clients.Group(Room, new string[0]).sendMessage(Room,Message+" "+DateTime.Now.ToString()); } } }
前端HTML+JS:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script src="Scripts/jquery-1.10.2.min.js"></script> <script src="Scripts/jquery.signalR-2.0.0.min.js"></script> <!--這裏要注意,這是虛擬目錄,也就是你在OWIN Startup中註冊的地址--> <script src="signalr/hubs"></script> <script> var chat var roomcount = 0; $(function () { chat = $.connection.groupsHub; chat.client.showMessage = function (Message) { alert(Message); } chat.client.sendMessage = function (roomname, message) { $("#" + roomname).find("ul").each(function () { $(this).append('<li>'+message+'</li>') }) } chat.client.removeRoom = function (data) { alert(data); } chat.client.addRoom = function (roomname) { var html = '<div style="float:left; margin-left:30px; border:double" id="' + roomname + '" roomname="' + roomname + '"><button onclick="RemoveRoom(this)">退出</button>\ ' + roomname + '房間\ 聊天記錄以下:<ul>\ </ul>\ <input type="text" /> <button onclick="SendMessage(this)">發送</button>\ </div>' $("#RoomList").append(html); } //註冊查詢房間列表的方法 chat.client.getRoomlist = function (data) { if (data) { var jsondata = $.parseJSON(data); $("#roomlist").html(" "); for (var i = 0; i < jsondata.length; i++) { var html = ' <li>房間名:' + jsondata[i].RoomName + '<button roomname="'+jsondata[i].RoomName+'" onclick="AddRoom(this)">加入</button></li>'; $("#roomlist").append(html); } } } // 獲取用戶名稱。 $('#username').html(prompt('請輸入您的名稱:', '')); $.connection.hub.start().done(function () { $('#CreatRoom').click(function () { if (roomcount < 2) { chat.server.creatRoom($("#Roomname").val()); roomcount++; } else { alert("聊天窗口只容許有2個") } }) }); }); function SendMessage(btn) { var message = $(btn).prev().val(); var room = $(btn).parent(); var username = $("#username").html(); message = username + ":" + message; var roomname = $(room).attr("roomname"); chat.server.sendMessage(roomname,message); } function RemoveRoom(btn) { var room = $(btn).parent(); var roomname = $(room).attr("roomname"); chat.server.removeFromRoom(roomname); } function AddRoom(roomname) { var data =$(roomname).attr("roomname"); chat.server.addToRoom(data); } </script> </head> <body> <div> <div>名稱:<p id="username"></p></div> 輸入房間名: <input type="text" value="asdasd" id="Roomname" /> <button id="CreatRoom">建立聊天室</button> </div> <div style="float:left;border:double"> <div>房間列表</div> <ul id="roomlist"> </ul> </div> <div id="RoomList"> </div> </body> </html>
至此就完成了基本的聊天室功能,有許多邏輯寫的不到位的狀況,請你們海涵.
我會堅持寫完本系列..
源碼下載地址:
http://pan.baidu.com/s/1pJLxwF5