《ASP.NET SignalR系列》第五课 在MVC中使用SignalR
接着上一篇:《ASP.NET SignalR系列》第四课 SignalR自托管(不用IIS)
一、概述
本教程主要阐释了如何在MVC下使用ASP.NET SignalR。
- 添加SignalR库到MVC中。
- 创建hub和OWIN startup 类来推送内容到客户端。
- 在页面中使用SignalR jQuery 库发送消息和呈现从来得更新。
下面屏幕截图展示了一个完成的聊天应用程序
二、创建项目
1.用MVC5 .NET4.5 创建一个名为SignalRChat的项目
2.改变授权.
3.选择 No Authentication
注意: 如果你选择了一个不一样的授权方式有一个 Startup.cs
会自动为你创建; 在下面的步骤中,你就不必自己创建这个类了。
4.打开 Tools | Library Package Manager | Package Manager Console
install-package Microsoft.AspNet.SignalR
5.解决方案中已经为你添加了需要的东西了
6.在解决方案中给项目添加一个名为Hubs文件夹
7.在 Hubs文件夹中添加SignalR节点下的类文件, 名为ChatHub.cs. 可以使用这个类作为服务端hub发送消息到所有的客户端。
8.类代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | using System; using System.Web; using Microsoft.AspNet.SignalR; namespace SignalRChat { public class ChatHub : Hub { public void Send( string name, string message) { // Call the addNewMessageToPage method to update clients. Clients.All.addNewMessageToPage(name, message); } } } |
7.创建Startup类
using Owin; using Microsoft.Owin; [assembly: OwinStartup(typeof(SignalRChat.Startup))] namespace SignalRChat { public class Startup { public void Configuration(IAppBuilder app) { // Any connection or hub wire up and configuration should go here app.MapSignalR(); } } }
8.在HomeController下添加一个action,名为Chat
public ActionResult Chat() { return View(); }
9.添加对应试图
@{ ViewBag.Title = "Chat"; } <h2>Chat</h2> <div class="container"> <input type="text" id="message" /> <input type="button" id="sendmessage" value="Send" /> <input type="hidden" id="displayname" /> <ul id="discussion"> </ul> </div> @section scripts { <!--Script references. --> <!--The jQuery library is required and is referenced by default in _Layout.cshtml. --> <!--Reference the SignalR library. --> <script src="~/Scripts/jquery.signalR-2.1.0.min.js"></script> <!--Reference the autogenerated SignalR hub script. --> <script src="~/signalr/hubs"></script> <!--SignalR script to update the chat page and send messages.--> <script> $(function () { // Reference the auto-generated proxy for the hub. var chat = $.connection.chatHub; // Create a function that the hub can call back to display messages. chat.client.addNewMessageToPage = function (name, message) { // Add the message to the page. $('#discussion').append('<li><strong>' + htmlEncode(name) + '</strong>: ' + htmlEncode(message) + '</li>'); }; // Get the user name and store it to prepend to messages. $('#displayname').val(prompt('Enter your name:', '')); // Set initial focus to message input box. $('#message').focus(); // Start the connection. $.connection.hub.start().done(function () { $('#sendmessage').click(function () { // Call the Send method on the hub. chat.server.send($('#displayname').val(), $('#message').val()); // Clear text box and reset focus for next comment. $('#message').val('').focus(); }); }); }); // This optional function html-encodes messages for display in the page. function htmlEncode(value) { var encodedValue = $('<div />').text(value).html(); return encodedValue; } </script> }
10.运行
11.代码下载:点击下载
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
2010-07-21 截图