django-channels – 组发送不调用在”type”中定义的函数。

huangapple go评论138阅读模式
英文:

django-channels - group send does not call the function defined in "type"

问题

我有这个Consumer:

class TableConsumer(AsyncWebsocketConsumer):            
    async def connect(self):
        self.table_name = f'table_test'
        self.channel_name = f'channel_test'

        await self.accept()
        await self.channel_layer.group_add(
            self.table_name, self.channel_name
        )
        await self.manage_new_game_video()
            
    async def manage_new_game_video(self):                
        await this.channel_layer.group_send(
            self.table_name, {"type": "chat_message", "message": "message"}
        )

    # Receive message from room group
    async def chat_message(self, event):
        print("test")
        message = event["message"]

        # Send message to WebSocket
        await self.send(text_data=json.dumps({"message": message}))

由于某种原因,print语句从未被执行,我不明白为什么。

为什么会发生这种情况?

谢谢。

英文:

I have this Consumer:

class TableConsumer(AsyncWebsocketConsumer):            
    async def connect(self):
        self.table_name = f'table_test'
        self.channel_name = f'channel_test'

        await self.accept()
        await self.channel_layer.group_add(
            self.table_name, self.channel_name
        )
        await self.manage_new_game_video()
            
    async def manage_new_game_video(self):                
        await self.channel_layer.group_send(
            self.table_name, {"type": "chat_message", "message": "message"}
        )

    # Receive message from room group
    async def chat_message(self, event):
        print("test")
        message = event["message"]

        # Send message to WebSocket
        await self.send(text_data=json.dumps({"message": message}))

for some reason, the print statement is never executed and I don't understand why.

Why can that happen?

Thanks.

答案1

得分: 1

因为我重写了self.channel_name,所以它不起作用。

英文:

It is not working because I overrided self.channel_name.

huangapple
  • 本文由 发表于 2023年3月7日 19:09:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定