|
| 1 | +WebSocket : EventTarget |
| 2 | + |
| 3 | +WebSockets are a persistent connection to a server that allows sending and receiving data. |
| 4 | + |
| 5 | +Spec: |
| 6 | +https://html.spec.whatwg.org/multipage/web-sockets.html#the-websocket-interface |
| 7 | + |
| 8 | +---- |
| 9 | +new WebSocket(url : String) : WebSocket |
| 10 | + |
| 11 | +Constructs a new WebSocket connection to **url**. |
| 12 | + |
| 13 | +<htmlexample> |
| 14 | +<script> |
| 15 | + // Connect to a WebSocket that echos back the messages it receives. |
| 16 | + var ws = new WebSocket('wss://echo.websocket.org/'); |
| 17 | + ws.onopen = function(e) { |
| 18 | + console.log('WebSocket Opened. Sending "Hello"'); |
| 19 | + ws.send('Hello'); |
| 20 | + }; |
| 21 | + |
| 22 | + ws.onmessage = function(e) { |
| 23 | + console.log('Received:', e.data); |
| 24 | + }; |
| 25 | +</script> |
| 26 | +</htmlexample> |
| 27 | + |
| 28 | +---- |
| 29 | +new WebSocket(url : String, protocol : String) : WebSocket |
| 30 | + |
| 31 | +Same as %%#new_WebSocket_String_Iterable|**new WebSocket(url, [protocol])**%%. |
| 32 | + |
| 33 | +---- |
| 34 | +new WebSocket(url : String, protocols : Iterable<String>) : WebSocket |
| 35 | + |
| 36 | + |
| 37 | +---- |
| 38 | +instance.url : String |
| 39 | + |
| 40 | +The url passed to the WebSocket constructor. |
| 41 | + |
| 42 | +<htmlexample> |
| 43 | +<script> |
| 44 | + // Connect to a WebSocket that echos back the messages it receives. |
| 45 | + var ws = new WebSocket('wss://echo.websocket.org/'); |
| 46 | + console.log(ws.url); |
| 47 | +</script> |
| 48 | +</htmlexample> |
| 49 | + |
| 50 | +ReadOnly: |
| 51 | +true |
| 52 | + |
| 53 | +---- |
| 54 | +instance.readyState : Number |
| 55 | + |
| 56 | +One of %%#CONNECTING|**WebSocket.CONNECTING**%%, |
| 57 | +%%#OPEN|**WebSocket.OPEN**%%, |
| 58 | +%%#CLOSING|**WebSocket.CLOSING**%%, or |
| 59 | +%%#CLOSED|**WebSocket.CLOSED**%%. |
| 60 | + |
| 61 | +<htmlexample> |
| 62 | +<button>Close</button> |
| 63 | +<script> |
| 64 | + // Connect to a WebSocket that echos back the messages it receives. |
| 65 | + var ws = new WebSocket('wss://echo.websocket.org/'); |
| 66 | + |
| 67 | + var readyStateNames = { |
| 68 | + [WebSocket.CONNECTING]: 'CONNECTING', |
| 69 | + [WebSocket.OPEN]: 'OPEN', |
| 70 | + [WebSocket.CLOSING]: 'CLOSING', |
| 71 | + [WebSocket.CLOSED]: 'CLOSED', |
| 72 | + }; |
| 73 | + |
| 74 | + var logReadyState = function() { |
| 75 | + console.log(readyStateNames[ws.readyState]); |
| 76 | + }; |
| 77 | + |
| 78 | + logReadyState(); |
| 79 | + ws.onopen = logReadyState; |
| 80 | + ws.onclose = logReadyState; |
| 81 | + document.querySelector('button').onclick = function() { |
| 82 | + ws.close(); |
| 83 | + logReadyState(); |
| 84 | + }; |
| 85 | +</script> |
| 86 | +</htmlexample> |
| 87 | + |
| 88 | +ReadOnly: |
| 89 | +true |
| 90 | + |
| 91 | +---- |
| 92 | +instance.bufferedAmount : Number |
| 93 | + |
| 94 | +ReadOnly: |
| 95 | +true |
| 96 | + |
| 97 | +---- |
| 98 | +instance.extensions : String |
| 99 | + |
| 100 | +ReadOnly: |
| 101 | +true |
| 102 | + |
| 103 | +---- |
| 104 | +instance.protocol : String |
| 105 | + |
| 106 | +ReadOnly: |
| 107 | +true |
| 108 | + |
| 109 | +---- |
| 110 | +instance.binaryType : String |
| 111 | + |
| 112 | +Set to either **'arraybuffer'** or **'blob'** to choose if the %%MessageEvent#data|MessageEvent.data%% |
| 113 | +property will be an %%/ArrayBuffer|ArrayBuffer%% or a %%/Blob|Blob%% for binary |
| 114 | +data received by the WebSocket. |
| 115 | + |
| 116 | +<htmlexample> |
| 117 | +<script> |
| 118 | + var testBinaryType = function(binaryType) { |
| 119 | + // Connect to a WebSocket that echos back the messages it receives. |
| 120 | + var ws = new WebSocket('wss://echo.websocket.org/'); |
| 121 | + ws.binaryType = binaryType; |
| 122 | + ws.onopen = function(e) { |
| 123 | + ws.send(new Uint32Array([1, 2, 3])); |
| 124 | + }; |
| 125 | + |
| 126 | + ws.onmessage = function(e) { |
| 127 | + // The type of the data will match the specified binaryType |
| 128 | + console.log(binaryType + ':', e.data); |
| 129 | + }; |
| 130 | + }; |
| 131 | + |
| 132 | + testBinaryType('arraybuffer'); |
| 133 | + testBinaryType('blob'); |
| 134 | +</script> |
| 135 | +</htmlexample> |
| 136 | + |
| 137 | +---- |
| 138 | +prototype.close([code : Number, [reason : String]]) : undefined |
| 139 | + |
| 140 | +Closes the WebSocket connection. |
| 141 | + |
| 142 | +---- |
| 143 | +prototype.send(data : ArrayBuffer) : undefined |
| 144 | + |
| 145 | +Sends **data** to the server. |
| 146 | + |
| 147 | +---- |
| 148 | +prototype.send(data : ArrayBufferView) : undefined |
| 149 | + |
| 150 | +Sends **data** to the server. |
| 151 | + |
| 152 | +---- |
| 153 | +prototype.send(data : Blob) : undefined |
| 154 | + |
| 155 | +Sends **data** to the server. |
| 156 | + |
| 157 | +---- |
| 158 | +prototype.send(data : String) : undefined |
| 159 | + |
| 160 | +Sends **data** to the server. |
| 161 | + |
| 162 | +---- |
| 163 | +CONNECTING : Number |
| 164 | + |
| 165 | +The value of %%#readyState|readyState%% after constructing the WebSocket and before |
| 166 | +%%#onopen|onopen%% fires. |
| 167 | + |
| 168 | +ReadOnly: |
| 169 | +true |
| 170 | + |
| 171 | +Value: |
| 172 | +0 |
| 173 | + |
| 174 | +---- |
| 175 | +OPEN : Number |
| 176 | + |
| 177 | +The value of %%#readyState|readyState%% after %%#onopen|onopen%% fires. |
| 178 | + |
| 179 | +ReadOnly: |
| 180 | +true |
| 181 | + |
| 182 | +Value: |
| 183 | +1 |
| 184 | + |
| 185 | +---- |
| 186 | +CLOSING : Number |
| 187 | + |
| 188 | +The value of %%#readyState|readyState%% after calling %%#close|**close()**%% and before |
| 189 | +%%#onclose|onclose%% fires. |
| 190 | + |
| 191 | +ReadOnly: |
| 192 | +true |
| 193 | + |
| 194 | +Value: |
| 195 | +2 |
| 196 | + |
| 197 | +---- |
| 198 | +CLOSED : Number |
| 199 | + |
| 200 | +The value of %%#readyState|readyState%% after %%#onclose|onclose%% fires. |
| 201 | + |
| 202 | +ReadOnly: |
| 203 | +true |
| 204 | + |
| 205 | +Value: |
| 206 | +3 |
| 207 | + |
| 208 | +---- |
| 209 | +event.open : listener(event : Event) : undefined |
| 210 | + |
| 211 | +---- |
| 212 | +event.error : listener(event : ErrorEvent) : undefined |
| 213 | + |
| 214 | +---- |
| 215 | +event.close : listener(event : CloseEvent) : undefined |
| 216 | + |
| 217 | +---- |
| 218 | +event.message : listener(event : MessageEvent) : undefined |
0 commit comments