0
votes

I have problem with calling function on signalr hub disconnect.

here are my js references

<script src="~/js/jquery.min.js"></script>

<script src="~/SignalR/dist/browser/signalr.min.js"></script>

script inside cshtml file

 var connection = new signalR.HubConnectionBuilder()
.withUrl("/chatt", {
    accessTokenFactory: () => "testing"
})
.build();


connection.start().then(function () {
connection.invoke('GetConnectionId')
.then(function (connectionId) {
   //alert(connectionId);
   //removeConnectionID(connectionId);
})

}).catch(err => console.error(err));

 
$.connection.hub.disconnected(function () {
    removeConnectionID();
})

async function removeConnectionID() {
    await  $.get("/Chat/Test") ;
    }

on $.connection.hub.disconnected i get Uncaught TypeError: Cannot read property 'hub' of undefined. When i start typing $.connection. i dont see hub property at all.

here is my configure services

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<mojDbContext>(options=> options.UseSqlServer(Configuration.GetConnectionString("IB150198T")));
        services.AddControllersWithViews();

        services.AddMvc();
        services.AddDistributedMemoryCache();
        services.AddSession();
        
        services.AddSignalR();
       // services.AddSingleton<IUserIdProvider, UserProvider>();
    }

I saw somewhere that I should have reference to js file that is named something like "/SignalR/Hubs" but cant find it in client side library for signalR

any help is appreciated!!!

1

1 Answers

0
votes

You're confusing ASP.NET SignalR with ASP.NET Core SignalR.

Your code is using ASP.NET Core SignalR until you try to access $.connection.hub which was the pattern in ASP.NET SignalR.

You can look at the tutorial for ASP.NET Core SignalR here https://docs.microsoft.com/aspnet/core/tutorials/signalr?view=aspnetcore-5.0&tabs=visual-studio