一、新建项目,选MVC项目默认 添加mvc文件夹和核心引用

二、添加SignaIR包

SignalR的准备:NuGet包管理器搜索:工具——>库程序包管理器——>Microsoft.AspNet.SignalR 或者 工具——>库程序包管理器——>程序包管理器控制台 Install-Package Microsoft.AspNet.SignalR。

三、新建startup文件(在App_Start目录内),用来启动SignalR

选择常规-->选择OWIN Startup类-->修改名字: Startup.cs

using Microsoft.Owin;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; [assembly: OwinStartup(typeof(demo2.App_Start.Startup))]
namespace demo2.App_Start
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// 有关如何配置应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=316888
app.MapSignalR();
}
}
}

项目外ServerHub.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR; namespace ChatSignalR
{
public class ServerHub : Hub
{
public void SendMsg(string message)
{
//调用所有客户端的sendMessage方法
Clients.All.sendMessage(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), message);
} }
}

四、客户端代码

@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/scripts/jquery-1.6.4.min.js"></script>
<!--引用SignalR库. -->
<script src="~/scripts/jquery.signalR-2.3.0.min.js"></script>
<!--引用自动生成的SignalR 集线器(Hub)脚本.在运行的时候在浏览器的Source下可看到
<script src="~/signalr/hubs"></script>这个js。这是在项目中找不到,是有signalr自己生成作为桥接的js。引入最重要的hubs js,这个js其实并不存在,SignalR会反射获取所有供客户端调用的方法放入hubs js中-->
<script src="~/signalr/hubs"></script>
</head>
<body>
<div>
<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="messageBox"></ul>
</div> </div>
</body>
<script>
$(function () {
//引用自动生成的集线器代理
var chat = $.connection.serverHub;
//定义服务器调用的客户端sendMessage来显示新消息
chat.client.sendMessage = function (name, message)
{
//向页面添加消息
$("#messageBox").append('<li><strong style="color:green">'+htmlEncode(name)+'</strong>:'+htmlEncode(message)+'</li>');
}
//设置焦点到输入框
$('#message').focus();
//开始连接服务器
$.connection.hub.start().done(function () { $('#sendmessage').click(function () {
//调用服务器端集线器的Send方法
chat.server.sendMsg($('#message').val());
//清空输入框信息并获取焦点
$("#message").val('').focus();
})
})
});
//为显示的消息进行html编码
function htmlEncode(value)
{
var encodeValue = $('<div/>').text(value).html();
return encodeValue;
}
</script> </html>

扩展 App_Start文件夹内启动文件不变,集线器操作类,实现控制器内发送

1、新建login控制器如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace ChatSignalR.Controllers
{
public class LoginController : Controller
{
// GET: Login
public ActionResult Index()
{
// 判断是否是新用户 if (Request.Cookies["USERNAME"] != null) //如果是新用户,则跳转到新用户页面
{
return RedirectToAction("Index", "Index");
} //iewBag.UserName = HttpUtility.UrlDecode(cookieUserName.Value); return View();
} [HttpPost]
public ActionResult Login(FormCollection fc)
{
string userName = fc["uid"]; Response.Cookies.Add(new HttpCookie("USERNAME", HttpUtility.UrlEncode(userName))); return RedirectToAction("Index", "Index");
}
}
}

View:

