最近作的一个MVC项目有个模块是要使用即时通讯实现弹幕效果。既要考虑通讯的实时性也要考虑服务器性能和资源消耗,所幸项目对浏览器的版本没有要求。因此我最早想到用WebSocket来实现,在查询资料时, 看到 SignalR 这个技术,也就是本专题所讨论的对象。在拜读了 史上最全面的SignalR系列教程-认识SignalR ,算是对 SignalR有一个简单的认识,SignalR 是 .NET平台为咱们提供的一种简洁高效智能的实时信息交互技术,SignalR 比 WebSocket 还要简单,这简直是个人福音。因此我直接一个Demo开始尝试。javascript
关于SignalR的介绍这边就很少赘述,能够去看我上面说的这篇博文史上最全面的SignalR系列教程-认识SignalR 或者直接看Microsoft文档 Introduction to SignalR,SignalR可用于任何即时性的功能;html
经过第一部分的介绍,相信你们对SignalR有初步的认识,接下来直接上手一个Demo来看看(个人开发环境:VS2019 + .net 4.6.2 + MVC 5)。步骤参考微软教程 Tutorial: Real-time chat with SignalR 2 and MVC 5。java
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace SignalRMvcChat
{
public class ChatHub : Hub
{
public void Send(string name, string message) {
Clients.All.addNewMessageToPage(name, message);
}
}
}
using Owin;
using Microsoft.Owin;
[assembly: OwinStartup(typeof(SignalRMvcChat.Startup))]
namespace SignalRMvcChat
{
public class Startup {
public void Configuration(IAppBuilder app) {
// Any connection or hub wire up and configuration should go here
app.MapSignalR();
}
}
}
注意: SignalR 框架使用Owin组件,每个使用的OWin组件的Web框架都须要一个Startup入口类,用来声明OWin组件:
1.项目会自动扫描程序集根下的名为Startup的类,来做为入口类;
2.经过添加 [assembly: OwinStartup(typeof(SignalRChat.Startup))] 标签标记入口类;
3.若是你的启动类不在当前命名空间下,须要在Web.config中添加节点:,来指定启动类;jquery
public ActionResult Chat() {
return View();
}
@{
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 { <!--jQuery库是必需的,默认状况下在_Layout.cshtml引用. --> <!--引用SignalR库. --> <script src="~/Scripts/jquery.signalR-2.2.2.min.js"></script> <!--引用自动生成的SignalR 集线器(Hub)脚本.在运行的时候在浏览器的Source下可看到. --> <script src="~/signalr/hubs"></script> <script> $(function () { // 引用自动生成的集线器代理 必须用小写字母开头. var chat = $.connection.chatHub; // 定义服务器端调用的客户端addNewMessageToPage来处理接收到的消息. chat.client.addNewMessageToPage = function (name, message) { // 将消息添加到ul上. $('#discussion').append('<li><strong>' + htmlEncode(name) + '</strong>: ' + htmlEncode(message) + '</li>'); }; // 获取用户名. $('#displayname').val(prompt('Enter your name:', '')); // 设置焦点到输入框. $('#message').focus(); // 开始链接服务器 done函数代表创建链接成功后为发送按钮注册了一个click事件. $.connection.hub.start().done(function () { $('#sendmessage').click(function () { // 调用服务器端集线器的Send方法 也要小写开头. chat.server.send($('#displayname').val(), $('#message').val()); // 清空输入框信息并获取焦点. $('#message').val('').focus(); }); }); }); // 这个可选功能将html-encodes消息显示在页面上. function htmlEncode(value) { var encodedValue = $('<div />').text(value).html(); return encodedValue; } </script> }
-进入聊天页面 ~/Home/Chatgit
The following errors occurred while attempting to load the app.web
The following errors occurred while attempting to load the app. - No assembly found containing an OwinStartupAttribute. - No assembly found containing a Startup or [AssemblyName].Startup class. To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config. To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.
以上的问题出如今我第一次运行的时候,看着错误提示是没有找到 OwinStartupAttribute ,而后跟着提示在web.config的appSetting中添加以下的节点:浏览器
<appSettings>
<add key="owin:AutomaticAppStartup" value="false" />
...
</appSettings>
运行竟然真的就不报错,可是问题更大条了。在聊天页面并无生成SignalR代理的hubs的脚本文件,出现了Uncaught TypeError: Cannot read property 'client' of undefined错误
。服务器
对比了微软官方使用 SignalR 步骤,才认识到 SignalR 框架使用Owin组件,必定要指定一个Startup入口类,我是少了以上步骤6,在添加了Startup.cs类以后,问题解决了(添加的Startup.cs类的命名空间为应用程序根目录的名称,去掉以前web.config中加入的错误代码)。对于SignalR代理对象异常,你们还能够看下这篇文章, SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论,只要注意自动生成的集线器脚本中的代理名称和服务端方法都是小写字母开头,调用的时候使用小写就能够避免这个问题。mvc
查看源码app
以上就是今天给你们的分享所有内容了,祝你们元旦快乐!