CloseEvent
CloseEvent represents a connection that has ended.
The close event of WebSocket is one. It carries the close code, the reason the other side gave, and whether the connection ended cleanly.
Inheritance: Event → CloseEvent
Constructor
Creates a CloseEvent. The runtime creates one itself when it fires the events below; app code can use it to build an event object and fire it with dispatchEvent() to try out a listener.
Instance properties
Also inherits the properties of Event; the most commonly used are type and target. The properties below are read-only; assigning to them throws TypeError.
| Property | Description |
|---|---|
code read-only | The close code: why the connection ended |
reason read-only | The reason the other side gave |
wasClean read-only | Whether both sides completed the close by the protocol |
These properties live on the prototype, not on the event object itself: Object.keys(event) and { ...event } don't include them, so read them one by one. See Events with fields.
Events that use this class
| Object | Events |
|---|---|
WebSocket | close |
It can't be canceled: cancelable is false, and preventDefault() has no effect.
Examples
const ws = new WebSocket('wss://echo.websocket.org');
ws.addEventListener('open', () => ws.close());
ws.addEventListener('close', (e) => {
console.log(e instanceof CloseEvent, e.code, e.wasClean);
});Effect: the connection closes right after opening, and prints true 1000 true. Not verified in the simulator.
Corresponding iOS API
There is no corresponding system API. It corresponds to CloseEvent in the browser. A WebSocket connection is the system's NSURLSessionWebSocketTask, and its closeCode and closeReason are the code and reason here.