我一直在努力了解 Django 的 channel ,但我无法将消息发送到我的 websocket。
这是我的consumers.py
import logging
from django.contrib.sites.models import Site
from django.utils import timezone
from channels import Group
from .models import *
import json
def send_free(message):
try:
pi = PInformation.objects.get(
pk=message.content.get('pk'),
)
except Parkplatzinformationen.DoesNotExist:
logging.error("PI not found!")
return
try:
message.reply_channel.send({
"text": 1,
})
except:
logging.exception('Problem sending %s' % (pi.name))
我的routing.py
from channels.routing import route
from RESTAPI.consumers import send_free
channel_routing = [
route('send-free',send_free),
]
我收到错误 AttributeError: 'NoneType' object has no attribute 'send'
。但是它确实获得了 PInformation 对象,所以它确实工作了一点。我在保存对象后立即调用它。
你能给我一些提示吗? The Getting Started guide像我尝试的那样使用它。
最佳答案
我假设您从这样的 View 中调用“send-free”
...
Channel('send-free').send({'message': 'your message'})
然后 send_free
没有 message.reply_channel
...
换句话说,一旦客户端将 WebSocket 数据包发送给我们
,消息就会从中获取 reply_channel
属性。这将用于将消息回复给客户端...(可能到前端)
所以你真的要发送消息...?然后再次使用消费者发送...
关于python - django channels message.reply_channel 没有属性发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38554631/