WebSocket: Eigenschaft 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.
Hinweis: Diese Funktion ist in Web Workers verfügbar.
Die WebSocket.binaryType
-Eigenschaft steuert den Typ der
binären Daten, die über die WebSocket-Verbindung empfangen werden.
Wert
Ein String:
"blob"
-
Verwenden Sie
Blob
-Objekte für binäre Daten. Dies ist der Standardwert. "arraybuffer"
-
Verwenden Sie
ArrayBuffer
-Objekte für binäre Daten.
Beispiele
js
// Create WebSocket connection.
const socket = new WebSocket("ws://localhost:8080");
// Change binary type from "blob" to "arraybuffer"
socket.binaryType = "arraybuffer";
// Listen for messages
socket.addEventListener("message", (event) => {
if (event.data instanceof ArrayBuffer) {
// binary frame
const view = new DataView(event.data);
console.log(view.getInt32(0));
} else {
// text frame
console.log(event.data);
}
});
Spezifikationen
Specification |
---|
WebSockets # ref-for-dom-websocket-binarytype① |
Browser-Kompatibilität
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.