前言:
如今各位老铁们对“aspnetsignalr系列”大概比较关怀,姐妹们都想要知道一些“aspnetsignalr系列”的相关文章。那么小编同时在网摘上汇集了一些对于“aspnetsignalr系列””的相关内容,希望大家能喜欢,兄弟们一起来学习一下吧!一、环境
.net framework 4.5以上、window、docker、nginx、linux
二、开发工具及前提
开发工具:microsoft visual studio 2017
前提:具备microsoft visual studio工具进行C#软件开发
三、需要引用的关键库
用NuGet管理引用库
如果宿主是IIS,则还需引入:
四、实践
1、新建空白WebApplication
2、服务端
添加Owin Startup类
Startup代码:
using System;
using System.Threading.Tasks;
using HSMY.Web.General.Config;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(HSMY.Message.Startup))]
namespace HSMY.Message
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
//redis服务连接字符串,redis暂不支持 redis cluster 模式,切记
//string redisConf = ConfigHelper.GetConfig("redisconf");//读配置
string redisConf = "192.168.2.111:7080,password=redis@test";
if (redisConf?.Length > 0)
{
RedisScaleoutConfiguration configuration = new
RedisScaleoutConfiguration(redisConf, "signalr");
GlobalHost.DependencyResolver.UseRedis(configuration);
}
//默认为pipeline为signalr
app.MapSignalR(new HubConfiguration() { EnableJSONP = true });
//如果希望启用其他pipeline,则可以使用以下代码:
/*app.Map("/signalrtest", map =>
{
var hubConfiguration = new HubConfiguration
{
EnableJSONP = true
};
map.RunSignalR(hubConfiguration);
}); */
}
}
}
创建客户端回调接口,便于调用客户端方法时,显性显示方法名(也可以不创建,不过回调
客户端方法都是dynamic类型):
namespace SignalRServer
{
public interface IChatClient
{
void Display(string msg);
}
}
创建Hub:
using Microsoft.AspNet.SignalR;
using System;
namespace SignalRServer
{
public class ChatHub : Hub<IChatClient>
{
public void Chat(string msg)
{
//这里如果不用IChatClient, Clients.All返回的就是dynamic类型
Clients.All.Display($"receive msg:{msg}");
}
}
}
如果基于Owin宿主启动(非IIS),则增加以下宿主代码:
using Host;
using Microsoft.Owin.Hosting;
using System;
namespace SignalRServer
{
class Program
{
static void Main(string[] args)
{
//启动服务
string url = args.Length == 0 ? "; : args[0];
using (WebApp.Start<Startup>(url))
{
Console.WriteLine($"Server running at {url}");
Console.ReadLine();
}
}
}
}
3、客户端代码
创建控制台应用:SignalRClient,代码如下:
using Microsoft.AspNet.SignalR.Client;
using System;
using System.Threading.Tasks;
namespace SignalRClient
{
class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{ //接收信息
Receive(args[0]);
}
else //发送信息
Send();
}
static void Send()
{
string url = ";;
var hubConn = new HubConnection(url);
Console.WriteLine($"connect server: {url}");
var proxy = hubConn.CreateHubProxy("ChatHub");
hubConn.Start().Wait();
//客户端模拟1秒发送一次消息
while (true)
{
var guid = DateTime.Now.ToString() + " " + Guid.NewGuid().ToString();
Console.WriteLine("Send Msg:" + guid);
//调用服务端方法:Chat
proxy.Invoke("Chat", guid).Wait();
Task.Delay(1000).Wait();
}
}
static void Receive(string url)
{
var hubConn = new HubConnection(url);
var proxy = hubConn.CreateHubProxy("ChatHub");
Console.WriteLine($"connect server: {url}");
//订阅服务端方法:Display
proxy.On<string>("Display", (msg) => { Console.WriteLine(msg); });
hubConn.Start().ContinueWith(t =>
{
if (t.IsFaulted)
{
Console.WriteLine("Connection fault.");
}
}).Wait();
Console.ReadLine();
}
}
}
4、示例:居于Redis实现SignalR不同服务之间实现服务共享,以Owin宿主启动(非IIS)
服务端:
命令行:SignalRServer.exe
:
命令行:SignalRServer.exe
客服端:
客户端1(连接服务 )
命令行:SignalRClient.exe
客户端2(连接服务 )
命令行:SignalRClient.exe
5、SignalR负载均衡部署(基于nginx+iis)
a、先将SignalR在IIS上发布
b、nginx配置
在nginx.conf配置文件中的http模块配置:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
在default.conf中增加:
upstream websocket {
ip_hash;
server 172.19.111.154:9000 weight=1 max_fails=3 fail_timeout=30;
server 172.19.111.154:9001 weight=1 max_fails=3 fail_timeout=30;
server 172.19.111.152:9000 weight=1 max_fails=3 fail_timeout=30;
server 172.19.111.152:9001 weight=1 max_fails=3 fail_timeout=30;
}
server {
listen 80;
listen 443 ssl http2;
ssl_certificate /etc/nginx/cert/signalr.test.com.crt;
ssl_certificate_key /etc/nginx/cert/signalr.test.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_session_timeout 10m;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_buffer_size 1400;
add_header Strict-Transport-Security max-age=15768000;
ssl_stapling on;
ssl_stapling_verify on;
server_name signalr.test.com;
location /signalr {
proxy_pass ;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_store off;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_send_timeout 500s;
proxy_read_timeout 500s;
}
}
c、重启nginx
到此就可以通过: 或
访问 signalr 服务
nginx如何配置,可以查看我的另外一篇文章:
做服务的负载均衡,你也可以(docker 中部署 nginx)