signalr core linux,SignalR如何搭建在Linux中以Nginx負載的服務端(C#)

SignalR搭建在Linux中以Nginx服務與IISExpress和SelfHost相比較來說,我碰到最大的麻煩是如何更改配置文件和搭建環境,下面將教大家如何搭建Nginx服務。

1、在Linux安裝環境

安裝mono,需要下載最新版本Mono編譯器,下載地址如下

–命令行輸入 mono -V 查看mono版本,查看是否安裝成功

2、安裝Nginx,下載地址如下

–sudo service nginx start 開啟Nginx服務,查看是否安裝成功

3、安裝成功后,需要更改Nginx配置文件

–gedit /etc/nginx/nginx.conf 打開配置文件 加上如下代碼

在http{}中加上如下代碼

map $http_upgrade $connection_upgrade{

default upgrade;

‘’ close;

}

server{

listen 8020; //客戶端用的端口號

server_name 192.168.254.135; //這里為Linux中IP地址

location / {

proxy_pass http://192.168.254.135:9999; //服務端url

proxy_http_version 1.1;

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection $connection_upgrade;

}

}

加完代碼后需重新運行nginx服務 ,查看nginx服務是否能夠成功啟動

sudo service nginx restart

2、搭建SignalR服務端

代碼如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.AspNet.SignalR;

using Owin;

using Microsoft.Owin.Cors;

namespace Signalr_SelfHost

{

class Program

{

static void Main(string[] args)

{

var uri = "http://127.0.0.1:9999";

using (Microsoft.Owin.Hosting.WebApp.Start(uri))

{

Console.WriteLine(string.Format("Server stared on {0}", uri));

Console.ReadLine();

}

}

}

class Startup

{

static Hub hub;

public void Configuration(IAppBuilder app)

{

app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

var configuration = new HubConfiguration();

configuration.EnableDetailedErrors = true;

app.MapSignalR("/signalr", configuration);

hub = new MyHub();

}

}

public class MyHub : Hub

{

public override Task OnConnected()

{

Console.WriteLine("有客戶端連接");

return base.OnConnected();

}

public void Send(string message)

{

string name = "BG";

Clients.All.SendMessage(name,message);

}

}

}

注:這里以SelfHost服務為例,以下為如何在Linux運行.sln項目

msbuild xxx.sln —生成Debug

msbuild xxx.sln /p:Configuration=Release —生成Release

在生成的Debug或Release文件里,運行.exe文件即可。

mono xxx.exe

3、搭建SignalR客戶端

注:客戶端可以在window下運行,也可以在Linux中用mono運行(mono支持運行Winform客戶端),本文介紹客戶端在window下運行

客戶端代碼如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.AspNet.SignalR.Client;

namespace ClientsConsole

{

class Program

{

static void Main(string[] args)

{

string url = "http:/192.168.254.135:9999";

HubConnection _conn = new HubConnection(url,true);

IHubProxy _proxy = _conn.CreateHubProxy("MyHub");

_conn.Start();

_proxy.On("sendMessage", (n, s) =>

{

});

_conn.StateChanged += new Action(tgt =>

{

if (((StateChange)tgt).NewState == Microsoft.AspNet.SignalR.Client.ConnectionState.Connected)

{

_proxy.Invoke("send", "Hello");

}

});

Console.ReadLine();

}

}

}

總結:這邊搭建SignalR3種不同服務(IISExpress、Owin SlefHost、Linux Nginx),客戶端(Console控制台、Winform引用),

目的是為了測試哪一種服務端和客戶端搭配傳輸效率更快、並發性更好。

測試過程:N個客戶端同時連接並且發送數據給所有客戶端的效率。

那么在我自己這邊測試結果為SignalR在Linux搭載Nginx服務效果最佳。大家可以去測試一下!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值