Django 3.x 原生支持websocket 配置
2024-08-24 19:31:14
websocket.py
1 # websocket.py
2 async def websocket_application(scope, receive, send):
3 while True:
4 event = await receive()
5
6 if event['type'] == 'websocket.connect':
7 await send({
8 'type': 'websocket.accept'
9 })
10
11 if event['type'] == 'websocket.disconnect':
12 break
13
14 if event['type'] == 'websocket.receive':
15 if event['text'] == 'ping':
16 await send({
17 'type': 'websocket.send',
18 'text': 'pong!'
19 })
asgi.py
1 """
2 ASGI config for chatroom project.
3
4 It exposes the ASGI callable as a module-level variable named ``application``.
5
6 For more information on this file, see
7 https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/
8 """
9
10 import os
11
12 from django.core.asgi import get_asgi_application
13 from chatroom.websocket import websocket_application
14
15 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'chatroom.settings')
16
17 django_application = get_asgi_application()
18
19 async def application(scope, receive, send):
20 if scope['type'] == 'http':
21 await django_application(scope, receive, send)
22 elif scope['type'] == 'websocket':
23 await websocket_application(scope, receive, send)
24 else:
25 raise NotImplementedError(f"Unknown scope type {scope['type']}")
最新文章
- SMARTY模板中如何使用get,post,request,cookies,session,server变量
- IE8浏览器不能识别CSS伪类的解决办法。
- Java-接口练习
- 架构模式之REST架构
- load url from future 解释
- YAML 语言语法
- WIN8重见开始菜单
- ajax 初始化请求前携带参数
- android studio github 项目导入问题
- how to remove MouseListener / ActionListener on a JTextField
- 有趣html5(两)----使用canvas结合剧本画在画布上的简单图(html5另一个强大)
- (二)Hololens Unity 开发之 语音识别
- 修改MySQL数据库密码
- PHp连接数据库实现增删改查
- ASP.NET没有魔法——ASP.NET MVC使用Oauth2.0实现身份验证
- MYSQL 二进制安装
- ASM: Active Shape Models--Their Training and Application
- Codeforces 948D Perfect Security 【01字典树】
- freeswitch黑名单mod_blacklist使用
- JS参差不齐的数组