我正在使用Django Channels
设置Websockets,在触发相关API端点时发送一条测试消息。但是,当我尝试使用Postman测试我的websocket连接时,我成功地连接了,接收了我发送的JSON,并立即断开了连接。这是完整的回溯:
redis.exceptions.ConnectionError: Error 10061 connecting to localhost:49155. No connection could be made because the target machine actively refused it.
HTTP GET /media/profile/2022/06/10/photo-1615672968435-95ade28e0b38.jpg 500 [4.47, 127.0.0.1:51105]
HTTP GET /users/1/ 500 [4.46, 127.0.0.1:51106]
WebSocket HANDSHAKING /stories/notification_testing/ [127.0.0.1:50570]
{'type': 'websocket', 'path': '/stories/notification_testing/', 'raw_path': b'/stories/notification_testing/', 'headers': [(b'sec-websocket-version', b'13'
), (b'sec-websocket-key', b'TcKbMvNYlHQtJdO5efSXDQ=='), (b'connection', b'Upgrade'), (b'upgrade', b'websocket'), (b'sec-websocket-extensions', b'permessage
-deflate; client_max_window_bits'), (b'host', b'127.0.0.1:8000')], 'query_string': b'', 'client': ['127.0.0.1', 50570], 'server': ['127.0.0.1', 8000], 'sub
protocols': [], 'asgi': {'version': '3.0'}, 'cookies': {}, 'session': <django.utils.functional.LazyObject object at 0x00000207DC59A5E0>, 'user': <channels.auth.UserLazyObject object at 0x00000207D8D5A820>, 'path_remaining': '', 'url_route': {'args': (), 'kwargs': {}}}
WebSocket CONNECT /stories/notification_testing/ [127.0.0.1:50570]
Exception inside application: [Errno 11003] getaddrinfo failed
Traceback (most recent call last):
...
File "C:\Users\15512\Desktop\django-project\peerplatform\signup\consumers.py", line 31, in websocket_connect
await self.channel_layer.group_add(self.room_group_name, self.channel_name)
...
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11003] getaddrinfo failed
WebSocket DISCONNECT /stories/notification_testing/ [127.0.0.1:50570]
我正在通过Django映像运行Redis,我已经将Redis缓存用于不同的目的。这就是我如何在Settings.py
中设置频道层的方法。
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("default:redispw@127.0.0.1", 49155)],
},
},
}
这是Docker中我的Redis实例的位置
"LOCATION": "redis://default:redispw@localhost:49153",
我在asgi.py
中为websockets设置了路由器
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'signup.settings')
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
[path('stories/notification_testing/', NotificationConsumer.as_asgi())]
))
})
我在trackback中注意到,其中一个错误似乎引用了我的consumers.py文件中的第31行,以下是来自consumers.py的特定函数:
class NotificationConsumer(AsyncWebsocketConsumer):
async def websocket_connect(self, event):
print(self.scope)
await self.accept()
await self.send(json.dumps({
"type": "websocket.send",
"text": "hello world"
}))
self.room_name='test_consumer'
self.room_group_name='test_consumer_group'
await self.channel_layer.group_add(self.room_group_name, self.channel_name)
self.send({
"type": "websocket.send",
"text": "room made"
})
为了防止这也与此相关,这里是我的models.py:
class Notifications(models.Model):
sender = models.ForeignKey(User, null=True, blank=True,
related_name='sender', on_delete=models.CASCADE)
receiver = models.ForeignKey(User, null=True, blank=True,
related_name='receiver', on_delete=models.CASCADE)
status = models.CharField(max_length=264, null=True, blank=True,
default="unread")
type_of_notification = models.CharField(max_length=264, null=True, blank=True)
我试过什么?
在做了一些研究之后,我尝试将主机更改为: error
redis://default:redispw@localhost, 49153
,all django-channels
& redis-channels
。
发布于 2022-09-22 09:16:29
Django-Channels fails while giving name or service unknown error
我也有同样的问题。Redis URL不正确。在我改变了它之后,它起了作用。
https://stackoverflow.com/questions/72981044
复制相似问题