简体   繁体   English

检查其他用户使用的资源的可用性

[英]Check availability of resource used by another user

Building a web application.构建 Web 应用程序。

User have access trough their browser to shared resources host on a server, however if UserA is already using Resource1, Resource1 should not be available to UserB until UserA release Resource1 or until a given amount of time.用户可以通过浏览器访问服务器上的共享资源主机,但是如果 UserA 已经在使用 Resource1,则在 UserA 释放 Resource1 或在给定的时间之前,UserB 不应使用 Resource1。

For this part : I chose to use a MySQL table with a list of tuples (resource,currentuser) and run a cron task to delete expired tuples.对于这一部分:我选择使用带有元组列表(资源、当前用户)的 MySQL 表并运行 cron 任务来删除过期的元组。

Now I want to be able to notify UserA that UserB wants to access Resource1 and if get not answer from UserA, then UserA lost his lock on Resource1 and then the Resource is then available to UserB.现在,我希望能够通知 UserA,UserB 想要访问 Resource1,如果没有得到 UserA 的答复,则 UserA 失去了对 Resource1 的锁定,然后该资源可供 UserB 使用。

For this part, I guess I have to use AJAX.对于这部分,我想我必须使用 AJAX。 I have thought about the following solution :我想过以下解决方案:

User's browser make periodic AJAX call (let's say each minute) to prove he is still alive and upon a call, if another User has requested the same resource, he has to challenge a server request in a given amount of time(for example a captcha).用户的浏览器定期进行 AJAX 调用(假设每分钟)以证明他还活着,并且在调用时,如果另一个用户请求了相同的资源,他必须在给定的时间内挑战服务器请求(例如验证码) )。 If the challenge fails, it means the user is not here anymore (maybe he left his browser opened or the webpage unfocused).如果挑战失败,则意味着用户不再在这里(可能他打开浏览器或未聚焦网页)。

The tricky part is : "he has to challenge a server request in a given amount of time (for example a captcha)".棘手的部分是:“他必须在给定的时间内(例如验证码)挑战服务器请求”。 How to do that?怎么做?

Am I following the best path ?我是否遵循最佳路径?

Yes, what you've outlined is fine.是的,你概述的很好。 Using ajax is also completely fine, especially if you're simply polling every minute.使用 ajax 也完全没问题,特别是如果你只是每分钟轮询一次。

For example, let's say you have the following:例如,假设您有以下内容:

setInterval(function() {
 $.get('/resource/status', function(response) {
    if (response.data.newRequest) {
       //This would signal a new request to the resource
    }
 })
}, 60000)

When handling the new request to access the resource, you could use something like reCaptcha and display that however appropriate (overlay or inline).在处理访问资源的新请求时,您可以使用reCaptcha 之类的内容并显示适当的内容(覆盖或内联)。 When you do this, you could also start a timer to determine if it's exceeded the amount of time allocated or not.执行此操作时,您还可以启动计时器以确定它是否超过分配的时间量。 If it has, then you can do another ajax request and revoke this person's access to the resource, or however you want to handle that.如果有,那么您可以执行另一个 ajax 请求并撤销此人对资源的访问权限,或者您想以任何方式处理该请求。

i would use web sockets to control all the users that need to get the resource.我会使用网络套接字来控制需要获取资源的所有用户。 this way you will know who is connected and using the resource and when he finish using it you can let the next user the resource and so on , (this way can tell each user an estimation of how much time it will take him to get the resource and do some progress bar)通过这种方式,您将知道谁连接并使用资源,当他使用完资源时,您可以让下一个用户使用资源等等,(这种方式可以告诉每个用户估计他需要多长时间才能获得资源)资源并做一些进度条)

I think there're two problems here.我认为这里有两个问题。

  1. How to notify users that resource becomes available?如何通知用户资源可用? Periodic AJAX requests might be okay, but you can also consider long-polling or websockets to get close to notifying waiting users in real time.定期的 AJAX 请求可能没问题,但您也可以考虑使用长轮询或 websockets 来接近实时通知等待的用户。

  2. How to find out that resource is still used by user?如何找出用户仍在使用的资源? If you want to catch the moment when human user is not doing anything on page, you can track mouse movement/clicking or keyboard button pressing.如果您想捕捉人类用户未在页面上执行任何操作的时刻,您可以跟踪鼠标移动/点击或键盘按钮按下。 If nothing is done for last n minutes, the page might be considered as not active.如果最后 n 分钟没有做任何事情,页面可能被认为是不活动的。 If you want to make sure that page is not exploited by automated software, you can ask to fill in captcha once in n minutes when resource is being used.如果您想确保该页面不被自动化软件利用,您可以要求在资源被使用时每 n 分钟填写一次验证码。

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

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