11 Django achieve WebSocket

  Because of the need to display real-time status of demand, we thought websocket, but Django does not support native websocket, later searched chango-channels project, can be achieved in the current demand.

A, Channels

  Official Documents

Second, the installation configuration

  Installation channels

pip install -U channels

  Configuring channels

  You must be configured in the settings to use channels.

1 #注册到app当中
2 INSTALLED_APPS = (
3     'django.contrib.auth',
4     'django.contrib.contenttypes',
5     'django.contrib.sessions',
6     'django.contrib.sites',
7     ...
8     'channels',
9 )

  Set the default routing channels of

. 1  from channels.routing Import ProtocolTypeRouter
 2  
. 3 file application = ProtocolTypeRouter ({
 . 4      # Add django view (route in url.py) 
5 })

  Finally set ASGI_APPLICATION, and then start the project

ASGI_APPLICATION = "myproject.routing.application"

  Because the channels to the role in the global project, it is best to channels this app into the top of INSTALLED_APPS.

  To obtain the latest version of Channels To change the repo source, switch to the project in a virtual environment, the installation

1 $ git clone [email protected]:django/channels.git
2 $ cd channels
3 $ <activate your project’s virtual environment>
4 (environment) $ pip install -e .  # the dot specifies the current repo

   One with channels redis used: https://www.jianshu.com/p/3de90e457bb4

Guess you like

Origin www.cnblogs.com/a2534786642/p/11094869.html
Recommended