这里https://developer.mozilla.org/en/WebSockets/WebSockets_reference/MessageEvent它声明属性数据的类型为 DOMString| Blob |数组缓冲区。我如何告诉它我想要哪种类型?或者我怎么知道我得到的是哪种类型?
最佳答案
服务器可以发送的两种类型的帧是文本帧和二进制帧 ( 5.2 )。 ws.binaryType
允许您定义要以哪种格式获取二进制数据。
- 二进制数据:取决于设置为
arraybuffer
或blob
的binaryType
- 文本数据:字符串
要确定类型,您可以使用:
e.data ArrayBuffer 实例
Blob 的 e.data 实例
typeof e.data ===“字符串”
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'sdata
attribute to a newBlob
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'sdata
attribute to a new read-onlyArrayBuffer
object whose contents aredata
.
关于javascript - 如何判断websocket onmessage参数的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9462879/