C# SignalR 即时通讯 聊天室
一.SignalR簡介
SignalR:當所連接的客戶端變得可用時服務(wù)器代碼可以立即向其推送內(nèi)容,而不是讓服務(wù)器等待客戶端請求新的數(shù)據(jù)。實現(xiàn)實時服務(wù)器與客戶端通信。是一個開源.NET 庫生成需要實時用戶交互或?qū)崟r數(shù)據(jù)更新的 web 應用程序。
SignalR的出現(xiàn),讓頁面通過javascript可以很簡單的調(diào)用后端服務(wù)的方法,而在后端也可以很簡單的直接調(diào)用javascript所實現(xiàn)的方法,前后端可以進行實時通信。實現(xiàn)了服務(wù)器主動推送(Push)消息到客戶端頁面,這樣客戶端就不必重新發(fā)送請求或使用輪詢技術(shù)來獲取消息。
注意:SignalR 會自動管理連接。客戶端和服務(wù)器之間的連接是持久性的,不像傳統(tǒng)的 HTTP 連接。
二.SignalR傳輸方式
SignalR會根據(jù)當前瀏覽器自動選擇適當?shù)膫鬏敺绞健T谧顗牡那闆r下,SignalR會選擇使用長輪詢(Long Polling).
SignalR會依照下列順序來判定使用那種傳輸方式:
- 1.如果瀏覽器是 Internet Explorer8 或更早版本,則使用長輪詢。
- 2.如果配置了 JSONP(即連接啟動時 jsonp 參數(shù)設(shè)置為 true),則使用長輪詢。
- 3.如果要建立跨域連接(即 SignalR 終結(jié)點和宿主頁不在相同的域中),并且滿足以下條件,則會使用 WebSocket:
- 3.1客戶端支持 CORS(跨域資源共享)
- 3.2客戶端支持 WebSocket
- 3.3服務(wù)器支持 WebSocket
- 如果這些條件中的任何一條不滿足,將使用長輪詢.
- 4.如果未配置 JSONP 并且連接沒有跨域,只要客戶端和服務(wù)器都支持的話,將使用 WebSocket。
- 5.如果客戶端或服務(wù)器不支持 WebSocket,則盡量使用服務(wù)器發(fā)送事件。Forever Frame。
- 7.如果 Forever Frame 失敗,則使用長輪詢。
長輪詢(long polling)與傳統(tǒng)Ajax的不同之處:
- 1.服務(wù)器端會阻塞請求直到有數(shù)據(jù)傳遞或超時才返回。
- 2.客戶端 JavaScript 響應處理函數(shù)會在處理完服務(wù)器返回的信息后,再次發(fā)出請求,重新建立連接。
- 3.當客戶端處理接收的數(shù)據(jù)、重新建立連接時,服務(wù)器端可能有新的數(shù)據(jù)到達;這些信息會被服務(wù)器端保存直到客戶端重新建立連接,客戶端會一次把當前服務(wù)器端所有的信息取回。
三.SignalR使用(個人理解)
下面是聊天室的主要代碼:
C#代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc;//工具 -> 庫程序包管理器 -> 程序包管理器控制臺 輸入下面命令 //install-package Microsoft.AspNet.SignalR -Version 1.1.4 using Microsoft.AspNet.SignalR; using System.Threading.Tasks; using Microsoft.AspNet.SignalR.Hubs;namespace SignalR.Controllers {[HubName("ChatRoomHub")]public class ChatHub : Hub{static List<UserEntity> users = new List<UserEntity>();/// <summary>/// 添加用戶/// </summary>/// <param name="nickName"></param>public void UserEnter(string nickName){UserEntity userEntity = new UserEntity{NickName = nickName,ConnectionId = Context.ConnectionId};users.Add(userEntity);Clients.All.NotifyUserEnter(nickName, users);//調(diào)用前臺NotifyUserEnter方法 }/// <summary>/// 發(fā)送消息/// </summary>/// <param name="nickName"></param>/// <param name="message"></param>public void SendMessage(string nickName, string message){Clients.All.NotifySendMessage(nickName, message);//調(diào)用前臺NotifySendMessage方法 }/// <summary>/// 斷開(刷新頁面可以觸發(fā)此方法)/// </summary>/// <returns></returns>public override Task OnDisconnected(){var currentUser = users.FirstOrDefault(u => u.ConnectionId == Context.ConnectionId);if (currentUser != null){users.Remove(currentUser);Clients.Others.NotifyUserLeft(currentUser.NickName, users);//調(diào)用前臺NotifyUserLeft方法 }return base.OnDisconnected();}}public class UserEntity{public string NickName { get; set; }public string ConnectionId { get; set; }}public class BaseController : Controller{/// <summary>/// 聊天室/// </summary>/// <returns></returns>public ActionResult BroadcastTest(){return View();}} }
前臺主要JavaScript代碼:
<script type="text/javascript">var userNickName;//昵稱var notification;//消息 jQuery(document).ready(function () {//沒有用戶名彈出輸入框while (!userNickName) {userNickName = window.prompt("請輸入昵稱!");}var chatHub = $.connection.ChatRoomHub;//對應后臺的類ChatHub//添加用戶chatHub.client.NotifyUserEnter = function (nickName, users) {buildUserTemplate(users);}//用戶離開chatHub.client.NotifyUserLeft = function (nickName, users) {buildUserTemplate(users);}//處理消息內(nèi)容chatHub.client.NotifySendMessage = function (nickName, message) {var userAvatar = 'http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/001/face/29.gif';//判斷消息歸屬if (nickName == userNickName) {$("#div_msg").append("<div style='text-align:right;'>"+ "<div float:right> <span style='margin-right:10px'>" + nickName + "</span>"+ "<img src='" + userAvatar + "' style='height:30px;width:30px;position:relative'/>"+ "<div class='demo clearfix fr'>"+ "<span class='triangle'></span>"+ "<div class='article' style='word'>" + message+ "</div></div></div></div><div class='clear-float'/>");}else {$("#div_msg").append("<div>"+ "<img src='" + userAvatar + "' style='height:30px;width:30px;position:relative'/>"+ "<span style='left:10px;position:relative'>" + nickName + "</span>"+ "<div class='demo clearfix'>"+ "<span class='triangle'></span>"+ "<div class='article'>" + message+ "</div></div></div>");}//消息彈出框if (Notification.permission == "granted") {notification = new Notification(nickName, {body: message,icon: userAvatar,renotify: true,tag: 1,noscreen: true});notification.onclick = function () {notification.close();};} else if (Notification.permission != "denied") {Notification.requestPermission(function (permission) {notification = new Notification(nickName, {body: message,icon: userAvatar,renotify: true,tag: 1,noscreen: true});notification.onclick = function () {notification.close();};});}var objDiv = document.getElementById("div_msgbody");objDiv.scrollTop = objDiv.scrollHeight;}$.connection.hub.start().done(function () {chatHub.server.userEnter(userNickName);});//聊天框發(fā)送消息$("#message").keydown(function (event) {if (event.keyCode == 13) {if ($("#message").val() != "") {chatHub.server.sendMessage(userNickName, $("#message").val());$("#message").val("");}}});//發(fā)送按鈕$("#btn_Send").click(function () {if ($("#message").val() != "") {chatHub.server.sendMessage(userNickName, $("#message").val());$("#message").val("");}});//用戶列表function buildUserTemplate(users) {$("#lab_total").text(users.length);var userTemplate = "<ul style='list-style:none;'>"$.each(users, function (e, v) {var userAvatar = 'http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/001/face/29.gif';userTemplate += "<li style='padding-top:5px;'>"+ "<img class='round-img-list' src='" + userAvatar + "'/>"+ "<label style='color:#666666;margin-left:10px'>" + v.NickName + "</label>"+ "</li>";});userTemplate += "</ul>";$("#div_member").html(userTemplate);}});</script>完整SignalR的源碼(包括聊天室,進度條)(我用的vs2013):
百度網(wǎng)盤:鏈接: https://pan.baidu.com/s/1gf7s5oB 密碼: mdi2
最后:
1、Clients.All.NotifySendMessage(nickName, message);調(diào)用的是前臺JschatHub.client.NotifySendMessage = function (nickName, message) {}這段代碼。
2、javascript中,注意使用client和server關(guān)鍵字來調(diào)用前端方法和后端方法。
聊天室例子:
下面是進度條例子:
廣播例子:
?
相關(guān)文章:http://www.cnblogs.com/frozenzhang/p/5406773.html
轉(zhuǎn)載于:https://www.cnblogs.com/cang12138/p/7404124.html
總結(jié)
以上是生活随笔為你收集整理的C# SignalR 即时通讯 聊天室的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows2008 R2 如何建立F
- 下一篇: C#显示百度地图API