【发布时间】:2020-01-23 20:17:41
【问题描述】:
我有一个处理 HTTP 请求(webhook)的前端消费者
from django.views.decorators.csrf import csrf_exempt
from channels.generic.http import AsyncHttpConsumer
from django.http import JsonResponse
import json
import myapp.models
import redis
@csrf_exempt
class frontEndConsumer(AsyncHttpConsumer):
async def http_request(self, request):
await self.channel_layer.group_add("link", self.channel_name)
await self.channel_layer.group_send("link",
{
"type": "websocket.reg",
"where": "webhook",
"message": "test message",
})
#channel layer functions
async def webhook_reg(self, event):
print("WH: message from " + event["where"] + ": " + event["message"]
# make response
fulfillmentText = "device connected"
fulfillmentText = {'fulfillmentText': fulfillmentText}
fulfillmentText = json.dumps(fulfillmentText).encode('utf-8')
await self.send_response(200, fulfillmentText,
headers=[(b"Content-Type", b"text/plain"),])
我有一个处理 websocket 连接的后端消费者。
from channels.generic.websocket import AsyncJsonWebsocketConsumer
import redis
import myapp.models
account = redis.Redis(db=0)
class backEndConsumer(AsyncJsonWebsocketConsumer):
async def websocket_connect(self, type):
await self.channel_layer.group_add("link", self.channel_name)
print("channel name: " + self.channel_name)
await self.accept()
#channel layer functions
async def websocket_reg(self, event):
print("WS: message from " + event["where"] + ": " + event["message"])
await self.channel_layer.group_send("link",
{
"type": "webhook.reg",
"where": "websocket",
"message": "msg from websocket",
})
await self.send(event["message"])
我在 procfile 中启动它们:
web: daphne APbackend.asgi:application --port $PORT --bind 0.0.0.0
我正在使用 django 频道让前台和后台相互交谈。在我的代码中,我试图让双方互相发送消息。
- 我会建立 websocket 连接
- 让 webhook 向我的前端发送一个 http 请求。
- 前端会向后端发送消息(通过通道消息传递)
- 后端会向前端发送消息。
- 前端消息将完成 http 请求。
但这不起作用,第 4 步,消息返回到前端,说处理程序丢失。这是为什么呢?
【问题讨论】:
-
csrf_exempt我不认为你可以在 AsyncHttpConsumer 消费者上使用它。你能分享一下你是如何配置你的 ASGI 应用程序的吗?您如何使用frontEndConsumer和backEndConsumer -
application = ProtocolTypeRouter({ "websocket": URLRouter([ path('ws/', backEndConsumer), ]), "http": URLRouter([ path('webhook/', frontEndConsumer), ]),是我配置 asgi 应用程序的方式。http 路由到前端,websocket 路由到后端。我可能会添加一个网页,所以我使用 URLrouter 将其拆分。csrf_exempt 是我因为教程而放入的。我是不确定它的作用细节,或者它是否适用于我。
-
csrf_exempt在频道中无效。