有没有一种方法可以连接到ASP中内置的SignalR Hub. NET从其他应用程序? [英] Is there a way to connect to SignalR Hub built in ASP. NET from other application?
问题描述
为了实现实时在线聊天,我已经有一些使用ASP .NET MVC和SignalR的Web应用程序.
我有SignalR ChatHub : Hub
并从客户端连接到此集线器,如下所示:
let hub = $.connection.chatHub
并且运行良好.
如果我想在桌面应用程序中使用该中心怎么办?这甚至可能吗,这是一个好习惯吗?如果是,我该如何连接?有任何想法吗?谢谢!
For implmenting a real-time online chat, I already have some web application using ASP .NET MVC and SignalR.
I have SignalR ChatHub : Hub
and connect to this hub from the client side like following:
let hub = $.connection.chatHub
and it's working well.
And what if I want to use this hub in desktop application? Is this even possible and is this a good practice? If yes, how do I connect to it? Any Ideas? Thanks!
推荐答案
存在一个.NET SignalR客户端,该客户端可用于允许桌面应用程序连接到SignalR集线器.可从NuGet获得.
A .NET SignalR client exists that can be used to allow a desktop application to connect to a SignalR hub. It is available from NuGet.
配置客户端以与您的Web客户端相同的方式连接到集线器,并且无论平台如何,所有连接的用户之间都将共享消息.
Configure the client to connect to the hub in the same way your web client does and messages will be shared between all connected users regardless of platform.
The SignalR hub and client must be the same version (see here: Connect to SignalR server in dotnet core from .NET client).
.NET 4.5 +
对于.NET 4.5+版本(不包括.NET Core),您需要较旧的客户端:Microsoft.AspNet.SignalR.Client
For .NET versions 4.5+ (not including .NET Core) you require the older client: Microsoft.AspNet.SignalR.Client
可在此处找到文档和代码示例: https://docs.microsoft.com/zh-cn/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-net-client
Documentation and code samples are available here: https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-net-client
ASP.NET Core
如果您的MVC应用程序使用的是ASP.NET Core,请改用以下程序包:Microsoft.AspNetCore.SignalR.Client
If your MVC application is using ASP.NET Core then use this package instead: Microsoft.AspNetCore.SignalR.Client
您可以在此处找到文档: https://docs.microsoft.com/zh-cn/aspnet/core/signalr/dotnet-client?view=aspnetcore-2.2
You can find documentation here: https://docs.microsoft.com/en-us/aspnet/core/signalr/dotnet-client?view=aspnetcore-2.2
示例-从桌面应用程序客户端调用服务器方法
以下来自.NET 4.5+文档:
The following is from the .NET 4.5+ documentation:
// Connect
var hubConnection = new HubConnection("http://www.contoso.com/");
IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("stockTicker");
await hubConnection.Start();
// Call server method "JoinGroup" from client
stockTickerHubProxy.Invoke("JoinGroup", "SignalRChatRoom");
这篇关于有没有一种方法可以连接到ASP中内置的SignalR Hub. NET从其他应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!