Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. WebSocket
  4. send()

WebSocket: send() method

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⁩.

Note: This feature is available inWeb Workers.

TheWebSocket.send() method enqueues the specified datato be transmitted to the server over the WebSocket connection, increasing the value ofbufferedAmount by the number of bytes needed to contain the data. If thedata can't be sent (for example, because it needs to be buffered but the buffer isfull), the socket is closed automatically.The browser will throw an exception if you callsend() when the connection is in theCONNECTING state. If you callsend() when the connection is in theCLOSING orCLOSED states, the browser will silently discard the data.

Syntax

js
send(data)

Parameters

data

The data to send to the server. It may be one of the following types:

string

A text string. The string is added to the buffer in UTF-8 format, and the valueofbufferedAmount is increased by the number of bytes required torepresent the UTF-8 string.

ArrayBuffer

You can send the underlying binary data used by a typed array object; its binarydata contents are queued in the buffer, increasing the value ofbufferedAmount by the requisite number of bytes.

Blob

Specifying aBlob enqueues the blob's raw data to be transmitted ina binary frame (theBlob.type is ignored).The value ofbufferedAmount is increased by the byte size of that raw data.

TypedArray or aDataView

You can send anyJavaScript typed array object as a binary frame;its binary data contents are queued in the buffer,increasing the value ofbufferedAmount by the requisite number of bytes.

Return value

None (undefined).

Exceptions

InvalidStateErrorDOMException

Thrown ifWebSocket.readyState isCONNECTING.

Specifications

Specification
WebSockets
# ref-for-dom-websocket-send①

Browser compatibility

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp