WebSocket:binaryType 属性
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
备注: 此特性在 Web Worker 中可用。
WebSocket.binaryType
属性用于控制通过 WebSocket 连接接收的二进制数据类型。
值
一个字符串:
"blob"
-
使用
Blob
对象处理二进制数据。这是默认值。 "arraybuffer"
-
使用
ArrayBuffer
对象处理二进制数据。
示例
js
// 创建 WebSocket 连接。
const socket = new WebSocket("ws://localhost:8080");
// 将二进制类型从“blob”更改为“arraybuffer”
socket.binaryType = "arraybuffer";
// 监听消息
socket.addEventListener("message", (event) => {
if (event.data instanceof ArrayBuffer) {
// 二进制帧
const view = new DataView(event.data);
console.log(view.getInt32(0));
} else {
// 文本帧
console.log(event.data);
}
});
规范
Specification |
---|
WebSockets # ref-for-dom-websocket-binarytype① |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
binaryType |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.