【发布时间】:2021-04-16 08:02:46
【问题描述】:
我正在关注Coding With Mitch 聊天应用,但遇到了错误。
当我运行服务器时,它工作正常,但是当我刷新浏览器时它会显示
TypeError: object.init() 只接受一个参数(要初始化的实例) WebSocket DISCONNECT /public_chat/1/ [127.0.0.1:50083]
settings.py
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
},
},
}
路由.py
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from django.urls import path, re_path
from chat.consumers import ChatConsumer
from public_chat.consumers import PublicChatConsumer
from notification.consumers import NotificationConsumer
application = ProtocolTypeRouter({
'websocket': AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter([
re_path('', NotificationConsumer.as_asgi()),
re_path('chat/<room_id>/', ChatConsumer.as_asgi()),
re_path('public_chat/<room_id>/', PublicChatConsumer.as_asgi()),
])
)
),
})
websockets.py
import json
from asgiref.sync import async_to_sync
from ..consumer import AsyncConsumer, SyncConsumer
from ..exceptions import (
AcceptConnection,
DenyConnection,
InvalidChannelLayerError,
StopConsumer,
)
class WebsocketConsumer(SyncConsumer):
groups = None
def __init__(self, *args, **kwargs):
if self.groups is None:
self.groups = []
def websocket_connect(self, message):
try:
for group in self.groups:
async_to_sync(self.channel_layer.group_add)(group,
self.channel_name)
except AttributeError:
raise InvalidChannelLayerError(
"BACKEND is unconfigured or doesn't support groups"
)
try:
self.connect()
except AcceptConnection:
self.accept()
except DenyConnection:
self.close()
def connect(self):
self.accept()
def accept(self, subprotocol=None):
super().send({"type": "websocket.accept", "subprotocol": subprotocol})
def websocket_receive(self, message):
if "text" in message:
self.receive(text_data=message["text"])
else:
self.receive(bytes_data=message["bytes"])
def receive(self, text_data=None, bytes_data=None):
pass
def send(self, text_data=None, bytes_data=None, close=False):
if text_data is not None:
super().send({"type": "websocket.send", "text": text_data})
elif bytes_data is not None:
super().send({"type": "websocket.send", "bytes": bytes_data})
else:
raise ValueError("You must pass one of bytes_data or text_data")
if close:
self.close(close)
def close(self, code=None):
if code is not None and code is not True:
super().send({"type": "websocket.close", "code": code})
else:
super().send({"type": "websocket.close"})
def websocket_disconnect(self, message):
try:
for group in self.groups:
async_to_sync(self.channel_layer.group_discard)(
group, self.channel_name
)
except AttributeError:
raise InvalidChannelLayerError(
"BACKEND is unconfigured or doesn't support groups"
)
self.disconnect(message["code"])
raise StopConsumer()
def disconnect(self, code):
pass
class JsonWebsocketConsumer(WebsocketConsumer):
def receive(self, text_data=None, bytes_data=None, **kwargs):
if text_data:
self.receive_json(self.decode_json(text_data), **kwargs)
else:
raise ValueError("No text section for incoming WebSocket frame!")
def receive_json(self, content, **kwargs):
pass
def send_json(self, content, close=False):
super().send(text_data=self.encode_json(content), close=close)
@classmethod
def decode_json(cls, text_data):
return json.loads(text_data)
@classmethod
def encode_json(cls, content):
return json.dumps(content)
class AsyncWebsocketConsumer(AsyncConsumer):
groups = None
def __init__(self, *args, **kwargs):
super().__init__(self,*args, **kwargs)
if self.groups is None:
self.groups = []
async def websocket_connect(self, message):
try:
for group in self.groups:
await self.channel_layer.group_add(group, self.channel_name)
except AttributeError:
raise InvalidChannelLayerError(
"BACKEND is unconfigured or doesn't support groups"
)
try:
await self.connect()
except AcceptConnection:
await self.accept()
except DenyConnection:
await self.close()
async def connect(self):
await self.accept()
async def accept(self, subprotocol=None):
await super().send({"type": "websocket.accept", "subprotocol": subprotocol})
async def websocket_receive(self, message):
if "text" in message:
await self.receive(text_data=message["text"])
else:
await self.receive(bytes_data=message["bytes"])
async def receive(self, text_data=None, bytes_data=None):
pass
async def send(self, text_data=None, bytes_data=None, close=False):
if text_data is not None:
await super().send({"type": "websocket.send", "text": text_data})
elif bytes_data is not None:
await super().send({"type": "websocket.send", "bytes": bytes_data})
else:
raise ValueError("You must pass one of bytes_data or text_data")
if close:
await self.close(close)
async def close(self, code=None):
if code is not None and code is not True:
await super().send({"type": "websocket.close", "code": code})
else:
await super().send({"type": "websocket.close"})
async def websocket_disconnect(self, message):
try:
for group in self.groups:
await self.channel_layer.group_discard(group, self.channel_name)
except AttributeError:
raise InvalidChannelLayerError(
"BACKEND is unconfigured or doesn't support groups"
)
await self.disconnect(message["code"])
raise StopConsumer()
async def disconnect(self, code):
pass
class AsyncJsonWebsocketConsumer(AsyncWebsocketConsumer):
async def receive(self, text_data=None, bytes_data=None, **kwargs):
if text_data:
await self.receive_json(await self.decode_json(text_data), **kwargs)
else:
raise ValueError("No text section for incoming WebSocket frame!")
async def receive_json(self, content, **kwargs):
pass
async def send_json(self, content, close=False):
"""
Encode the given content as JSON and send it to the client.
"""
await super().send(text_data=await self.encode_json(content), close=close)
@classmethod
async def decode_json(cls, text_data):
return json.loads(text_data)
@classmethod
async def encode_json(cls, content):
return json.dumps(content)
settings.py:
ASGI_APPLICATION = 'brain.routing.application'
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
},
},
}
当我尝试检查浏览器页面时出现一个错误
任何帮助将不胜感激。提前谢谢你
【问题讨论】:
-
是的,但是我还没有构建自定义 websockets。它在频道包中。我已经从问题中删除了 websockets.py。因为它只是显示在终端中。查看问题中的屏幕截图
-
请不要将错误发布为截图。您可以而且应该从终端复制粘贴。
-
@AKX,我会上传
-
欢迎来到 SO @developer !如果您能以某种方式生成minimal reproducible example,它也可能会有所帮助
-
@AjayLingayat,我上传了但我认为这是默认下载的频道。
标签: python python-3.x django django-rest-framework channel