一、前言

上次讲SignalR还是在《在ASP.NET Core下使用SignalR技术》文章中提到,ASP.NET Core 1.x.x 版本发布中并没有包含SignalR技术和开发计划中。时间过得很快,MS已经发布了.NET Core 2.0 Preview 2 预览版,距离正式版已经不远了,上文中也提到过在ASP.NET Core 2.0中的SignalR将做为重要的组件与MVC等框架一起发布。它的开发团队也兑现了承诺,使用TypeScript对它的javascript客户端进行重写,服务端方面也会贴近ASP.NET Core的开发方式,比如会集成到ASP.NET Core依赖注入框架中。

二、环境搭建

要在ASP.NET Core 2.0中使用SignalR,要先引用Microsoft.AspNetCore.SignalR 、 Microsoft.AspNetCore.SignalR.Http 两个Package包。

目前ASP.NET Core 2.0与SignalR还都是Preview版本,所以NUGET上也找不到SignalR的程序包,想添加引用我们就得去MyGet上去找找。既然要用MyGet的话,就要为项目添加NuGet源了。

1.添加NuGet源

在程序根目录新建一个命为NuGet.Config的文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear/>
<add key="aspnetcidev" value="https://dotnet.myget.org/F/aspnetcore-ci-dev/api/v3/index.json"/>
<add key="api.nuget.org" value="https://api.nuget.org/v3/index.json"/>
</packageSources>
</configuration>

2.编辑项目文件csproj

添加上面提到的两个包的引用:

    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0-preview3-26040" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.0.0-preview3-26037" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Http" Version="1.0.0-preview3-26037" />

我在这个示例里使用的是目前的最高,当然版本号每天都有可能发生变化,最新版本的SignalR,是不兼容.NET Core SDK 2.0 Preview 1中默认创建项目时Microsoft.AspNetCore.All这个包的版本的,这里也修改修改一下版本号为:Microsoft.AspNetCore.All 2.0.0-preview3-26040。

当然也可以用dotnet cli 来添加包引用:

dotnet add package Microsoft.AspNetCore.SignalR --version 1.0.0-preview3-26037 --source https://dotnet.myget.org/F/aspnetcore-dev/api/v3/index.json

dotnet add package Microsoft.AspNetCore.SignalR.Http --version 1.0.0-preview3-26037 --source https://dotnet.myget.org/F/aspnetcore-dev/api/v3/index.json

3.添加配置代码

我们需要在Startup类中的 ConfigureServices方法中添加如下代码:

public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR();
}

在Startup类中的Configure方法中添加如下代码:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
app.UseSignalR(routes =>
{
routes.MapHub<Chat>("hubs");
});
}

4.添加一个HUB类

public class Chat : Hub
{
public override async Task OnConnectedAsync()
{
await Clients.All.InvokeAsync("Send", $"{Context.ConnectionId} joined");
} public override async Task OnDisconnectedAsync(Exception ex)
{
await Clients.All.InvokeAsync("Send", $"{Context.ConnectionId} left");
} public Task Send(string message)
{
return Clients.All.InvokeAsync("Send", $"{Context.ConnectionId}: {message}");
} public Task SendToGroup(string groupName, string message)
{
return Clients.Group(groupName).InvokeAsync("Send", $"{Context.ConnectionId}@{groupName}: {message}");
} public async Task JoinGroup(string groupName)
{
await Groups.AddAsync(Context.ConnectionId, groupName); await Clients.Group(groupName).InvokeAsync("Send", $"{Context.ConnectionId} joined {groupName}");
} public async Task LeaveGroup(string groupName)
{
await Groups.RemoveAsync(Context.ConnectionId, groupName); await Clients.Group(groupName).InvokeAsync("Send", $"{Context.ConnectionId} left {groupName}");
} public Task Echo(string message)
{
return Clients.Client(Context.ConnectionId).InvokeAsync("Send", $"{Context.ConnectionId}: {message}");
}
}

5.客户端支持

  在wwwroot目录下创建一个名为chat.html的Html静态文件,内容如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<h1 id="head1"></h1>
