【问题标题】:Django channels error : TypeError: __call__() missing 1 required positional argument: 'send'Django 频道错误:TypeError:__call__() 缺少 1 个必需的位置参数:'send'
【发布时间】:2022-06-22 18:45:33
【问题描述】:

我正在研究 Django 频道和异步应用程序,我对它完全陌生。 我开始看那个教程。当我运行 django 服务器时,它工作正常,但是当我加载页面或尝试连接 websocket king 客户端以测试服务器时,它显示错误 ->


WebSocket HANDSHAKING /ws/game/roomname [127.0.0.1:51190]
Exception inside application: __call__() missing 1 required positional argument: 'send'
Traceback (most recent call last):
  File "C:\Users\user\anaconda3\lib\site-packages\channels\staticfiles.py", line 44, in __call__
    return await self.application(scope, receive, send)
  File "C:\Users\user\anaconda3\lib\site-packages\channels\routing.py", line 71, in __call__
    return await application(scope, receive, send)
  File "C:\Users\user\anaconda3\lib\site-packages\channels\sessions.py", line 47, in __call__
    return await self.inner(dict(scope, cookies=cookies), receive, send)
  File "C:\Users\user\anaconda3\lib\site-packages\channels\sessions.py", line 263, in __call__
    return await self.inner(wrapper.scope, receive, wrapper.send)
  File "C:\Users\user\anaconda3\lib\site-packages\channels\auth.py", line 185, in __call__
    return await super().__call__(scope, receive, send)
  File "C:\Users\user\anaconda3\lib\site-packages\channels\middleware.py", line 26, in __call__
    return await self.inner(scope, receive, send)
  File "C:\Users\user\anaconda3\lib\site-packages\channels\routing.py", line 150, in __call__
    return await application(
  File "C:\Users\user\anaconda3\lib\site-packages\asgiref\compatibility.py", line 34, in new_application
    return await instance(receive, send)
TypeError: __call__() missing 1 required positional argument: 'send'
WebSocket DISCONNECT /ws/game/roomname [127.0.0.1:51190]

我是新手,无法解决问题。请帮忙。

我的项目 asgi.py

import os
from channels.routing import ProtocolTypeRouter , URLRouter 
from channels.auth import AuthMiddlewareStack
from home.consumers import GameRoom
from django.urls import path

from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tictac.settings')

application = get_asgi_application()

ws_pattern = [
    path("ws/game/<room_code>" , GameRoom)
]

application = ProtocolTypeRouter({
    "websocket" : AuthMiddlewareStack(URLRouter(
        ws_pattern
    ))
})

如果可能,请对此提供详细说明,以便我了解错误所在。

【问题讨论】:

  • 您使用的是什么版本的 django 和频道?尝试使用GameRoom.as_asgi()
  • 我很抱歉回复晚了,我尝试添加 .as_asgi() 它给了我一个错误,即 NoneType 对象没有属性“group_add”,我正在使用 django_channels 版本 3.0.4
  • 你能分享新错误的整个回溯吗?您似乎缺少/配置错误CHANNEL_LAYERS
  • 尝试使用渠道==3.0.4
  • 问题解决了,伙计。我在消费者名称之后使用了 .as_asgi() 函数。它奏效了。

标签: python django websocket channels


【解决方案1】:

改变你的

ws_pattern = [
    path("ws/game/<room_code>" , GameRoom)
]

ws_pattern = [
    path("ws/game/<room_code>" , GameRoom.as_asgi())
]

【讨论】:

    猜你喜欢
    • 2021-09-12
    • 2022-11-10
    • 2019-05-27
    • 2020-12-24
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    相关资源
    最近更新 更多