【翻译】Django Channels 官方文档 -- Tutorial

Django Channels 官方文档

https://channels.readthedocs.io/en/latest/index.html


Tutorial
教程

Channels allows you to use WebSockets and other non-HTTP protocols in your Django site. For example you might want to use WebSockets to allow a page on your site to immediately receive updates from your Django server without using HTTP long-polling or other expensive techniques.
Channels 允许您在 Django 站点中使用 Websockets 和其他非 HTTP 协议。例如, 您可能希望 Websockets 允许网站上的页面立即从 Django 服务器接收更新, 而无需使用 HTTP 长轮询或其他昂贵的技术。

In this tutorial we will build a simple chat server, where you can join an online room, post messages to the room, and have others in the same room see those messages immediately.
在本教程中, 我们将建立一个简单的聊天服务器, 在那里你可以加入一个在线房间, 张贴消息到房间, 并让其他人在同一房间看到这些消息立即。

  • Tutorial Part 1: Basic Setup
  • 教程1部分: 基本设置
  • Tutorial Part 2: Implement a Chat Server
  • 教程2部分: 实现聊天服务器
  • Tutorial Part 3: Rewrite Chat Server as Asynchronous
  • 教程3部分: 将聊天服务器重写为异步
  • Tutorial Part 4: Automated Testing
  • 教程4部分: 自动化测试

Tutorial Part 1: Basic Setup

教程1部分: 基本设置

In this tutorial we will build a simple chat server. It will have two pages:
在本教程中, 我们将构建一个简单的聊天服务器。它将有两个页面:

  • An index view that lets you type the name of a chat room to join.
  • 一个 index 视图, 用于输入要加入的聊天室的名称。
  • A room view that lets you see messages posted in a particular chat room.
  • 一个可以查看在特定聊天室中发送的消息的房间视图。

The room view will use a WebSocket to communicate with the Django server and listen for any messages that are posted.
房间视图将使用 WebSocket 与 Django 服务器进行通信, 并监听任何发送出来的消息。

We assume that you are familar with basic concepts for building a Django site. If not we recommend you complete the Django tutorial first and then come back to this tutorial.
我们假设您熟悉构建 Django 站点的基本概念。如果不是, 我们建议您先完成 Django 教程, 然后再回到本教程。

We assume that you have Django installed already. You can tell Django is installed and which version by running the following command in a shell prompt (indicated by the $ prefix):
我们假设已经安装了 Django。您可以通过在 shell 提示符下运行以下命令 (用 $ 前缀表示) 来查看 您安装的 Django 版本:

$ python3 -m django --version

We also assume that you have Channels installed already. You can tell Channels is installed by running the following command:
我们还假设您已经安装了频道。您可以通过运行以下命令来查看 Channels 安装与否:

$ python3 -c 'import channels; print(channels.__version__)'

This tutorial is written for Channels 2.0, which supports Python 3.5+ and Django 1.11+. If the Channels version does not match, you can refer to the tutorial for your version of Channels by using the version switcher at the bottom left corner of this page, or update Channels to the newest version.
本教程是为 Channels 2.0 编写的, 它支持 Python 3.5 + 和 Django 1.11 +。如果 Channels 版本不匹配, 你可以使用本页左下角的版本切换器, 或将 Channels 更新到最新版本, 以参考您的频道版本的教程。

This tutorial also uses Docker to install and run Redis. We use Redis as the backing store for the channel layer, which is an optional component of the Channels library that we use in the tutorial. Install Docker from its official website - there are official runtimes for Mac OS and Windows that make it easy to use, and packages for many Linux distributions where it can run natively.
本教程还使用 Docker 安装和运行 Redis。我们使用 Redis 作为 Channels 层的后备存储, 它是我们在教程中使用的 Channels 库的可选组件。从其官方网站安装 Docker --有用于 Mac OS 和 Windows 的易于使用的正式运行版, 并为许多 Linux 发行版提供了可本地运行的软件包。

Note
While you can run the standard Django runserver without the need for Docker, the channels features we’ll be using in later parts of the tutorial will need Redis to run, and we recommend Docker as the easiest way to do this.
注意
虽然您可以运行标准的 Django runserver 不需要 Docker , 我们将使用的 channels 功能在后面的教程将需要 Redis 运行, 我们建议使用 Docker 这一最简单的方式来做到这一点。

Creating a project
新建一个项目

If you don’t already have a Django project, you will need to create one.
如果您还没有 Django 项目, 您将需要创建一个。

From the command line, cd into a directory where you’d like to store your code, then run the following command:
从命令行, 将 cd 放入要存储代码的目录中, 然后运行以下命令:

$ django-admin startproject mysite

This will create a mysite directory in your current directory with the following contents:
这将在当前目录中创建一个 mysite 目录, 其中有以下内容:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

Creating the Chat app
创建聊天应用程序

 

猜你喜欢

转载自www.cnblogs.com/chuangming/p/9222794.html