2016-02-02 35 views
3

语境SignalR - 一组调用成员不工作

我使用SignalR 3 RC1与ASP.NET 5为我的项目,我有麻烦我的客户预订了特定小组,从我的服务器接收消息。

HUB类

[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"); 

是否有将消息广播到特定组的特定配置?

+1

我降级到signalR 2.2和它的作品。 –

回答

1

对于有意使用ASP.NET 5 Signalr 2.2人,我创建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" 
+0

与此相关:http://stackoverflow.com/questions/32890084/using-signalr-2-in-asp-net-5-application –