javascript - 如何判断websocket onmessage参数的类型?

标签 javascript websocket blob arraybuffer

这里https://developer.mozilla.org/en/WebSockets/WebSockets_reference/MessageEvent它声明属性数据的类型为 DOMString| Blob |数组缓冲区。我如何告诉它我想要哪种类型?或者我怎么知道我得到的是哪种类型?

最佳答案

服务器可以发送的两种类型的帧是文本帧和二进制帧 ( 5.2 )。 ws.binaryType允许您定义要以哪种格式获取二进制数据。

  • 二进制数据:取决于设置为 arraybufferblobbinaryType
  • 文本数据:字符串

要确定类型,您可以使用:

  • e.data ArrayBuffer 实例
  • Blob 的 e.data 实例
  • typeof e.data ===“字符串”

Reference :

4. If type indicates that the data is Text, then initialize event's data attribute to data.

If type indicates that the data is Binary, and binaryType is set to "blob", then initialize event's data attribute to a new Blob object that represents data as its raw data.

If type indicates that the data is Binary, and binaryType is set to "arraybuffer", then initialize event's data attribute to a new read-only ArrayBuffer object whose contents are data.

关于javascript - 如何判断websocket onmessage参数的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9462879/

相关文章:

javascript - 简化这个 node.js 函数的调用方式

javascript - 识别特定 DOM 对象是否实际上是 slider 对象

javascript - Node js + Sails js + websocket 应用程序每隔几个小时崩溃一次

javascript - 何时使用 Blob 类型从 API 获取图像?

database - 如何将 BLOB 存储到 sqlite 数据库

javascript - Kineticjs 文本更改

javascript - 使用 ES 模块运行 pm2

javascript - 在 socket.emit() 中传递参数的问题

node.js - Socket.IO 房间不在 Azure 上工作,但在本地工作

git - 为什么 git log --find-object 对于给定的 blob 会获得两个具有不同内容的文件提交?