<div>
<select id="formatType">
<option value="json">json</option>
<option value="line">line</option>
</select> <input type="button" id="connect" value="Connect" />
<input type="button" id="disconnect" value="Disconnect" />
</div> <h4>To Everybody</h4>
<form class="form-inline">
<div class="input-append">
<input type="text" id="message-text" placeholder="Type a message, name or group" />
<input type="button" id="broadcast" class="btn" value="Broadcast" />
<input type="button" id="broadcast-exceptme" class="btn" value="Broadcast (All Except Me)" />
<input type="button" id="join" class="btn" value="Enter Name" />
<input type="button" id="join-group" class="btn" value="Join Group" />
<input type="button" id="leave-group" class="btn" value="Leave Group" />
</div>
</form> <h4>To Me</h4>
<form class="form-inline">
<div class="input-append">
<input type="text" id="me-message-text" placeholder="Type a message" />
<input type="button" id="send" class="btn" value="Send to me" />
</div>
</form> <h4>Private Message</h4>
<form class="form-inline">
<div class="input-prepend input-append">
<input type="text" name="private-message" id="private-message-text" placeholder="Type a message" />
<input type="text" name="user" id="target" placeholder="Type a user or group name" /> <input type="button" id="privatemsg" class="btn" value="Send to user" />
<input type="button" id="groupmsg" class="btn" value="Send to group" />
</div>
</form> <ul id="message-list"></ul>
</body>
</html>
<script src="signalr-client.js"></script>
<script src="utils.js"></script>
<script>
var isConnected = false;
function invoke(connection, method, ...args) {
if (!isConnected) {
return;
}
var argsArray = Array.prototype.slice.call(arguments);
connection.invoke.apply(connection, argsArray.slice(1))
.then(result => {
console.log("invocation completed successfully: " + (result === null ? '(null)' : result)); if (result) {
addLine('message-list', result);
}
})
.catch(err => {
addLine('message-list', err, 'red');
});
} function getText(id) {
return document.getElementById(id).value;
} let transportType = signalR.TransportType[getParameterByName('transport')] || signalR.TransportType.WebSockets; document.getElementById('head1').innerHTML = signalR.TransportType[transportType]; let connectButton = document.getElementById('connect');
let disconnectButton = document.getElementById('disconnect');
disconnectButton.disabled = true;
var connection; click('connect', event => {
connectButton.disabled = true;
disconnectButton.disabled = false;
let http = new signalR.HttpConnection(`http://${document.location.host}/hubs`, { transport: transportType });
connection = new signalR.HubConnection(http);
connection.on('Send', msg => {
addLine('message-list', msg);
}); connection.onClosed = e => {
if (e) {
addLine('message-list', 'Connection closed with error: ' + e, 'red');
}
else {
addLine('message-list', 'Disconnected', 'green');
}
} connection.start()
.then(() => {
isConnected = true;
addLine('message-list', 'Connected successfully', 'green');
})
.catch(err => {
addLine('message-list', err, 'red');
});
}); click('disconnect', event => {
connectButton.disabled = false;
disconnectButton.disabled = true;
connection.stop()
.then(() => {
isConnected = false;
});
}); click('broadcast', event => {
let data = getText('message-text');
invoke(connection, 'Send', data);
}); click('join-group', event => {
let groupName = getText('message-text');
invoke(connection, 'JoinGroup', groupName);
}); click('leave-group', event => {
let groupName = getText('message-text');
invoke(connection, 'LeaveGroup', groupName);
}); click('groupmsg', event => {
let groupName = getText('target');
let message = getText('private-message-text');
invoke(connection, 'SendToGroup', groupName, message);
}); click('send', event => {
let data = getText('me-message-text');
invoke(connection, 'Echo', data);
}); </script>

值得注意的是,你可能会发现,目前找不到signalr-client.js这个文件,它是怎么来的呢,有两种方式:

第1种是通过下载SignalR的源代码,找到Client-TS项目,对TypeScript进行编译可以得到。

第2种比较简单通过Npm可以在线获取:

npm install signalr-client --registry https://dotnet.myget.org/f/aspnetcore-ci-dev/npm/

三、最后

  附上一个可用的Demo:https://github.com/maxzhang1985/AspNetCore.SignalRDemo

  GitHub:https://github.com/maxzhang1985/YOYOFx 如果觉还可以请Star下, 欢迎一起交流。

  .NET Core 开源学习群:214741894

