【发布时间】:2021-08-21 13:44:47
【问题描述】:
我检查了所有相关问题,但没有一个对我有帮助。我正在制作一个简单的应用程序,我将在 websocketking.com 网站上获得 JSON 响应。无论我进行多少次更改,都没有尝试过。 这里的每一个细节
asgi.py 文件
import os
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
from django.urls import path
from home.consumers import *
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
ws_patterns = [
path('ws/test/', TestConsumer),
]
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(URLRouter(ws_patterns)),
})
consumers.py 文件
from channels.generic.websocket import WebsocketConsumer
from asgiref.sync import async_to_sync
import json
class TestConsumer(WebsocketConsumer):
def connect(self):
self.room_name = "test_consumer"
self.room_group_name = "test_consumer_group"
async_to_sync(self.channel_layer.group_add)(self.room_name, self.room_group_name)
self.accept()
self.send(text_data=json.dumps({'status' : 'connected'}))
def receive(self):
pass
def disconnect(self):
pass
这些是我在 settings.py 文件中添加的内容
ASGI_APPLICATION = 'core.asgi.application'
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("localhost", 6379)],
},
},
}
这是我在控制台中遇到的错误
WebSocket HANDSHAKING /ws/test/ [127.0.0.1:2795]
Exception inside application: object.__init__() takes exactly one argument (the instance to initialize)
Traceback (most recent call last):
File "D:\PythonProjects\channels_project\env\lib\site-packages\channels\staticfiles.py", line 44, in __call__
return await self.application(scope, receive, send)
File "D:\PythonProjects\channels_project\env\lib\site-packages\channels\routing.py", line 71, in __call__
return await application(scope, receive, send)
File "D:\PythonProjects\channels_project\env\lib\site-packages\channels\sessions.py", line 47, in __call__
return await self.inner(dict(scope, cookies=cookies), receive, send)
File "D:\PythonProjects\channels_project\env\lib\site-packages\channels\sessions.py", line 254, in __call__
return await self.inner(wrapper.scope, receive, wrapper.send)
File "D:\PythonProjects\channels_project\env\lib\site-packages\channels\auth.py", line 181, in __call__
return await super().__call__(scope, receive, send)
File "D:\PythonProjects\channels_project\env\lib\site-packages\channels\middleware.py", line 26, in __call__
return await self.inner(scope, receive, send)
File "D:\PythonProjects\channels_project\env\lib\site-packages\channels\routing.py", line 150, in __call__
return await application(
File "D:\PythonProjects\channels_project\env\lib\site-packages\asgiref\compatibility.py", line 33, in new_application
instance = application(scope)
File "D:\PythonProjects\channels_project\env\lib\site-packages\channels\generic\websocket.py", line 23, in __init__
super().__init__(*args, **kwargs)
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
WebSocket DISCONNECT /ws/test/ [127.0.0.1:2795]
【问题讨论】:
标签: django websocket django-channels