简体   繁体   English

如何有效地满足该用例?

[英]How to satisfy this use case effectively?

I have a web app that is written using Python and Flask. 我有一个使用Python和Flask编写的Web应用程序。 This web app runs only when the user is connected to the internal VPN. 仅当用户连接到内部VPN时,此Web应用程序才运行。 Now let's say that the user is connected to internet and VPN and is browsing the web app and his VPN gets disconnected. 现在,假设用户已连接到Internet和VPN,并且正在浏览Web应用程序,而他的VPN已断开连接。 In this case he will no longer be able to access the web app. 在这种情况下,他将不再能够访问Web应用程序。 I want to notify the user through my app to connect his VPN again. 我想通过我的应用程序通知用户再次连接他的VPN。 I have written a Javascript wherein I am making a HEAD ajax call to the app and depending on the status of the response tell whether he is connected to VPN or not. 我写了一个Javascript,其中我正在向应用程序进行HEAD ajax调用,并根据响应的状态判断他是否已连接到VPN。 Below are the two functions. 以下是两个功能。

function isConnectedToVpn(){
    var xhr = new (window.ActiveXObject || XMLHttpRequest)("Microsoft.XMLHTTP");
    var status;
    xhr.open("HEAD", "//" + window.location.hostname , false);
    try{
        xhr.send();
        return (xhr.status >= 200 && (xhr.status < 300 || xhr.status === 304));
    }catch (error){
       return false;
    }
}

function isOnline()
{
    return navigator.onLine;
}

Now how can I let the end user download this JS and then run it till his entire session and alert him whenever the site cannot be connected. 现在如何让最终用户下载此JS,然后运行它直到整个会话,并在站点无法连接时提醒他。 Also , how will I be able to reduce false positives for instance , if he is connected to VPN and there is a real problem in the app. 此外,例如,如果他连接到VPN并且应用程序中存在实际问题,我将如何减少误报。 Any pointers would be useful. 任何指针都是有用的。

Put your js code on the static folder, then change your js code for isOnline 将您的js代码放在静态文件夹中,然后更改isOnline的js代码

document.onload = function isOnline()
{
    if !(isConnectedToVpn())
    {
        alert('Your not connected to the VPN');
    }
}

Now make all your templates inherit from a main (base.html) template that import the js: 现在,使所有模板都从导入js的主(base.html)模板继承:

on base.html 在base.html上

`<script src="{{url_for('yourapp.static',filename='js/yourjsfile.js')}}"></script>`

Put on all your templates this: 放上所有模板:

{% extends "yourapp/base.html" %}
..
..
..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM