页面刷新后连接后 SignalR 立即断开连接

时间:2021-01-22 04:48:09

标签: c# asp.net-core-signalr

我有一个 SignalR 集线器,当用户到达页面或用户离开页面时,它可以正常工作。问题在于,当用户刷新页面时,在 OnConnectedAsync 方法之后调用 OnDisconnectedAsync 方法。有意义吗?

这是一个问题,因为当用户连接时我更新数据库表,当他们断开连接时我更新同一个表。如果最后发生的事情是断开连接,那么它给人的印象是用户已经离开了页面,而当他们刷新页面时情况并非如此。顺便说一句,这个刷新问题发生在 Azure 中。在我的本地机器上没有发生这个问题。

以下是我的测试中心页面中的相关方法:

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);
            // ----------------------------------------
        }

这是我查看页面中的关键 Javascript 代码:

    <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>

提前致谢。

0 个答案:

没有答案
相关问题