[英]How to run Django channels with StreamingHttpResponse in ASGI
I have a simple app that streams images using open cv and the server set in wsgi
.我有一个简单的应用程序,它使用 open cv 和
wsgi
中设置的服务器流式传输图像。 But whenever I introduce Django channels to the picture and change from WSGI
to ASGI
the streaming stops.但是每当我将 Django 个频道引入图片并将其从
WSGI
更改为ASGI
时,流媒体就会停止。 How can I stream images from cv2 and in the same time use Django channels
?我怎样才能 stream 来自 cv2 的图像并同时使用
Django channels
? Thanks you in advance提前谢谢你
My code for streaming:我的流媒体代码:
def camera_feed(request):
stream = CameraStream()
frames = stream.get_frames()
return StreamingHttpResponse(frames, content_type='multipart/x-mixed-replace; boundary=frame')
settings.py:设置.py:
ASGI_APPLICATION = 'photon.asgi.application'
asgi.py asgi.py
application = ProtocolTypeRouter({
'http': get_asgi_application(),
'websocket': AuthMiddlewareStack(URLRouter(ws_urlpatterns))
})
First, we don't need StramingHTTPResponse for sending image data at all...首先,我们根本不需要 StramingHTTPResponse 来发送图像数据......
For this, first, ensure you have a Django version with 3.x and Python 3.7+.为此,首先,确保您拥有 Django 版本 3.x 和 Python 3.7+。
Then, install django-channels third party package.然后,安装django-channels第三方 package。
Configure the ASGI application as follows:配置 ASGI 应用程序如下:
import os
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
import .routing
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
app.routing.websocket_urlpatterns
)
)
})
Then You need to set ASGI_APPLICATION constant in the settings.py file:然后你需要在 settings.py 文件中设置 ASGI_APPLICATION 常量:
ASGI_APPLICATION = "myproject.asgi.application"
After that, just create an async WebSocket consumer in the consumers.py file present in the application:之后,只需在应用程序中的 consumers.py 文件中创建一个异步 WebSocket 消费者:
import json
from channels.generic.websocket import AsyncWebsocketConsumer
class PairingChat(AsyncWebsocketConsumer):
async def connect(self):
self.room_name = self.scope['url_route']['kwargs']['room_name']
self.room_group_name = 'chat_%s' % self.room_name
await self.channel_layer.group_add(
self.room_group_name,
self.channel_name
)
await self.accept()
async def disconnect(self):
await self.channel_layer.group_discard(
self.room_group_name,
self.channel_name
)
# Asyncwebsocket consumer can send any type of data ...
async def receive(self, text_data):
data_json = json.loads(your_data)
message = data_json['message']
await self.channel_layer.group_send(
self.room_group_name,
{
'type': '# send your data from here ...',
'message': message,
'user': self.scope['session']['name']
}
)
async def chat_message(self, event):
message = event['message']
await self.send(data=json.dumps({
'user': event['user'],
'message': message,
}))
Create a route for asyncwebsocket consumers as well...也为 asyncwebsocket 消费者创建一个路由...
from django.urls import re_path
from . import consumers
websocket_urlpatterns = [
re_path(r'ws/chat1/(?P<room_name>\w+)/$', consumers.PairingChat.as_asgi()),
]
Then, just create a WebSocket client in javascript... and you are good to go...然后,只需在 javascript 中创建一个 WebSocket 客户端...您就可以使用 go...
Link for JS Websocket Create: javascript-websocket JS Websocket 的链接创建: javascript-websocket
We also facing same problem when We use channel in setting cv2 streaming stopped当我们在设置 cv2 流停止时使用通道时,我们也面临同样的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.