gpt4 book ai didi

asp.net-core - SignalR - 调用组的成员不起作用

转载 作者:行者123 更新时间:2023-12-03 17:49:53 26 4
gpt4 key购买 nike

语境

我正在将 SignalR 3 RC1 与 ASP.NET 5 用于我的项目,但我无法让我的客户端订阅特定组,以从我的服务器接收消息。

集线器类

[HubName("ChatHub")]
public class ChatHub : Hub
{
public async Task Join(string userId)
{
if (!string.IsNullOrEmpty(userId))
{
await Groups.Add(Context.ConnectionId, userId);
}
}
}

JS代码
var chatHub = hubConnection.createHubProxy("chatHub");
chatHub .on("newMessage", (response) => {
console.log(response);
});

hubConnection.start().done(response => {
chatHub.invoke("join", "userid");
});

WebApi
public class ChatController : ApiController
{
protected readonly IHubContext ChatHub;

public ChatController(IConnectionManager signalRConnectionManager)
{
ChatHub = signalRConnectionManager.GetHubContext<ChatHub>();
}

[Authorize]
[HttpPost]
[Route("Message")]
public async Task<IActionResult> CreateMessage([FromBody] messageParams dto)
{
await ChatHub.Clients.Group("userid").NewMessage("hello world");
}
}

如果我广播“所有”客户端,它就可以工作。
await ChatHub.Clients.All.NewMessage("hello world");

是否有特定的配置来向特定组广播消息?

最佳答案

对于有兴趣在 Signalr 2.2 中使用 ASP.NET 5 的人,我在 IAppBuilder 和 IApplicationBuilder 之间创建了一座桥梁

internal static class IApplicationBuilderExtensions
{
public static void UseOwin(
this IApplicationBuilder app,
Action<IAppBuilder> owinConfiguration)
{
app.UseOwin(
addToPipeline =>
{
addToPipeline(
next =>
{
var builder = new AppBuilder();

owinConfiguration(builder);

builder.Run(ctx => next(ctx.Environment));

Func<IDictionary<string, object>, Task> appFunc =
(Func<IDictionary<string, object>, Task>)
builder.Build(typeof(Func<IDictionary<string, object>, Task>));

return appFunc;
});
});
}
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseOwin(owin => owin.MapSignalR());
}

导入这些依赖
"Microsoft.AspNet.Owin": "1.0.0-rc1-final",
"Microsoft.Owin": "3.0.1"

关于asp.net-core - SignalR - 调用组的成员不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35158835/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com