【发布时间】:2021-07-31 18:34:43
【问题描述】:
我按照 Django Channels 文档教程创建了一个聊天应用程序。现在我想从另一个应用程序的views.py 发送一条消息到consumers.py 之外。但是,每次我触发该函数时,消费者类中的 chat_message 函数都不会触发。 chat_message 函数在消费者范围内正常运行。以下是我使用 websockets 打开聊天页面的浏览器选项卡,然后从其他浏览器登录的回溯。 chat_message 函数中的“ccc”没有打印出来。关于我在使用消费者之外的渠道时错过了什么有什么想法吗?
users.views.py中的部分登录功能
...
channel_layer = get_channel_layer()
print("channel_layer: "+ str(channel_layer))
async_to_sync(channel_layer.group_send(
"chat_hi", {
"type": "chat.message",
"message": "from views",
}))
...
追溯
...
WebSocket HANDSHAKING /ws/chat/abc/ [127.0.0.1:56478]
chat_abc
WebSocket CONNECT /ws/chat/abc/ [127.0.0.1:56478]
channel_layer: RedisChannelLayer(hosts=[{'address': ('127.0.0.1', 6379)}])
HTTP POST /accounts/login/ 302 [0.97, 127.0.0.1:54362]
...
chat.consumers.py
class OnlineFriends(AsyncWebsocketConsumer):
async def connect(self):
self.room_name = self.scope['url_route']['kwargs']['room_name']
self.room_group_name = 'chat_%s' % self.room_name
print(self.room_group_name)
await self.channel_layer.group_add(
self.room_group_name,
self.channel_name
)
await self.accept()
async def disconnect(self, close_code):
print("aa")
await self.channel_layer.group_discard(
self.room_group_name,
self.channel_name
)
async def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']
# Send message to room group
await self.channel_layer.group_send(
self.room_group_name,
{
'type': 'chat_message',
'message': message
}
)
async def chat_message(self, event):
print("ccc")
message = event['message']
# Send message to WebSocket
await self.send(text_data=json.dumps({
'message': message
}))
chat.routing.py
websocket_urlpatterns = [
re_path(r'ws/chat/(?P<room_name>\w+)/$', consumers.OnlineFriends.as_asgi()),
]
asgi.py
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "music.settings")
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
chat.routing.websocket_urlpatterns,
)
),
})
settings.py
ASGI_APPLICATION = "music.asgi.application"
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
},
},
}
【问题讨论】: