Javascript - 只需识别浏览器连接的网络即可
Javascript - Simply identify on which network the browser is connected
我设置了一个节点js Web服务器,它在JSON文件中提供图像链接。一个链接引用互联网上的图像,另一个链接引用我家庭服务器中的本地目录。
Web 服务器处于联机状态,而不是在我的主服务器上。出于带宽使用目的,Web 服务器仅提供图像链接。如果客户端通过我的家庭连接连接,则使用本地链接,如果不是,则使用Internet链接。
目前,我提供一个包含链接的 JSON 文件,并且总是在客户端选择互联网文件,因为我不知道如何让客户端了解他在我的本地网络上。
有什么存在吗?我已经做了关于获取路由器(第一跳)的 mac 地址、网络 ssid、...但这似乎不能只用JavaScript来实现。
所以,基本上,这是我的设置:
家庭网络 :
- 主服务器(本地存储的图像)
- 通过此连接连接的潜在客户
网络托管内容 :
- Web 服务器(节点 js)
- FTP(存储的图像)
- 潜在客户已连接
在任何情况下,客户端都需要互联网连接,因为节点服务器处于联机状态。但是,如果客户端与本地主服务器位于同一(我的本地网络)网络上,则他应使用本地链接来获取图像。
我需要以下两种解决方案之一:
- 客户端知道他是否在我的本地网络上,然后选取良好的链接
- Web 服务器知道客户端来自哪里,并直接提供服务良好的链接(因此,客户端没有问题)。问题是我的 Web 服务器没有托管在我的家庭网络上。
服务器端写一些东西,才能让它工作。
只有服务器才能判断客户端是否与服务器位于同一网络上。客户端JS根本无法访问网络信息。
你会做这样的事情:
在客户端请求中,服务器:
- 检查客户端的连接 IP 是否为本地 IP。
- 如果是这样,请设置客户端可以读取的某种布尔值。
- 然后,客户端根据该布尔值选择本地或远程 url 集。
选择要包含在页面中的网址要简单一些:
在客户端请求中,服务器:
- 检查客户端的连接 IP 是否为本地 IP。
- 如果是这样,请在请求的网页中包含本地 URL。否则,请包含远程 URL。
我正在研究以下server connection lost
PHP,jQuery和Bootstrap CSS。它每秒检查一次浏览器是否能够连接到服务器上托管<?=__DOMAIN_URL__;?>/network_access.gif
单个像素图像,其中<?=__DOMAIN_URL__;?>
是PHP语法,用于将服务器URL管道到HTML。如果无法建立图像连接,则调用 javascript this.img.onerror
函数。
<?php
// ============================== start plugin for displaying server downtime ?>
<div id="myModalDT" class="modal fade" role="dialog" data-backdrop="static">
<div class="modal-dialog mt-4 pt-2" style="min-width: 75% !important;">
<div class="modal-content">
<div class="modal-body p-3">
Server disconnect/network outage
</div>
</div>
</div>
</div>
<script>
// server connection check
var timer = $.timer(function() {
this.img = new Image();
d = new Date();
// if the image will not load then we need to alert the user that the network connection was lost ?>
this.img.onerror = function() {
$('#myModalDT').find(".modal-body").html('<span class="h2"><strong>Server connection lost.</strong><br/>Checking connection at '+d.toString('dddd, MMMM ,yyyy') + '<hr/>This message is likely due to the server being restarted and this message should automatically disappear once the connection is restored. <br/><br/> If this message does not disappear in several minutes then refresh the page.</span>');
$('#myModalDT').modal('show');
};
// if the image is loaded then hide the alert
this.img.onload = function () {
$('#myModalDT').modal('hide');
}
<?php // looks on the server to see if we can get to an image file ?>
this.img.src = "<?=__DOMAIN_URL__;?>/network_access.gif?cacheDiscard=" + d.getTime();
});
<?php // setting a timer to check that we have network connectivity every second ?>
timer.set({ time : 1000, autostart : true });
</script>
<?php
// ============================== end plugin for displaying server downtime ?>
将上面的代码剥离为 javascript,您基本上可以简单地将<?=__DOMAIN_URL__;?>
替换为本地服务器 URL,如果调用了 this.img.onload
函数,那么您可以让 javascript 将图像引用 URL 替换为本地 URL,如果调用this.img.onerror
函数(意味着您无法连接到本地服务器),则将图像 URL 替换为"云"服务器 URL。
<script>
this.img = new Image(); // create an empty image object
d = new Date(); // we do not want the browser to cache the image we are checking so we get the current timestamp to clear the cache
this.img.src = "http://localhost-whatever/network_access.gif?cacheDiscard=" + d.getTime(); // this sets the contents of our image object to one of our locally hosted images, with the cache busting timestamp included
this.img.onerror = function() {
// were were not able to connect locally if this were called and should use the server hosted images
};
this.img.onload = function () {
// if we made it here then we can use locally sourced images
}
</script>
- 节点不是持久的网络连接
- 检测与流星的3G网络连接
- 如何通过AJAX调用检查网络连接
- 在 JavaScript 中检查网络连接
- 节点.js mysql 查询和网络连接
- 科尔多瓦 IOS 8.1 打开应用程序时如何检查网络连接
- 检查网络连接已连接/已断开
- 使用IONIC的IOS没有网络连接功能问题
- 断开网络连接时出现消息(SamsungSmart电视应用程序)
- 正在检查Cordova和AngularJS中的网络连接状态
- AngularJS/Cordova应用程序中网络连接断开时显示弹出窗口
- 检查javascript中是否存在网络连接
- 什么'当你'I’我失去了网络连接
- 通过Firefox扩展检查网络连接
- 使用ngCordova检查活跃的网络连接,如果在ionic中不是活跃的,就退出应用
- 如何仅在有网络连接的情况下更新屏幕/网站
- 等到网络连接后再启动节点webkit应用
- jQuery.Get将请求排队,直到网络连接可用
- 如何根据网络连接的可用性切换基础层
- 如何根据网络连接的可用性切换基础层