Django+channels -> websocket
学习视频: https://www.bilibili.com/video/BV1J44y1p7NX/?p=10 (视频中的版本是 django3
)
实战code: https://blog.csdn.net/qq_22038327/article/details/128603272
workon # 查看虚拟环境
mkvirtualenv web -p python3.10 # 创建虚拟环境
workon web # 进入虚拟环境
pip install django==4.1.5
pip install channels==4.0.0
pip install daphne==4.0.0
pip install black==22.12.0 # format code
django-admin startproject ws_demo
cd ws_demo
python manage.py startapp app01
修改asgi.py文件
http+webscoket
index.html
consumer.ChatConsmer.as_asgi() # 处理视图的类
- wsgi
- asgi
聊天室
-
建立连接
-
client -> server
-
server -> client
clinet 端的socket
- 3个回调函数
- 2个方法
群聊
channel layers 实现群聊
通过群号区分聊天分组