@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/scripts/jquery-1.6.4.min.js"></script>
</head>
<body>
<div>
@using (Html.BeginForm("login", "Login", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
<div class="loginBox">
<div class="loginBoxCenter">
<p><input type="text" id="uid" name="uid" class="loginInput" required="required" placeholder="请输入用户名" value="" /></p>
</div>
<div class="loginBoxButtons">
<button class="loginBtn">登录</button>
</div>
</div>
}
</div>
<script type="text/javascript">
$(function () { });
</script>
</body>
</html>

1、新建Index控制器如下

    public class IndexController : Controller
{ /// <summary>
/// Clients,用来主动发送消息
/// </summary>
///
// GET: Index
public ActionResult Index()
{
ViewBag.hard_value = new List<SelectListItem>() {
new SelectListItem(){Value="",Text="xpy0928"},
new SelectListItem(){Value="",Text="cnblogs"}
};
return View();
} [HttpPost]
public ActionResult ClearCookIe()
{ HttpCookie cookies = Request.Cookies["USERNAME"]; //一定要注意设置Cookies是用Response读取是用Request两者不一样!
if (cookies != null)
{
cookies.Expires = DateTime.Today.AddDays(-);
Response.Cookies.Add(cookies);
Request.Cookies.Remove("USERNAME");
} if (Request.Cookies["USERNAME"] == null)
{
return Content("/Login/Index");
}
else {
return Content("fail");
}
} [HttpPost]
public ActionResult SendSystemMsg()
{ //从外部访问类访问服务器上相对应的hub服务方式
var hub = GlobalHost.ConnectionManager.GetHubContext<ServerHub>();
//在集线器外部推送消息
//hub.Clients.All.notice("都起来吃饭了");
hub.Clients.All.sendMessage(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "都起来吃饭了");
if ( == null)
{
return Content("ok");
}
else
{
return Content("fail");
}
} /*
//从外部访问持久性连接服务 方式
var connectionContext = GlobalHost.ConnectionManager.GetConnectionContext<TestConnection>();//管理相对应的持久性连接
connectionContext.Connection.Broadcast("该吃饭了");//向所有已连接的客户端发送信息
*/ }

View

@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title> <script src="~/scripts/jquery-1.6.4.min.js"></script>
<!--引用SignalR库. -->
<script src="~/scripts/jquery.signalR-2.3.0.min.js"></script>
<!--引用自动生成的SignalR 集线器(Hub)脚本.在运行的时候在浏览器的Source下可看到
<script src="~/signalr/hubs"></script>这个js。这是在项目中找不到,是有signalr自己生成作为桥接的js。引入最重要的hubs js,这个js其实并不存在,SignalR会反射获取所有供客户端调用的方法放入hubs js中-->
<script src="~/signalr/hubs"></script> </head>
<body>
<div> @Html.DropDownList("hard-code-dropdownlist", new SelectList(ViewBag.hard_value, "Value", "Text"), new { @class = "btn btn-success dropdown-toggle form-control" }) <div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="messageBox"></ul>
</div> <input type="button" id="clearcookie" value="清楚cookie" />
<input type="button" id="SendSystemMsg" value="发送系统消息" />
</div> </body> <script type="text/javascript">
$(function () {
//引用自动生成的集线器代理
var chat = $.connection.chatHub;
//定义服务器调用的客户端sendMessage来显示新消息
chat.client.sendMessage = function (name, message)
{
//向页面添加消息
$("#messageBox").append('<li><strong style="color:green">'+htmlEncode(name)+'</strong>:'+htmlEncode(message)+'</li>');
}
//设置焦点到输入框
$('#message').focus();
//开始连接服务器
$.connection.hub.start().done(function () {
// 连接成功时
$('#sendmessage').click(function () {
//调用服务器端集线器的Send方法
chat.server.sendMsg($('#message').val());
//清空输入框信息并获取焦点
$("#message").val('').focus();
})
}).fail(function (res) {
// 连接失败时
});
});
$("#clearcookie").click(function () { $.ajax({
url: '/Index/ClearCookIe',
type:'post',
//dataType:'json',
timeout:,
success: function (data, status) {
if(data=="fail")
console.log(data)
else
location.href = data;
},
fail: function (err, status) {
console.log(err)
}
}); })
$("#SendSystemMsg").click(function () { $.ajax({
url: '/Index/SendSystemMsg',
type: 'post',
//dataType:'json',
timeout: ,
success: function (data, status) { console.log(data) },
fail: function (err, status) {
console.log(err)
}
}); })
//为显示的消息进行html编码
function htmlEncode(value)
{
var encodeValue = $('<div/>').text(value).html();
return encodeValue;
}
</script> </html>

另外集线器代码也更改--我放入HubignaIR文件夹内

using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Web; namespace demo2.HubignaIR
{ [HubName("chatHub")] // using Microsoft.AspNet.SignalR.Hubs; // 标记名称供js调用
public class ServerHub : Hub
{
public void SendMsg(string message)
{
//调用所有客户端的sendMessage方法
Clients.All.sendMessage(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), message);
// 获取链接id
var connectionId = Context.ConnectionId;
// 获取cookie
var cookie = HttpUtility.UrlDecode(Context.RequestCookies.ToString()); }
/// <summary>
/// 客户端连接的时候调用
/// </summary>
/// <returns></returns>
public override Task OnConnected()
{
Trace.WriteLine("客户端连接成功");
return base.OnConnected();
} private string UserName
{
get
{
var userName = Context.RequestCookies["USERNAME"];
return userName == null ? "" : HttpUtility.UrlDecode(userName.Value);
}
}
}
}

GroupChat.cs

namespace ChatSignalR.HubSignalR
{ /*封装主动发送消息的单例
如果需要发送消息,直接控制器里面调用SendSystemMsg即可。
GroupChat.Instance.SendSystemMsg("消息");
*/ /// <summary>
/// 主动发送给用户消息,单例模式
/// </summary>
public class GroupChat
{
/// <summary>
/// Clients,用来主动发送消息
/// </summary>
private IHubConnectionContext<dynamic> Clients { get; set; } private readonly static GroupChat _instance = new GroupChat(GlobalHost.ConnectionManager.GetHubContext<ServerHub>().Clients); private GroupChat(IHubConnectionContext<dynamic> clients)
{
Clients = clients;
} public static GroupChat Instance
{
get
{
return _instance;
}
}
/// <summary>
/// 主动给所有人发送消息,系统直接调用
/// </summary>
/// <param name="msg"></param>
public void SendSystemMsg(string msg)
{
Clients.All.publshMsg(new { Name = "系统消息", Msg = msg, Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") });
}
}
}

一、ASP.NET Iframework_SignalR集线器类(v2)的更多相关文章

  1. ASP.NET导出EXCEL类