在ASP.NET CORE 2.0使用SignalR技术的更多相关文章

  1. asp.net core 2.0集成signalr

    在博客园也很多年了,一直未曾分享过什么东西,也没有写过博客,但自己也是汲取着博客园的知识成长的: 这两天想着不能这么无私,最近.NET CORE貌似挺流行的,闲来无事也自己搞了个asp.net cor ...

  2. ASP.NET CORE 2.0 Uses SignalR Technology

    https://www.codeproject.com/Articles/1208322/ASP-NET-CORE-Uses-SignalR-Technology

  3. ASP.NET Core 2.0 SignalR 示例

    # 一.前言 上次讲SignalR还是在<[在ASP.NET Core下使用SignalR技术](http://dotnet.ren/2017/02/21/%E5%9C%A8ASP-NET-Co ...

  4. asp.net core 2.0 webapi集成signalr

    asp.net core 2.0 webapi集成signalr   在博客园也很多年了,一直未曾分享过什么东西,也没有写过博客,但自己也是汲取着博客园的知识成长的: 这两天想着不能这么无私,最近.N ...

  5. 升级 ASP.NET Core 3.0 设置 JSON 返回 PascalCase 格式与 SignalR 问题

    由于一些 JS 组件要求 JSON 格式是 PascalCase 格式,新版本 ASP.NET Core 3.0 中默认移除了 Newtonsoft.Json ,使用了微软自己实现的 System.T ...

  6. ASP.NET 5 已死 - 隆重介绍 ASP.NET Core 1.0 和 .NET Core 1.0

    还没正式登场就死了?不能怪我标题党,是大神Scott在他博客上这么说的,我只是翻译了一下. 在1月20号最新的ASP.NET Community Standup视频中,微软aspnet开发组的大帅哥 ...

  7. ASP.NET 5已终结,迎来ASP.NET Core 1.0和.NET Core 1.0 转

    作者:yourber 命名是非常困难的事情,微软这次为了和ASP.NET4.6做区分,采用了全新的命名方式ASP.NET Core 1.0,它是一个全新的框架. ASP.NET 在过去的 15 年里是 ...

  8. [翻译] ASP.NET Core 3.0 的新增功能

    ASP.NET Core 3.0 的新增功能 全文翻译自微软官方文档英文版 What's new in ASP.NET Core 3.0 本文重点介绍了 ASP.NET Core 3.0 中最重要的更 ...

  9. 推荐:【视频教程】ASP.NET Core 3.0 入门

    墙裂推荐了,免费,通俗易懂,唯一可惜的就是不是我录的,更可惜的是人家录制完了快半年了我还没看完... 版权归原作者所有,建议新手还是边看边实践吧,要不然过完一遍发现自己啥也没学会,不要眼高手低 [视频 ...

随机推荐

  1. 类设计的SOLID原则

    SOLID原则是面向对象范式的核心 单一职责原则(Single Responsible Principle, SRP):对于一个类,应该仅有一个引起它变化的原因.其基础是内聚,表示类完成单一功能的程度 ...

  2. python求职Top10城市,来看看是否有你所在的城市

    前言 从智联招聘爬取相关信息后,我们关心的是如何对内容进行分析,获取用用的信息. 本次以上篇文章“5分钟掌握智联招聘网站爬取并保存到MongoDB数据库”中爬取的数据为基础,分析关键词为“python ...

  3. Plupload上传插件自定义图片的修改

    若自定义的一个上传图片效果,代码(可能不全),当用户再次点击所有或任意一个上传图片的input时,uploader.files已经多了客户再次上传的图片,但是你就想要最后的两张图片,这就可以使用到up ...

  4. Python正则表达式完全指南(上)

    正则表达式处理文本有如疾风扫秋叶,绝大部分编程语言都内置支持正则表达式,它应用在诸如表单验证.文本提取.替换等场景.爬虫系统更是离不开正则表达式,用好正则表达式往往能收到事半功倍的效果. 介绍正则表达 ...

  5. FTP服务器搭建及操作(一)

    FTP服务器搭建及操作(一) FTP搭建 PHP FTP操作 搭建方法参照(windows):http://www.cnblogs.com/lidan/archive/2012/06/04/25351 ...

  6. Detailed Information for Outputted Files from Somatic Mutation Annotators(annovar 注释文件条目详细解释)

    CONTENTS *_annoTable.txt (ANNOVAR) *_annoTable.txt (SnpEff) *_genelist.txt (ANNOVAR & SnpEff) db ...

  7. JVM-8.性能监控与故障处理工具

    一.JDK的命令行工具 1.jdk/bin下面有非常多工具:这些工具大多数是用Java实现(jdk/lib/tools.jar) 2.jps:类似于Linux中的ps,显示Hotspot虚拟机进程 3 ...

  8. 网络编程2之Socket简介和java.net包

    一.Socket 通信链路的端点就被称为"套接字"(英文名Socket) 是提供给应用程序的接口 图文说明Socket Socket通信原理 二.java.net包 Java.ne ...

  9. java设计模式之 装饰器模式

    装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构. 这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装 ...

  10. Java语言编程注意事项

    1.大小写敏感,要注意区分大小写: 2.一般每一句代码写完之后,后面以":"结尾: 3.在代码中,括号的出现一般都是成对的,如:{}.