You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With the standard WebSocket API, the events you receive from the WebSocket instance are typically:
27
+
```bash
28
+
onopen
29
+
onmessage
30
+
onmessage
31
+
onmessage
32
+
onclose // At this point the WebSocket instance is dead.
33
+
```
34
+
With a ReconnectingWebSocket, after an onclose event is called it will automatically attempt to reconnect. In addition, a connection is attempted repeatedly (with a small pause) until it succeeds. So the events you receive may look something more like:
35
+
```bash
36
+
onopen
37
+
onmessage
38
+
onmessage
39
+
onmessage
40
+
onclose
41
+
// ReconnectingWebSocket attempts to reconnect
42
+
onopen
43
+
onmessage
44
+
onmessage
45
+
onmessage
46
+
onclose
47
+
// ReconnectingWebSocket attempts to reconnect
48
+
onopen
49
+
onmessage
50
+
onmessage
51
+
onmessage
52
+
onclose
53
+
```
54
+
This is all handled automatically for you by the library.
24
55
25
56
## Parameters
26
57
@@ -72,30 +103,54 @@ var socket = new ReconnectingWebSocket(url, null, {reconnectInterval: 3000});
72
103
- Accepts `integer` or `null`.
73
104
- Default: `null`
74
105
75
-
### own options
76
-
77
-
#### `unrecognized`
106
+
#### `origin`
78
107
- Preserve deprecated backwards compatibility for the `origin` option
79
108
80
109
#### `headers`
81
110
- Specifying `origin` as a WebSocket connection option is deprecated. Include it under `headers` instead
See the detail in [react-native/WebSocket.js](https://github.com/facebook/react-native/blob/master/Libraries/WebSocket/WebSocket.js)
121
+
#### `ws.close(code?: number, reason?: string)`
122
+
123
+
- Closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED, this method does nothing.
124
+
-`code` is optional the closing code (default value 1000). [https://tools.ietf.org/html/rfc6455#section-7.4.1](https://tools.ietf.org/html/rfc6455#section-7.4.1)
125
+
-`reason` is the optional reason that the socket is being closed. [https://tools.ietf.org/html/rfc6455#section-7.1.6](https://tools.ietf.org/html/rfc6455#section-7.1.6)
- Transmits data to the server over the WebSocket connection.
130
+
- Accepts @param data a text string, ArrayBuffer, ArrayBufferView or Blob
131
+
132
+
#### `ws.ping()`
93
133
94
-
## How to add heartbeat?
134
+
- Sending websocket ping/pong frame.
135
+
- Some servers do not support it and need to be implemented manually, like `How to add heartbeat?`
136
+
137
+
#### `ws.reconnect()`
138
+
139
+
- Additional public API method to refresh the connection if still open (close, re-open).
140
+
- For example, if the app suspects bad data / missed heart beats, it can try to refresh.
141
+
142
+
143
+
See the more detail in [[react-native/WebSocket.js@3982a2c6]](https://github.com/facebook/react-native/blob/3982a2c6bd116a6dcc6ee6889e4a246b710b70a7/Libraries/WebSocket/WebSocket.js)
0 commit comments