    最新ASP.NET导出EXCEL类 说明:可以导出ASP.NET页面和DATAGRID(WebControl)数据,可以导出表单头 using System;using System.Data;usi ...

  2. 扩展ASP.NET MVC HtmlHelper类

    在这篇帖子中我会使用一个示例演示扩展ASP.NET MVC HtmlHelper类,让它们可以在你的MVC视图中工作.这个示例中我会提供一个简单的方案生成Html表格. HtmlHelper类 Htm ...

  3. 【ASP.NET MVC系列】浅谈ASP.NET MVC八大类扩展(上篇)

    lASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操 ...

  4. ASP.NET -- WebForm -- ScriptManager 类

    ASP.NET -- WebForm -- ScriptManager 类 通过 ScriptManager 可注册随后将作为页面一部分呈现的脚本. 1. 注册并立即执行脚本. --RegisterS ...

  5. ASP.NET -- WebForm -- HttpRequest类的方法和属性

    ASP.NET -- WebForm --  HttpRequest类的方法和属性 1. HttpRequest类的方法(1) BinaryRead: 执行对当前输入流进行指定字节数的二进制读取. ( ...

  6. ASP.NET -- WebForm -- HttpResponse 类的方法和属性

    ASP.NET -- WebForm -- HttpResponse 类的方法和属性 1. HttpResponse 类的方法 (1) AddCacheDependency: 将一组缓存依赖项与响应关 ...

  7. 一、ASP.NET Iframework_SignalR永久连接类(v2)

    一.新建项目,选MVC项目默认 添加mvc文件夹和核心引用 二.添加SignaIR包 SignalR的准备:NuGet包管理器搜索:工具——>库程序包管理器——>Microsoft.Asp ...

  8. 史上最全面的SignalR系列教程-3、SignalR 实现推送功能-集线器类实现方式

    1.概述 通过前两篇 史上最全面的SignalR系列教程-1.认识SignalR 史上最全面的SignalR系列教程-2.SignalR 实现推送功能-永久连接类实现方式 文章对SignalR的介绍, ...

  9. Asp.net的request类

    ASP.NET获取客户端信息,暂时就这几个,有待添加~~ 1. 在ASP.NET中专用属性: 获取服务器电脑名:Page.Server.ManchineName 获取用户信息:Page.User 获取 ...

随机推荐

  1. 516D Drazil and Morning Exercise

    分析 求出直径和最远距离d 之后我们以直径中点为根 发现父亲的d肯定不小于儿子的d 于是从下往上启发式合并维护与子树根的值相差L内的个数即可 代码 #include<bits/stdc++.h& ...

  2. Eclipse高版本无法兼容FatJar的问题解决

    发现eclipse打包jar无法连带打包第三方lib,于是选择安装插件fatjar,现在说明fatjar安装过程: 1.安装方法:   1)下载安装:   https://sourceforge.ne ...

  3. Git - 对一组仓库进行配置

    对一组仓库使用一套配置,另一组仓库使用另一套配置的需求也是有的,比如公司仓库的配置和我个人项目的仓库配置并不完全相同,每次都修改单个仓库的配置太麻烦并且可能会粗心忘改了以错误的配置进行提交,如何对一个 ...

  4. 2018-5 - 热经 - 北京中地时空数码科技有限公司 - 研发工程师(WEBGIS 方向)

    一面: 登记,填写个人信息 笔试 选择题: HTML,CSS,JS 的选择题,都是基础题.其中有一道问哪个不是 document 的属性或方法,我在 bgColor 和 focus() 上面纠结了一下 ...

  5. 用JS实现快速排序

    "快速排序"的思想很简单,整个排序过程只需要三步: (1)在数据集之中,选择一个元素作为"基准"(pivot). (2)所有小于"基准"的元 ...

  6. webpack打包文件解析

    /** * 对于没有代码分割的,webpack会打包生成main.js一个大的自执行函数 * 函数参数是一个对象,键值分别是路径和模块的函数 * 函数内部定义了一些方法,包括__webpack_req ...

  7. windows,oracle,dg报错:ORA-12528,ORA-12154,ORA-10456 ,PING[ARC1]: Heartbeat failed to connect to standby 'orclbk'. Error is 12154

    windows,oracle,dg报错:ORA-12528,ORA-12154,ORA-10456 最近有需求在windows的2台oracle服务器上搭建dg,在过程中遇到了一些错误,跟在linux ...

  8. mysql安装及相关配置

    安装下载 第一种 安装mysql安装包 //www.jb51.net/softs/451120.html 保存root密码 打开系统偏好设置,start mysql server #配置mysql e ...

  9. 机器学习实战-Logistics回归

    Logistics回归:实战,有两个特征X0,X1.100个样本,进行Logistics回归 1.导入数据 def load_data_set(): """ 加载数据集 ...

  10. Java——HashMap源码解析

    以下针对JDK 1.8版本中的HashMap进行分析. 概述     哈希表基于Map接口的实现.此实现提供了所有可选的映射操作,并且允许键为null,值也为null.HashMap 除了不支持同步操 ...