我需要在单个 Web 套接字消息中发送带有 blob 数据的消息类型。
var socket = new WebSocket('ws://somedomain.com/data');
// just pseudocode
// I know that only DOMString|ArrayBuffer|Blob types are supported
socket.send({ type: 'someMessageType', data: someBlobData });
最好的方法是什么?首先想到的是使用JSON.stringify。但我认为它相当大地增加了消息长度。也许有更好的方法?
最佳答案
您可以使用 FileReader 将 blob 转换为 base64 字符串。这就是你可以在 Dart 中做到的方式。对不起,我不知道JS。
final FileReader reader = new FileReader();
reader.onLoadEnd.listen((e) {
print(reader.result);
socket.send({ type: 'someMessageType', data: reader.result });
});
reader.readAsDataUrl(file);
关于javascript - 网络套接字 : String and Blob in single send message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21572151/