Flask-SocketIO发送图像 [英] Flask-SocketIO send images
问题描述
我目前正在开发一个使用 Flask-SocketIO 通过互联网发送内容的项目,但是遇到了这个问题.
I am currently working on a project that uses Flask-SocketIO to send things over the internet, but I came across this question.
问题:
有什么方法可以在 Flask-SocketIO 中发送图像?我做了一些谷歌搜索,但对我却没有运气.
Is there any way to send images in Flask-SocketIO? I did some googling but no luck for me.
推荐答案
Socket.IO是与数据无关的协议,因此您可以发送任何类型的信息.文本和二进制有效负载均受支持.
Socket.IO is a data agnostic protocol, so you can send any kind of information. Both text and binary payloads are supported.
如果要从服务器发送图像,可以执行以下操作:
If you want to send an image from the server, you can do something like this:
with open('my_image_file.jpg', 'rb') as f:
image_data = f.read()
emit('my-image-event', {'image_data': image_data})
客户端将必须知道您正在发送jpeg数据,Socket.IO协议中没有任何内容可以使发送图像不同于发送文本或其他数据格式.
The client will have to be aware that you are sending jpeg data, there is nothing in the Socket.IO protocol that makes sending images different than sending text or other data formats.
如果使用的是JavaScript客户端,则将以字节数组形式获取数据.其他客户端可能会为此数据选择最合适的二进制表示形式.
If you are using a JavaScript client, you will get the data as a byte array. Other clients may choose the most appropriate binary representation for this data.
这篇关于Flask-SocketIO发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!