【问题标题】:Auto end video call session from a page Django从页面 Django 自动结束视频通话会话
【发布时间】:2020-06-21 11:11:48
【问题描述】:

我正在用 Django 开发一个自定义视频通话应用。我想在 1 小时后结束视频通话。除了使用 JS 或 AJAX 自动退出页面,还有什么方法吗?如果您需要更多信息,请发表评论

【问题讨论】:

  • 如果您要求 服务器端 方法,this 可能会帮助您。
  • 谢谢。在这里,我没有注销用户。我只是希望用户在一段时间后退出页面。
  • 澄清一下,您正在寻找一种在 1 小时后重定向的方法,在服务器端并且没有 JS 或 AJAX?
  • @correctsyntax 是

标签: django session


【解决方案1】:

我不确定您具体要做什么,但这是一个最小的工作示例(没有 JS 或 AJAX),您应该能够对其进行修改以满足您的需要。

它使用设置一个最大年龄的cookie的原则,例如here

使这项工作的主要内容是将 html 中的 http-equiv="refresh" 元标记设置为与 cookie 的 max_age 相同的时间段。这样,页面将在 cookie(我们设置的)过期时自动刷新,并且页面将重定向而无需进一步输入。

views.py

    from django.shortcuts import redirect, render
    
    
    def initial_page(request):
        # Set the cookie
        response = redirect("video-call")
    
        # You probably want to set it only once for each user and make this secure
        response.set_cookie( 
            "call_timeout", 
            "1",
            max_age=3600 # Set this to the time you need
            )
    
        return response
    
    
    def video_call_page(request):
        # See if the cookie has expired 
        # (This will auto-check because of the http-equiv="refresh" in the HTML)
        try:
            c = request.COOKIES['call_timeout'] # Try to access the cookie
            context = {}
            # The cookie hasn't expired, so continue
            return render(request, "vid_call.html", context)
    
        except KeyError:
            # The time is up, so redirect to the page of your choice
            return redirect("expire_redirect")
    
    
    def redirect_page(request):
        # The cookie expired
        context = {}
        return render(request, "redirect.html", context)

urls.py

from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls import url
from django.conf.urls.static import static

from .views import initial_page, video_call_page, redirect_page

urlpatterns = [
    url(r'^$', initial_page, name='initial'),
    url(r'^video-call/$', video_call_page, name='video-call'),
    url(r'^expired/$', redirect_page, name='expire_redirect'),

    path('admin/', admin.site.urls),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT

vid_call.html

{% load static %}
<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>

<!-- Set http-equiv="refresh" to the same as the max_age param -->
<meta http-equiv="refresh" content="3600">
</head>
<body>
    <h1>Video call page</h1>
    <video width="320" height="240" autoplay>
      <!-- Set the path to a test video in your static dir (represents your video call) -->
      <source src="{% static 'mov_bbb.mp4' %}" type="video/mp4">
      Your browser does not support the video tag.
    </video> 
</body>
</html>

redirect.html 只是您希望用户在一小时(或任何其他时间段等)后重定向到的页面内容,因此我不会在此处包含它。

谢谢。

【讨论】:

  • 感谢您的回答。我一定会试试这个,让你知道。
  • @PSN,这回答了你的问题吗? :)
猜你喜欢
  • 2021-08-09
  • 2015-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-15
  • 1970-01-01
  • 2011-04-10
  • 1970-01-01
相关资源
最近更新 更多