Django websockets 和通道 group_send 不起作用
我开始研究 websockets,并且希望实现 Web 客户端(Angular 13)和 Python 应用程序之间的实时通信。作为后端,我使用了带有实现的 websockets 和通道的 Django
因为我遇到了问题,所以我尽可能地简化了代码,所以很多值都被硬编码以使其工作。 (现在是一个 WebClient 和一个 Bot)
我实现了 2 个消费者。 Bot 和 webclient:
class BotConsumer(AsyncJsonWebsocketConsumer):
async def connect(self):
await self.accept()
await self.channel_layer.group_add("bot","dev")
async def disconnect(self, close_code):
await self.channel_layer.group_discard("bot","dev")
async def receive(self, text_data):
await self.channel_layer.group_send("dev",{"type": "recv","text": "test"})
print("Data sent to group")
class WebClientConsumer(AsyncJsonWebsocketConsumer):
async def connect(self):
await self.accept()
await self.channel_layer.group_add("client","dev")
async def recv(self,event):
print("Consumer received smthing from channels")
async def disconnect(self, close_code):
await self.channel_layer.group_discard("client","dev")
async def receive(self, text_data):
print("Smthing received")
我在Setting.py 中设置的channel_layer
CHANNEL_LAYERS = {
'default': {
# 'BACKEND': 'channels_redis.core.RedisChannelLayer',
# 'CONFIG': {
# "hosts": [('192.168.1.10', 6379)],
# },
"BACKEND": "channels.layers.InMemoryChannelLayer"
}}
我尝试了本地服务器运行的Redis 和内存通道
我的websocket 的routing.py:
from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r'live/bot', consumers.BotConsumer.as_asgi()),
re_path(r'live/webclient', consumers.WebClientConsumer.as_asgi()),
]
我的行为:
Django 日志:
HTTP POST /core/UpdateAvailableServers 200 [0.07, 127.0.0.1:50494]
HTTP POST /core/GetPostInfo 200 [0.02, 127.0.0.1:50497]
**WebSocket HANDSHAKING /live/bot [127.0.0.1:50498]**
**WebSocket CONNECT /live/bot [127.0.0.1:50498]**
HTTP POST /core/GetStatus 200 [0.00, 127.0.0.1:50501]
HTTP POST /core/GetPostInfo 200 [0.02, 127.0.0.1:50504]
HTTP POST /core/GetBots 200 [0.00, 127.0.0.1:50524]
**WebSocket HANDSHAKING /live/webclient [127.0.0.1:50527]**
**WebSocket CONNECT /live/webclient [127.0.0.1:50527]**
HTTP POST /core/GetStatus 200 [0.01, 127.0.0.1:50528]
HTTP POST /core/UpdateAvailableServers 200 [0.02, 127.0.0.1:50531]
HTTP POST /core/GetPostInfo 200 [0.02, 127.0.0.1:50534]
HTTP POST /core/GetStatus 200 [0.00, 127.0.0.1:50537]
HTTP POST /core/GetPostInfo 200 [0.02, 127.0.0.1:50540]
HTTP POST /core/UpdateAvailableServers 200 [0.06, 127.0.0.1:50544]
HTTP POST /core/GetStatus 200 [0.00, 127.0.0.1:50547]
HTTP POST /core/GetPostInfo 200 [0.02, 127.0.0.1:50550]
**Data sent to group**
HTTP POST /core/GetPostInfo 200 [0.00, 127.0.0.1:50554]
HTTP POST /core/GetStatus 200 [0.00, 127.0.0.1:50557]
HTTP POST /core/UpdateAvailableServers 200 [0.06, 127.0.0.1:50560]
HTTP POST /core/GetPostInfo 200 [0.02, 127.0.0.1:50563]
正如您在日志中看到的那样,我从我的 python 应用程序(BOT)和 Angular 应用程序(WebClient)成功获得了 websocket 连接,两者都被添加到 connect 函数的组中。
然后,机器人应用程序将使用 websockets 发送一些数据,并且它们应该发送到整个组,我将得到确认,如日志中所示。
问题是我的网络客户端将完全忽略这一点。 recv 方法永远不会执行,WebClient 使用者也不会显示此操作的任何活动。
我犯了一些明显的错误吗? (如果需要,我可以提供任何其他信息)
I started looking into websockets and I would liek to implement realtime communication between web-client (Angular 13) and Python app. As backend I used Django with implemented websockets and channels
As i am having problem i simplified code as much as i could so a lot of values are hardcoded for a sake to make it work. (Right now it is One WebClient and one Bot)
I implemented 2 Consumers. Bot and webclient:
class BotConsumer(AsyncJsonWebsocketConsumer):
async def connect(self):
await self.accept()
await self.channel_layer.group_add("bot","dev")
async def disconnect(self, close_code):
await self.channel_layer.group_discard("bot","dev")
async def receive(self, text_data):
await self.channel_layer.group_send("dev",{"type": "recv","text": "test"})
print("Data sent to group")
class WebClientConsumer(AsyncJsonWebsocketConsumer):
async def connect(self):
await self.accept()
await self.channel_layer.group_add("client","dev")
async def recv(self,event):
print("Consumer received smthing from channels")
async def disconnect(self, close_code):
await self.channel_layer.group_discard("client","dev")
async def receive(self, text_data):
print("Smthing received")
my channel_layer setup in Setting.py
CHANNEL_LAYERS = {
'default': {
# 'BACKEND': 'channels_redis.core.RedisChannelLayer',
# 'CONFIG': {
# "hosts": [('192.168.1.10', 6379)],
# },
"BACKEND": "channels.layers.InMemoryChannelLayer"
}}
I tried both Redis with local server running and in memory channels
My websocket's routing.py:
from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r'live/bot', consumers.BotConsumer.as_asgi()),
re_path(r'live/webclient', consumers.WebClientConsumer.as_asgi()),
]
My behavior:
Django logs:
HTTP POST /core/UpdateAvailableServers 200 [0.07, 127.0.0.1:50494]
HTTP POST /core/GetPostInfo 200 [0.02, 127.0.0.1:50497]
**WebSocket HANDSHAKING /live/bot [127.0.0.1:50498]**
**WebSocket CONNECT /live/bot [127.0.0.1:50498]**
HTTP POST /core/GetStatus 200 [0.00, 127.0.0.1:50501]
HTTP POST /core/GetPostInfo 200 [0.02, 127.0.0.1:50504]
HTTP POST /core/GetBots 200 [0.00, 127.0.0.1:50524]
**WebSocket HANDSHAKING /live/webclient [127.0.0.1:50527]**
**WebSocket CONNECT /live/webclient [127.0.0.1:50527]**
HTTP POST /core/GetStatus 200 [0.01, 127.0.0.1:50528]
HTTP POST /core/UpdateAvailableServers 200 [0.02, 127.0.0.1:50531]
HTTP POST /core/GetPostInfo 200 [0.02, 127.0.0.1:50534]
HTTP POST /core/GetStatus 200 [0.00, 127.0.0.1:50537]
HTTP POST /core/GetPostInfo 200 [0.02, 127.0.0.1:50540]
HTTP POST /core/UpdateAvailableServers 200 [0.06, 127.0.0.1:50544]
HTTP POST /core/GetStatus 200 [0.00, 127.0.0.1:50547]
HTTP POST /core/GetPostInfo 200 [0.02, 127.0.0.1:50550]
**Data sent to group**
HTTP POST /core/GetPostInfo 200 [0.00, 127.0.0.1:50554]
HTTP POST /core/GetStatus 200 [0.00, 127.0.0.1:50557]
HTTP POST /core/UpdateAvailableServers 200 [0.06, 127.0.0.1:50560]
HTTP POST /core/GetPostInfo 200 [0.02, 127.0.0.1:50563]
As you can see in the log i got successfull websocket connection from my python app (BOT) and also angular app (WebClient) Both are added into group at connect Function.
Bot app will then send some data using websockets and they are suppose to be send to whole group which I will get confirmation as it is shown in log.
Problem is that My webclient will totally ignore this. recv method is never executed nor WebClient consumer will not show any activity on this action.
Is there some obvious mistake I am doing? (I can provide any additional info if needed)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该创建自定义频道。相反,将自定义组分配给通道。
另外,将套接字消息发送到同一通道中的客户端组。
请参阅下面更正后的实现。
You are not supposed to create a custom channel. Assign a custom group to the channel instead.
Also, send the socket message to the client group in the same channel.
See the corrected implementation below.