Websocket 发送和接收带有 Base64 带宽问题的图像
我使用Django频道对Web插座进行了编码,现在想添加在聊天中发送图像的可能性(显然是聊天应用程序)。
我现在所做的:
- 用户选择一个图像(前端)
- 用户将其发送到后端(HTTP帖子),
- 我使用Django信号(这是数据库的侦听器)来检测是否有一个带有映像附加映像的新消息
- 我将图像发送回Websocket
以下是问题所在:
- 它很慢(不太明显但很慢)
- 有时会导致怪异的不端行为,
所以现在我想到将图像发送为base64,然后直接将其发送给我任何可能有帮助的HTTP,因为它更快一些。
但是: base64图像大20-30%,我需要在前端提取所有EXIF数据。是否有可能将图像作为文件作为文件发送到后端的文件,或者如果没有的话,有办法绕过更大的尺寸(20-30%),这是一个问题,因为我不能使用太多的带宽: /
其他聊天应用程序如何解决此问题?
I coded a web socket using Django channels and now want to add the possibility to send images over the chat (obviously it is a chat application).
What I did for now:
- The user picks an image (frontend)
- The user sends it to the backend (HTTP POST)
- I use Django signals (it is a listener for the database) to detect if there is a new message with an image appended
- I send the image back over the websocket
Following is the problem:
- It is slow (not too noticeable but quite slow)
- Sometimes it causes weird misbehavior
So now I thought about sending the image as a base64 and then sending it back directly so I never have any HTTP which can be helpful, because it is a little bit faster.
BUT: Base64 images are 20-30 percent bigger and I need to extract all the exif data in the frontend. Is there any possibility to send the images as a file over the websocket to the backend or if not is there a way to get around the quite greater size (20-30 percent), which is a problem because I cannot use too much bandwidth :/
How do other chat application solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WebSocket.send() 接受不同的数据-类型作为参数。您可以通过 WebSocket 双向传输二进制数据。
在此代码段上,您可以了解如何在客户端和服务器端使用它在 JavaScript 中。 Django 通道还支持二进制数据。
您可以将任何信息附加到文件中的二进制数据,并将其作为标头与文件一起发送。
例如,通过使用2(或3)Byte来指示报头的长度。这意味着最多 64KB(或最多 16MB)用于存储文件名和您可能需要的其他信息等信息。最后,这类似于 HTTP POST。
然后,您发送的消息将如下所示:
甚至可以有多个文件:
之后您在服务器端所需要做的就是将组合消息拆分回由长度值定义的部分。
WebSocket.send() accept different data-types as parameter. You can transfer binary data over a WebSocket in both directions.
On this snippet you can see how to make use of it on the client- and server-side in JavaScript. The Django channels also support binary data.
You can attach any information to the binary data from the file and send it together with the file as a header.
For example, by using 2 (or 3) Byte to indicate the length of the header. Which means, up to 64KB (or up to 16MB) for informations like filename and other infos you might need. In the end, this is similar to a HTTP POST.
The message you send would then look like this:
Even multiple files would be possible:
All you have to do on the serverside afterwards is to split the combined message back into it's parts defined by the length values.