Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

c# - SignalR disconnects right after connecting after page refresh

I have a SignalR hub that works fine when a user arrives on the page or when a user leaves the page. The issue is that when a user refreshes the page the OnDisconnectedAsync method is being called AFTER the OnConnectedAsync method. Does that make sense?

This is an issue because when the user connects I update a database table and when they disconnect I update that same table. If the last thing that happened was a disconnect then it gives the impression that the user has left the page, which is not really the case when they refresh the page. Btw, this refresh issue happens in Azure. On my local machine the issue does not happen.

Here are the relevant methods from my test hub page:

public override async Task OnConnectedAsync()
        {
            //-------------------------------------------------------------
            // Get user attributes from ClaimsHelper
            UserAttributes userAttObj = ClaimsHelper.GetClaimValues(_httpContextAccessor);
            if (userAttObj != null)
            {
                // Will use loginid for tracking user
                loginid = userAttObj.userid;
                loggedinuser = userAttObj.loggedin;
                sessionNum = userAttObj.SessionNumber;
            }
            //-------------------------------------------------------------

            string conid = Context.ConnectionId;
            string ContestGroupName = "ContestGroup0";

            await Groups.AddToGroupAsync(conid, ContestGroupName);

            string msg = "WELCOME <span class='txtMedGreen'>" + loggedinuser + "</span> (" + conid + "). Time: " + DateTime.Now.ToString();

            await Clients.Group(ContestGroupName).SendAsync("UpdateInfo", msg);
            await base.OnConnectedAsync();
            // ----------------------------------------
        }

        public override async Task OnDisconnectedAsync(Exception exception)
        {
            //-------------------------------------------------------------
            // Get user attributes from ClaimsHelper
            UserAttributes userAttObj = ClaimsHelper.GetClaimValues(_httpContextAccessor);
            if (userAttObj != null)
            {
                // Will use loginid for tracking user
                loginid = userAttObj.userid;
                loggedinuser = userAttObj.loggedin;
                sessionNum = userAttObj.SessionNumber;
            }
            //-------------------------------------------------------------
            string conid = Context.ConnectionId;
            //-------------------------------------------------------------
            string ContestGroupName = "ContestGroup0";
            string msg = "<span class='txtMedGreen'>" + loggedinuser + "</span> (" + conid + ") has LEFT the group. Time: " + DateTime.Now.ToString();

            await Clients.Group(ContestGroupName).SendAsync("UpdateInfo", msg);
            await base.OnDisconnectedAsync(exception);
            // ----------------------------------------
        }

Here is the key Javascript code in my View page:

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

    <script type="text/javascript">

        // For showing information via SignalR.
        var huburl = "/testhub?cid=0";
        var connection = new signalR.HubConnectionBuilder().withUrl(huburl).withAutomaticReconnect().build();

        $(document).ready(function () {

            // ---- SignalR stuff ---------------------------------------------
            connection.on("UpdateInfo", function (msg) {
                var strmsg = "<div class='inforow'>" + msg + "</div>";
                // alert("strmsg is: " + strmsg);
                $("#siginfo").append(strmsg);
            });

            connection.on("SendGroupMsg", function (msg, fromuser) {
                var strmsg = "<div class='msgrow'>" + fromuser + ": " + msg + "</div>";
                // alert("strmsg is: " + strmsg);
                $("#msgarea").append(strmsg);
            });

            // This initiates the connection and makes connection info available.
            connection.start().catch(function (err) {
                alert("Error starting connection: " + err.toString());
                return console.error(err.toString());
            });

        });

    </script>

Thanks in advance.

question from:https://stackoverflow.com/questions/65839460/signalr-disconnects-right-after-connecting-after-page-refresh

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...