WebSocket

You’re reading the English version of this content since no translation exists yet for this locale. Help us translate this article!

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

WebSocket 객체는 서버와의 WebSocket 연결을 생성하고 관리할 수 있는 API 들을 제공합니다. 이는 데이터를 전송하거나 주고 받는 등의 API 들을 포함합니다.

WebSocket 생성자는 하나의 필수 파라미터와, 하나의 옵셔널 파라미터를 받습니다:

WebSocket WebSocket(
  in DOMString url,
  in optional DOMString protocols
);

WebSocket WebSocket(
  in DOMString url,
  in optional DOMString[] protocols
);
url
연결할 URL 주소입니다; 웹소켓 서버가 응답할 수 있는 위치의 주소이여야 합니다.
protocols Optional
단일 프로토콜 문자열, 또는 프로토콜 문자열의 배열. 이 문자열들은 서브 프로토콜을 지정하는데 사용됩니다. 이를 통해 하나의 웹소켓 서버가 여러개의 웹 소켓 서브 프로토콜을 구현할 수 있도록 해줍니다. (예를 들어, 하나의 서버가 두 개 이상의 커뮤니케이션 방식을 가지고 싶도록 하고 싶을 때). 만약 지정하지 않으면 빈 문자열을 넣은 것으로 간주됩니다.

이 생성자는 예외가 발생할 수 있습니다:

SECURITY_ERR
The port to which the connection is being attempted is being blocked.

메소드 오버뷰

void close(in optional unsigned long code, in optional DOMString reason);
void send(in DOMString data);

어트리뷰트

Attribute Type Description
binaryType DOMString A string indicating the type of binary data being transmitted by the connection. This should be either "blob" if DOM Blob objects are being used or "arraybuffer" if ArrayBuffer objects are being used.
bufferedAmount unsigned long The number of bytes of data that have been queued using calls to send() but not yet transmitted to the network. This value does not reset to zero when the connection is closed; if you keep calling send(), this will continue to climb. Read only
extensions DOMString The extensions selected by the server. This is currently only the empty string or a list of extensions as negotiated by the connection.
onclose EventListener WebSocket 인터페이스의 연결상태가 readyState 에서CLOSED 로 바뀌었을 때 호출되는 이벤트 리스너입니다. 이 이벤트 리스너는 "close"라는 이름의 CloseEvent를 받습니다.
onerror EventListener "error" 라는 이름의 이벤트가 발생하면 처리할 핸들러입니다. 이는 에러가 발생하는 상황에 호출됩니다.
onmessage EventListener "message" 이름의 MessageEvent 이벤트가 발생하면 처리할 핸들러입니다. 이는 서버로부터 메세지가 도착했을 때 호출됩니다.
onopen EventListener WebSocket 인터페이스의 연결상태가 readyState 에서 OPEN으로 바뀌었을 때 호출되는 이벤트 리스너입니다. ; 연결 상태가 OPEN으로 바뀌었다는 말은 데이터를 주고 받을 준비가 되었다는 뜻입니다. 이 리스너가 처리하는 이벤트는 "open"이라는 이벤트 하나입니다.
protocol DOMString 서버에 의해 선택된 서브 프로토콜을 가리킵니다; 이 값은 객체를 생성하면서 protocols 파라미터에 전달했던 값 들 중 하나입니다.
readyState unsigned short 연결의 현재 상태입니다; 값은 Ready state constants 중에 하나입니다. 읽기 전용.
url DOMString 생성자에 의해 해석된 URL입니다. 이는 항상 절대 주소를 가리킵니다. 읽기 전용.

상수

Ready state constants

아래의 값들은 readyState 어트리뷰트가 웹소켓의 연결 상태를 나타내기 위해서 사용됩니다.

Constant Value Description
CONNECTING 0 연결이 수립되지 않은 상태입니다.
OPEN 1 연결이 수립되어 데이터가 오고갈 수 있는 상태입니다.
CLOSING 2 연결이 닫히는 중 입니다.
CLOSED 3 연결이 종료되었거나, 연결에 실패한 경우입니다.

메소드

close()

맺어진 연결, 또는 연결 시도를 종료합니다. 이미 종료된 경우에는 아무 동작도 하지 않습니다.

