0

I have an application(asp.net c#) with 5 projects currently. I want to add chat to my application so I use signalr technology. How can I configure the client-server in my application?. Because I can't understand to configure the hub connection in my project, the hub is created in another project of my application. In the Microsoft documentation, the hub is created in the same application so it configures like simply var chatHub = $.connection.chatHub;. but in my case, the hub is another project. also what is mean by this <script src="signalr/hubs"></script> script tag.
application projects ,project review

Shalu Shaji
  • 68
  • 1
  • 8

1 Answers1

0

Add nuget library Microsoft.AspNet.SignalR

public class Startup
{
    public void Configuration(IAppBuilder appBuilder)
    {
        httpConfiguration.EnableCors(new EnableCorsAttribute("*", "*","*"));
        appBuilder.UseCors(CorsOptions.AllowAll);
        GlobalHost.DependencyResolver.Register(typeof(NotificationHub), 
        IocInitializer.Container.GetInstance<NotificationHub>);
        appBuilder.Map(
            "/signalr",
            map =>
            {
                var hubConfiguration = new HubConfiguration { 
               EnableDetailedErrors = true };
                map.RunSignalR(hubConfiguration);
            });
        appBuilder.MapSignalR();

}

public class NotificationHub : Hub
{
    private static IHubContext hubContext = 
    GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
    public IEcapBus EcapBus { get; set; }
    public ISecurityDataProvider SecurityDataProvider { get; set; }
    public NotificationHub()
    {
        this.SecurityDataProvider = securityDataProvider;
        this.EcapBus = ecapBus;
    }



    public void Hello()
    {
        Clients.All.hello();
    }
}

client code add signalr jquery library

    var connection = $.hubConnection(baseUrl);
    connection.qs = "Authorization=" + token;
    var chatHubProxy = connection.createHubProxy('NotificationHub');
    var actionName = ['Notify'];
    chatHubProxy.on(actionName[0], function (message) {
        console.log("I got message");
        console.log(message);
        onReceiveMessage(message);
    });

    connection.start().done(function () {
        console.log("connected");
    }).fail(function (a) {
        console.log('not connected' + a);
        });

Now just call NotifierHub.Hello() from any where in server side

  • I cant understand the code !. Can you please explain to me this? my application looks like this [link](https://drive.google.com/open?id=1aMrAN8-HI-SU7XI79QBg9dymGxkyJPHL). I want to call from chat.aspx(project2) to chathub.cs(project1). – Shalu Shaji Mar 22 '18 at 11:55
  • can you share your email id for contacting you? – Shalu Shaji Mar 22 '18 at 11:57
  • Can you just tell me, how my baseUrl look like?. [Project view](https://drive.google.com/open?id=1aMrAN8-HI-SU7XI79QBg9dymGxkyJPHL) and also tell me about **connection.qs = "Authorization=" + token** – Shalu Shaji Mar 23 '18 at 05:30
  • var baseUrl = "http://google.com" and you can pass authorization token with this following way – Md Shahi Dullah Mar 26 '18 at 05:43