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']}")

最新文章

  1. SMARTY模板中如何使用get,post,request,cookies,session,server变量
  2. IE8浏览器不能识别CSS伪类的解决办法。
  3. Java-接口练习
  4. 架构模式之REST架构
  5. load url from future 解释
  6. YAML 语言语法
  7. WIN8重见开始菜单
  8. ajax 初始化请求前携带参数
  9. android studio github 项目导入问题
  10. how to remove MouseListener / ActionListener on a JTextField
  11. 有趣html5(两)----使用canvas结合剧本画在画布上的简单图(html5另一个强大)
  12. (二)Hololens Unity 开发之 语音识别
  13. 修改MySQL数据库密码
  14. PHp连接数据库实现增删改查
  15. ASP.NET没有魔法——ASP.NET MVC使用Oauth2.0实现身份验证
  16. MYSQL 二进制安装
  17. ASM: Active Shape Models--Their Training and Application
  18. Codeforces 948D Perfect Security 【01字典树】
  19. freeswitch黑名单mod_blacklist使用
  20. JS参差不齐的数组

热门文章

  1. ppt-页面大小和颜色更改
  2. 9、Spring Boot安全
  3. mysql 分组查询
  4. Idea中如何导入jar包
  5. win10安装jenkins忘记密码的解决方法
  6. java Base64算法
  7. 放进你的收藏夹吃灰!Linux 运维必备的 40 个命令总结
  8. 使用 IDEA 创建多模块项目
  9. windows下使用pyinstaller将多个目录的Python文件打包成exe可执行文件
  10. PyQt(Python+Qt)学习随笔:QTreeWidget中给树型部件增加顶层项的方法