locked
Connection Lifetime Events Not Triggered RRS feed

  • Question

  • User911861663 posted
    $(function ()
    {
        var FirePointHub = $.connection.FirePointHub;
        FirePointHub.client.SendByGroupAsFirePoint = function (data)
        {
            firePointMessageBox(data);
        };
    
        $.connection.hub.stateChanged(function ()
        {
            console.log('stateChanged!')
        });
    
        $.connection.hub.start()
            .done(function ()
            {
                console.log('Now connected, connection ID=' + $.connection.hub.id);
                console.log("Connected, transport = " + $.connection.hub.transport.name);
            })
            .fail(function () { console.log('Could not Connect!'); });
    });
        [HubName("FirePointHub")]
        public class FirePointHub : Hub
        {
           
    
            public override Task OnConnected()
            {
               return base.OnConnected();
            }
    
           
            public override Task OnDisconnected(bool stopCalled)
            {
                return base.OnDisconnected(stopCalled);
            }
    
            public override Task OnReconnected()
            {
                return base.OnReconnected();
            }
    
            public async Task AddGroup()
            {
    
                //await Groups.Add(Context.ConnectionId, groupName);
    
            }
    
            [HubMethodName("sendFirePoint")]
            public async static Task SendAllClientsAsFirePoint(TFireDistinguish message)
            {
                IHubContext context = GlobalHost.ConnectionManager.GetHubContext<FirePointHub>();
                await context.Clients.All.sendFirePoint(message);
            }
    
            [HubMethodName("SendByGroupAsFirePoint")]
            public async static Task SendByGroupsAsFirePoint(List<string> groups, TFireDistinguish point)
            {
                IHubContext context = GlobalHost.ConnectionManager.GetHubContext<FirePointHub>();
                await context.Clients.Groups(groups).SendByGroupAsFirePoint(point);
            }    
    }

    OnConnected event is triggered only once when the project starts,How can I trigger this event each time a new connection is established?

    OnDisconnected(bool stopCalled)、OnReconnected() :Neither of these events has been triggered..

    Version:2.4.0.0

    thank you for helping me!

    Wednesday, January 9, 2019 9:32 AM

All replies

  • User1724605321 posted

    Hi overrideeee ,

    The OnConnected  events occurs when the connection connects to this hub instance . Please refer to document :https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server#onreconnected :

    Each time a browser navigates to a new page, a new connection has to be established, which means SignalR will execute the OnDisconnected method followed by the OnConnected method. SignalR always creates a new connection ID when a new connection is established.

    The OnReconnected method is called when there has been a temporary break in connectivity that SignalR can automatically recover from, such as when a cable is temporarily disconnected and reconnected before the connection times out.

    Please debug your application and follow the tutorial to confirm the behavior .

    Best Regards,

    Nan Yu 

    Thursday, January 10, 2019 5:42 AM
  • User911861663 posted

    hi Nan Yu,

    So why doesn't the OnConnected event fire when I use a new tab or another browser?

    The OnConnected event is triggered only when the first connection is made after the project is started. The log and message push are normal.

    thinks!

    by translate.google.cn

    Thursday, January 10, 2019 8:27 AM
  • User1724605321 posted

    Hi overrideeee ,

    So why doesn't the OnConnected event fire when I use a new tab or another browser?

    That shouldn't happen . Please try to debug and trace your application :

    https://www.asp.net/signalr/overview/testing-and-debugging 

    Best Regards,

    Nan Yu

    Tuesday, January 29, 2019 7:02 AM