void close(
  in optional unsigned short code,
  in optional DOMString reason
);

Parameters

code Optional
연결이 종료되는 이유를 가리키는 숫자 값입니다. 지정되지 않을 경우 기본값은 1000으로 간주됩니다. (일반적인 경우의 "transaction complete" 종료를 나타내는 값).  허용되는 값들은 CloseEvent 페이지의  list of status codes 를 참고하세요.
reason Optional
연결이 왜 종료되는지를 사람이 읽을 수 있도록 나타내는 문자열입니다. 이 문자열은 UTF-8 포멧이며, 123 바이트를 넘을 수 없습니다. (글자 수 아님).

Exceptions thrown

INVALID_ACCESS_ERR
잘못된 code 값을 지정했습니다.
SYNTAX_ERR
reason 값이 너무 길거나, 짝을 이루지 못하는 서로게이트 문자가 있습니다.

Note: In Gecko, this method didn't support any parameters prior to Gecko 8.0 (Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5).

send()

웹소켓 연결을 통해 데이터를 서버로 전송합니다.

void send(
  in DOMString data
);

void send(
  in ArrayBuffer data
);

void send(
  in Blob data
); 

Parameters

data
서버로 전송할 텍스트 메세지입니다.

Exceptions thrown

INVALID_STATE_ERR
연결이 현재 OPEN 상태가 아닙니다.
SYNTAX_ERR
data 값에 짝을 이루지 못하는 서로게이트 문자가 있습니다.

Note: Gecko's implementation of the send() method differs somewhat from the specification in Gecko 6.0; Gecko returns a boolean indicating whether or not the connection is still open (and, by extension, that the data was successfully queued or transmitted); this is corrected in Gecko 8.0.

As of Gecko 11.0, support for ArrayBuffer is implemented but not Blob data types.

Specifications

Specification Status Comment
Unknown
The definition of 'WebSocket' in that specification.
Unknown Initial definition

브라우저 호환성

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
WebSocketChrome Full support YesEdge Full support YesFirefox Full support 11
Notes
Full support 11
Notes
Notes See bug 695635.
No support 7 — 11
Prefixed Notes
Prefixed Implemented with the vendor prefix: Moz
Notes Message size limited to 16 MB (see bug 711205).
No support 4 — 6
Notes
Notes Message size limited to 16 MB (see bug 711205).
IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 14
Notes
Full support 14
Notes
Notes See bug 695635.
No support 7 — 14
Prefixed Notes
Prefixed Implemented with the vendor prefix: Moz
Notes Message size limited to 16 MB (see bug 711205).
Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
WebSocket() constructorChrome Full support YesEdge ? Firefox Full support 7
Full support 7
No support 4 — 7
Notes
Notes Parameter protocols not supported.
IE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 7Opera Android ? Safari iOS ? Samsung Internet Android ?
binaryTypeChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
bufferedAmountChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
closeChrome Full support YesEdge Full support 12Firefox Full support 8
Full support 8
No support 4 — 8
Notes
Notes Parameters not supported, see bug 674716.
IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
close eventChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android ?
error eventChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android ?
extensionsChrome Full support YesEdge Full support 12Firefox Full support 8IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 8Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
message eventChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android ?
oncloseChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
onerrorChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
onmessageChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
onopenChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
open eventChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android ? Safari iOS ? Samsung Internet Android ?
protocolChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
Supports protocol as specified by RFC 6455Chrome Full support 16Edge Full support 12Firefox Full support 11IE Full support 10Opera Full support 15Safari Full support 6WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 14Opera Android Full support 14Safari iOS Full support 6Samsung Internet Android ?
readyStateChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
sendChrome Full support YesEdge Full support 12Firefox Full support 18
Notes
Full support 18
Notes
Notes See bug 775368.
No support 11 — 18
Notes
Notes Only parameter of type ArrayBuffer and String supported.
No support 8 — 11
Notes
Notes Only parameter of type String supported.
No support 4 — 8
Notes
Notes Only parameter of type String supported. Returns boolean.
IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
urlChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android ?
Available in workersChrome Full support YesEdge Full support YesFirefox Full support 37IE ? Opera ? Safari ? WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 37Opera Android ? Safari iOS ? Samsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

같이 보기