WebSocket, bir WebSocket sunucusuyla bağlantı kurmak ve bağlantıyı yönetmek için bir uygulama programlama arayüzü (API) sunar.

WebSocket'i kurmak için WebSocket() yapısını kullanın.

Yapılar

WebSocket(url[, protocols])
Yeni oluşturulmuş bir WebSocket objesi verir.

Sabitler

Sabit Değer
WebSocket.CONNECTING 0
WebSocket.OPEN 1
WebSocket.CLOSING 2
WebSocket.CLOSED 3

Özellikler

WebSocket.binaryType
Bağlantıda kurulan ikilik veri tipi.
WebSocket.bufferedAmount Read only
Kuyruktaki değerlerin byte sayıları.
WebSocket.extensions Read only
Sunucu tarafından seçilmiş uzantılar.
WebSocket.onclose
Bağlantı kapandığında çağırılan olay dinleyicisi.
WebSocket.onerror
Hata oluştuğunda çağırılan olay dinleyicisi.
WebSocket.onmessage
Sunucudan mesaj alındığında çağırılan olay dinleyicisi.
WebSocket.onopen
Bağlantı kurulduğunda çağırılan olay dinleyicisi.
WebSocket.protocol Read only
Sunucu tarafından seçilmiş alt-protokol.
WebSocket.readyState Read only
Bağlantının şimdiki durumu.
WebSocket.url Read only
WebSocket'in mutlak URL'si.

Yöntemler

WebSocket.close([code[, reason]])
Bağlantıyı kapatır.
WebSocket.send(data)
İletilmek üzere veriyi kuyruğa ekler.

Örnek

 

// WebSocket bağlantısı kur
const socket = new WebSocket('ws://localhost:8080');

// Bağlantı kurulduğunda
socket.addEventListener('open', function (event) {
    socket.send('Sunucuya bağlanıldı.');
});

// Mesaj alındığında
socket.addEventListener('message', function (event) {
    console.log('Mesaj alındı: ', event.data);
});

// Mesaj gönder
socket.send('Merhaba!');

// Bağlantıyı bitir
socket.close(1000, 'Güle güle.');

 

Belirtimler

Belirtim Durum Yorum
HTML Living Standard
The definition of 'WebSocket' in that specification.
Living Standard İlk tanım

Tarayıcı desteği

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox 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 ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile 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 ? Chrome Android Full support YesEdge Mobile ? Firefox 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 YesEdge Mobile ? Firefox 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 YesEdge Mobile ? Firefox 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 YesEdge Mobile ? Firefox 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 YesEdge Mobile ? Firefox 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 YesEdge Mobile ? Firefox 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 YesEdge Mobile ? Firefox 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 YesEdge Mobile ? Firefox 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 YesEdge Mobile ? Firefox 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 YesEdge Mobile ? Firefox 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 YesEdge Mobile ? Firefox 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 YesEdge Mobile ? Firefox 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 YesEdge Mobile ? Firefox 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 YesEdge Mobile ? Firefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung 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 YesEdge Mobile ? Firefox 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 YesEdge Mobile ? Firefox 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 YesEdge Mobile ? Firefox 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 18Edge Mobile Full support 12Firefox Android Full support 14Opera Android Full support 14Safari iOS Full support 6Samsung Internet Android ?
Available in workersChrome Full support YesEdge Full support YesFirefox Full support 37IE ? Opera ? Safari ? WebView Android Full support YesChrome Android Full support YesEdge Mobile 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.

Ayrıca bakınız

Document Tags and Contributors

Contributors to this page: mdnwebdocs-bot, Yukani
Last updated by: mdnwebdocs-bot,