简体   繁体   English

如何检查某个特定用户是否有互联网连接?

[英]How can I check whether some specific user has an internet connection?

I maintain a system built with Ruby on Rails.我维护一个用 Ruby on Rails 构建的系统。 The client has asked me to prevent the system from working when a specific user has no internet connection.客户要求我在特定用户没有互联网连接时阻止系统工作。

How would you do it?你会怎么做?

I need to check if a specific computer has internet connection!我需要检查特定的计算机是否有互联网连接! For instance if my pc or your pc or my mother's pc has no internet connection!例如,如果我的电脑或您的电脑或我母亲的电脑没有互联网连接!

This has nothing to do with Ruby on Rails.这与 Ruby on Rails 无关。

Is this user accessing a site that's on a local network but no outside internet access?此用户是否正在访问位于本地网络但无法访问外部 Internet 的站点? If so you can have a before_filter or some kind of method on your homepage that tries to ping google.com or some other site outside of the LAN to check for internet.如果是这样,您可以在主页上使用before_filter或某种方法来尝试 ping google.com或 LAN 外的其他站点以检查互联网。

Use Offline.js使用Offline.js

  1. Add offline.min.js file in your vendor/assets/javascripts在您的 vendor/assets/javascripts 中添加 offline.min.js 文件
  2. Require it in your app/assets/javascripts/application.js as //= require offline.min在你的 app/assets/javascripts/application.js 中要求它作为//= require offline.min
  3. Add the following code in the same application.js file在同一个 application.js 文件中添加以下代码

    $(document).ready(function() { Offline.check(); Offline.on('up', function() { $('a.btn').removeClass('disabled'); }); Offline.on('down', function() { $('a.btn').addClass('disabled'); }); });

This is in relevance to my code where I just disable all buttons when the connection goes off.这与我的代码有关,我只是在连接断开时禁用所有按钮。

The default behaviour of Offline.js adds a overlay with a please wait message until the internet connection comes back on. Offline.js 的默认行为添加了一个带有请稍候消息的覆盖,直到互联网连接恢复。

I prefer to handle it this way as it give better user experience.我更喜欢以这种方式处理它,因为它可以提供更好的用户体验。 Disabling buttons is a subtle way of preventing a user from working.禁用按钮是一种防止用户工作的微妙方式。

So just to clarify, your Rails application exists for users to send food delivery requests to one specific user -- the shop preparing and sending that food out, presumably -- and you want users to be unable to create new delivery requests when the shop/owner isn't currently online?因此,澄清一下,您的 Rails 应用程序存在供用户向一个特定用户发送送餐请求——大概是准备和发送食物的商店——并且您希望用户在商店/楼主目前不在线?

The simplest way to implement this would be to have an Admin Dashboard area where the owner can click a button to 'open the shop' and allow new orders.实现这一点的最简单方法是拥有一个管理仪表板区域,所有者可以在其中单击按钮以“打开商店”并允许新订单。 The worry, then, is about the shop's connection going offline, resulting in customers whose order are never received.然后,担心的是商店的连接脱机,导致客户从未收到订单。 To get around that, you can have each customer order start in an 'unconfirmed' state;为了解决这个问题,您可以让每个客户订单都以“未确认”状态开始; when a new order is submitted, the owner is notified, and he can click a button to confirm it, letting the user know that yes, their order has been seen and is being made (and their credit card can be charged then , if you have online payment).当一个新的提交订单,业主被告知,他可以点击一个按钮来确认,让用户知道,是的,他们的订单已经看到并正在取得(与他们的信用卡可以充电,然后,如果你有在线支付)。 This is probably the most reliable solution because what you really want is confirmation that a human has acknowledged the order.这可能是最可靠的解决方案,因为您真正想要的是确认人类已确认订单。

If you do want to tie it directly to online status, then you can have the Admin Dashboard page regularly check that it's being viewed.如果您确实想将它直接与在线状态联系起来,那么您可以让管理仪表板页面定期检查它是否正在被查看。 You could accomplish this with a websockets connection, see Action Cable introduced in Rails 5 .您可以使用 websockets 连接来完成此操作请参阅 Rails 5 中引入的 Action Cable The Admin Dashboard page opens a websocket connection upon login; Admin Dashboard 页面在登录时打开一个 websocket 连接; the new-connection event sets accepting_orders = true . new-connection 事件设置accepting_orders = true The websocket disconnect event sets accepting_orders = false , which will happen soon after their connection is dropped (make sure to check the timeout settings). websocket 断开连接事件设置accepting_orders = false ,这将在他们的连接断开后不久发生(确保检查超时设置)。 Without websockets, you could have a background HTTP request occurring on an interval;如果没有 websockets,你可能会有一个间隔发生的后台 HTTP 请求; x seconds without receiving that request could cause Rails to stop accepting new orders. x秒没有收到该请求可能会导致 Rails 停止接受新订单。

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

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