使用javascript ping一個webapp以保持會話打開

[英]Using javascript for pinging a webapp to keep session open


I'm writing a greasemonkey script to keep session open on a webapp I use for work. Which javascript command would you use to create some feedback with the server and ensure the session doesn't fall without having to bother the user making a complete refresh of the page?

我正在寫一個greasemonkey腳本,以便在我用於工作的webapp上打開會話。您將使用哪個javascript命令與服務器創建一些反饋,並確保會話不會下降而不必打擾用戶完全刷新頁面?

6 个解决方案

#1


I've solved the issue using:

我用以下方法解決了這個問題:

function keepAlive() {
    var httpRequest = new XMLHttpRequest();
    httpRequest.open('GET', "/restricted_file_url");
    httpRequest.send(null);
}

setInterval(keepAlive, 840000);  //My session expires at 15 minutes

#2


Second choice

If you absolutely insist on using Greasemonkey, any element.click() method on any event that submits an XMLHTTPrequest should do.

如果你絕對堅持使用Greasemonkey,那么提交XMLHTTPrequest的任何事件上的任何element.click()方法都應該這樣做。

First choice

If you are willing to use a solution that does not require you to write a Greasemonkey script:

如果您願意使用不需要編寫Greasemonkey腳本的解決方案:

ReloadEvery 3.0.0, by Jaap Haitsma

由Jaap Haitsma完成的ReloadEvery 3.0.0

This reloads web pages every so many seconds or minutes. The function is accessible via the context menu (the menu you get when you right click on a web page) or via a drop down menu on the reload button.

這會每隔很多秒或幾分鍾重新加載網頁。可以通過上下文菜單(右鍵單擊網頁時獲得的菜單)或通過重新加載按鈕上的下拉菜單訪問該功能。

It's not what you asked for, but unless you simply want to brush up on your javascript skills, this is probably the most straight-forward method for solving your problem.

這不是你要求的,但除非你只是想要提高你的javascript技能,這可能是解決問題最直接的方法。

#3


If you want to write JavaScript to solve this (the other answers are a lot easier and I would recommend going the easier route), here's what I would do:

如果你想編寫JavaScript來解決這個問題(其他答案要容易得多,我建議你選擇更簡單的方法),這就是我要做的事情:

  • Call document.createElement("iframe") to create an iframe
  • 調用document.createElement(“iframe”)來創建iframe

  • Size the iframe to 0x0, style.display = "none", set the ID to something, and set the src property to a page on the site that will extend your session
  • 將iframe的大小設置為0x0,style.display =“none”,將ID設置為某個值,並將src屬性設置為站點上將擴展會話的頁面

  • Use document.body.appendChild to add the iframe to the page
  • 使用document.body.appendChild將iframe添加到頁面

  • Using setTimeout refresh the iFrame using window.frames['iframeID'].location.reload(true); and also call setTimeout to refresh the page again.
  • 使用setTimeout使用window.frames ['iframeID']刷新iFrame。location.reload(true);並調用setTimeout再次刷新頁面。

See if the other answers would work because using JavaScript seems like it would be more trouble than it's worth.

看看其他答案是否有效,因為使用JavaScript似乎比它的價值更麻煩。

#4


Using some tips from Jesse's answer, here's the complete code.

使用Jesse的答案中的一些提示,這是完整的代碼。

You will need to update the @match param with your site's domain

您需要使用您網站的域更新@match參數

// ==UserScript==
// @name         Auto session extender
// @namespace    http://obive.net/
// @version      0.2
// @description  Automatically extend server-side session
// @author       Charlie Hayes
// @match        http://obive.net/
// @grant   GM_getValue
// @grant   GM_setValue
// @noframes
// ==/UserScript==

(function() {
    'use strict';

    console.log('The session for this site will be extended automatically via userscript.');

    var minute = 60*1000;
    var refreshTime = 15 * minute;

    var iframe = document.createElement("iframe");
    iframe.style.width = 0;
    iframe.style.height=0;

    var loc = window.location;
    var src = loc.protocol +'//' + loc.host + loc.pathname;
    src += loc.search ? (loc.search + '&') : '?';
    src +=  'sessionextendercachebuster=';

    var reloadIframe = function(){
        var time = new Date().getTime();
        var lastRefresh = GM_getValue('lastRefresh');
        var timeSinceLastRefresh = time - lastRefresh;
        if (!lastRefresh || timeSinceLastRefresh > refreshTime - minute) {
            console.log('Auto-extending session');
            iframe.src = src + time;
            GM_setValue('lastRefresh',time);
            setTimeout(reloadIframe, refreshTime);

            setTimeout(function(){
                // Unload the iframe contents since it might be taking a lot of memory
                iframe.src='about:blank';
            },10000);
        }else{
            console.log('Another tab/window probably refreshed the session, waiting a bit longer');
            setTimeout(reloadIframe, refreshTime - timeSinceLastRefresh - minute);
        }
    };
    setTimeout(reloadIframe, refreshTime);
    document.body.appendChild(iframe);
})();

#5


I can definitely recommend the solution suggested by dreftymac!

我絕對可以推薦dreftymac建議的解決方案!

If you don't want to worry about remembering to click the reloadevery option, your grease monkey script is simply

如果你不想擔心忘記點擊reloadevery選項,你的油脂猴子腳本就是這么簡單

window.location.href = window.location.href

#6


This is presuming you don't want to keep the page constantly refreshing, as suggested by others.

這是假設你不想像其他人所建議的那樣不斷刷新頁面。

Ordinarily, if you were simply looking to ping the server, you would probably do best to do something like use an image that you know exists on the remote server, and check it's height - thus creating traffic.

通常,如果您只是想ping服務器,您可能最好做一些事情,比如使用遠程服務器上存在的圖像,並檢查它的高度 - 從而創建流量。

However, your problem (I presume) needs to access some limited area of the website, in order to keep the session active... with this being the case (and the fact that you can't directly ping), find a page that is 'restricted' and do an Ajax call, or similar. Don't quote me on this, as I'm by no means a JavaScript expert... just throwing out a potential solution.

但是,你的問題(我認為)需要訪問網站的某些有限區域,以保持會話活動...在這種情況下(以及你不能直接ping)的事實,找到一個頁面,被“限制”並執行Ajax調用或類似操作。不要引用我這個,因為我不是一個JavaScript專家......只是拋出一個潛在的解決方案。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/05/11/7208c2c7a536437303b5e5f7897e4c27.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com