1

那是我的代码:

<input type="button" id="SignOut" value="SignOut"  onclick="logout();" />

服务器端:

public void ondisconnected() // when the user is disconnected
{
    try
    { 
        DataRow[] UserRow = ConnectedClientDt.Select("ConnectionID='" + Context.ConnectionId + "'");
        int ClientID = Convert.ToInt32(UserRow[0][0]);
        new SQLHelper(SQLHelper.ConnectionStrings.WebSiteConnectionString).Update("Update clients set USER_STATUS='F' where CLIENT_ID=" + ClientID);
        string query = " Select FRIEND_ID from friends where CLIENT_ID= " + ClientID;
        DataTable FriendsDt = new SQLHelper(SQLHelper.ConnectionStrings.WebSiteConnectionString).getQueryResult(query);
        for (int i = 0; i < FriendsDt.Rows.Count; i++)
        {
            DataRow[] FrRow = ConnectedClientDt.Select("ClientId=" + FriendsDt.Rows[i][0] + "");
            if (FrRow.Length > 0)
                Clients.Client(FrRow[0][2].ToString()).userDisconnected(ClientID);
        }
        DeleteUser(Context.ConnectionId);  
    }
    catch (Exception ex)
    {
        return ;
    }
}

客户端:

function logout() {
    $.connection.chatHub.server.ondisconnected();
    setTimeout(window.location= "LoginPage.aspx?action=logout", 5000);
}

当我按下按钮时,ondisconnected 函数没有执行,但是当我删除 settimeout 行时,代码正常工作,没有任何问题:s

4

1 回答 1

3

以下代码将在window.location评估时触发,这不是您想要的:

setTimeout(window.location= "LoginPage.aspx?action=logout", 5000);

你需要:

setTimeout(function(){window.location="LoginPage.aspx?action=logout"}, 5000);

通过将代码包装在一个函数中,它只会在函数被调用时执行,这将在 5 秒后发生。您以前的版本会window.location立即评估,这意味着signalr没有时间与服务器通信。

于 2013-03-16T11:18:50.363 回答