forked from socketio/socket.io-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialization.html
More file actions
377 lines (326 loc) · 16.9 KB
/
Copy pathinitialization.html
File metadata and controls
377 lines (326 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from src/site/markdown/initialization.md at 2021-04-27
| Rendered using Apache Maven Fluido Skin 1.9
-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="generator" content="Apache Maven Doxia Site Renderer 1.9.2" />
<title>socket.io-client – Initialization</title>
<link rel="stylesheet" href="./css/apache-maven-fluido-1.9.min.css" />
<link rel="stylesheet" href="./css/site.css" />
<link rel="stylesheet" href="./css/print.css" media="print" />
<script src="./js/apache-maven-fluido-1.9.min.js"></script>
</head>
<body class="topBarDisabled">
<a href="https://github.com/socketio/socket.io-client-java">
<img style="position: absolute; top: 0; right: 0; border: 0; z-index: 10000;"
src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png"
alt="Fork me on GitHub">
</a>
<div class="container-fluid">
<header>
<div id="banner">
<div class="pull-left"><div id="bannerLeft"><h2>Socket.IO Java client</h2>
</div>
</div>
<div class="pull-right"></div>
<div class="clear"><hr/></div>
</div>
<div id="breadcrumbs">
<ul class="breadcrumb">
<li id="publishDate">Last Published: 2021-04-27<span class="divider">|</span>
</li>
<li id="projectVersion">Version: 2.0.2-SNAPSHOT</li>
</ul>
</div>
</header>
<div class="row-fluid">
<header id="leftColumn" class="span2">
<nav class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">Overview</li>
<li><a href="installation.html" title="Installation"><span class="none"></span>Installation</a></li>
<li class="active"><a href="#"><span class="none"></span>Initialization</a></li>
<li><a href="emitting_events.html" title="Emitting events"><span class="none"></span>Emitting events</a></li>
<li><a href="listening_to_events.html" title="Listening to events"><span class="none"></span>Listening to events</a></li>
<li><a href="socket_instance.html" title="The Socket instance"><span class="none"></span>The Socket instance</a></li>
<li><a href="migrating_from_1_x.html" title="Migrating from 1.x"><span class="none"></span>Migrating from 1.x</a></li>
<li class="nav-header">Miscellaneous</li>
<li><a href="changelog.html" title="Changelog"><span class="none"></span>Changelog</a></li>
<li><a href="apidocs/index.html" title="Javadoc"><span class="none"></span>Javadoc</a></li>
<li class="nav-header">Project Documentation</li>
<li><a href="project-info.html" title="Project Information"><span class="icon-chevron-right"></span>Project Information</a></li>
</ul>
</nav>
<div class="well sidebar-nav">
<hr />
<div id="poweredBy">
<div class="clear"></div>
<div class="clear"></div>
<div class="clear"></div>
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy"><img class="builtBy" alt="Built by Maven" src="./images/logos/maven-feather.png" /></a>
</div>
</div>
</header>
<main id="bodyColumn" class="span10" >
<h1>Initialization</h1>
<p><b>Table of content</b></p>
<ul>
<li><a href="#Creation_of_a_Socket_instance">Creation of a Socket instance</a></li>
<li><a href="#Default_values">Default values</a></li>
<li><a href="#Description">Description</a>
<ul>
<li><a href="#IO_factory_options">IO factory options</a>
<ul>
<li><a href="#forceNew">forceNew</a></li>
<li><a href="#multiplex">multiplex</a></li></ul></li>
<li><a href="#Low-level_engine_options">Low-level engine options</a>
<ul>
<li><a href="#transports">transports</a></li>
<li><a href="#upgrade">upgrade</a></li>
<li><a href="#rememberUpgrade">rememberUpgrade</a></li>
<li><a href="#path">path</a></li>
<li><a href="#query">query</a></li>
<li><a href="#extraHeaders">extraHeaders</a></li></ul></li>
<li><a href="#Socket_options">Socket options</a>
<ul>
<li><a href="#auth">auth</a></li></ul></li></ul></li></ul>
<section>
<h2><a name="Creation_of_a_Socket_instance"></a>Creation of a Socket instance</h2>
<div class="source">
<div class="source"><pre class="prettyprint">URI uri = URI.create("https://example.com");
IO.Options options = IO.Options.builder()
// ...
.build();
Socket socket = IO.socket(uri, options);
</pre></div></div>
<p>Unlike the JS client (which can infer it from the <code>window.location</code> object), the URI is mandatory here.</p>
<p>The <a class="externalLink" href="https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#Syntax">scheme</a> part of the URI is also mandatory. Both <code>ws://</code> and <code>http://</code> can be used interchangeably.</p>
<div class="source">
<div class="source"><pre class="prettyprint">Socket socket = IO.socket("https://example.com"); // OK
Socket socket = IO.socket("wss://example.com"); // OK, similar to the example above
Socket socket = IO.socket("192.168.0.1:1234"); // NOT OK, missing the scheme part
</pre></div></div>
<p>The path represents the <a class="externalLink" href="https://socket.io/docs/v3/namespaces/">Namespace</a>, and not the actual path (see <a href="#path">below</a>) of the HTTP requests:</p>
<div class="source">
<div class="source"><pre class="prettyprint">Socket socket = IO.socket(URI.create("https://example.com")); // the main namespace
Socket productSocket = IO.socket(URI.create("https://example.com/product")); // the "product" namespace
Socket orderSocket = IO.socket(URI.create("https://example.com/order")); // the "order" namespace
</pre></div></div>
</section><section>
<h2><a name="Default_values"></a>Default values</h2>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
// IO factory options
.setForceNew(false)
.setMultiplex(true)
// low-level engine options
.setTransports(new String[] { Polling.NAME, WebSocket.NAME })
.setUpgrade(true)
.setRememberUpgrade(false)
.setPath("/socket.io/")
.setQuery(null)
.setExtraHeaders(null)
// Manager options
.setReconnection(true)
.setReconnectionAttempts(Integer.MAX_VALUE)
.setReconnectionDelay(1_000)
.setReconnectionDelayMax(5_000)
.setRandomizationFactor(0.5)
.setTimeout(20_000)
// Socket options
.setAuth(null)
.build();
</pre></div></div>
</section><section>
<h2><a name="Description"></a>Description</h2><section>
<h3><a name="IO_factory_options"></a>IO factory options</h3>
<p>These settings will be shared by all Socket instances attached to the same Manager.</p><section>
<h4><a name="forceNew"></a><code>forceNew</code></h4>
<p>Default value: <code>false</code></p>
<p>Whether to create a new Manager instance.</p>
<p>A Manager instance is in charge of the low-level connection to the server (established with HTTP long-polling or WebSocket). It handles the reconnection logic.</p>
<p>A Socket instance is the interface which is used to sends events to — and receive events from — the server. It belongs to a given <a class="externalLink" href="https://socket.io/docs/v3/namespaces">namespace</a>.</p>
<p>A single Manager can be attached to several Socket instances.</p>
<p>The following example will reuse the same Manager instance for the 3 Socket instances (one single WebSocket connection):</p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setForceNew(false)
.build();
Socket socket = IO.socket(URI.create("https://example.com"), options); // the main namespace
Socket productSocket = IO.socket(URI.create("https://example.com/product"), options); // the "product" namespace
Socket orderSocket = IO.socket(URI.create("https://example.com/order"), options); // the "order" namespace
</pre></div></div>
<p>The following example will create 3 different Manager instances (and thus 3 distinct WebSocket connections):</p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setForceNew(true)
.build();
Socket socket = IO.socket(URI.create("https://example.com"), options); // the main namespace
Socket productSocket = IO.socket(URI.create("https://example.com/product"), options); // the "product" namespace
Socket orderSocket = IO.socket(URI.create("https://example.com/order"), options); // the "order" namespace
</pre></div></div>
</section><section>
<h4><a name="multiplex"></a><code>multiplex</code></h4>
<p>Default value: <code>true</code></p>
<p>The opposite of <code>forceNew</code>: whether to reuse an existing Manager instance.</p></section></section><section>
<h3><a name="Low-level_engine_options"></a>Low-level engine options</h3><section>
<h4><a name="transports"></a><code>transports</code></h4>
<p>Default value: <code>new String[] { Polling.NAME, WebSocket.NAME }</code></p>
<p>The low-level connection to the Socket.IO server can either be established with:</p>
<ul>
<li>HTTP long-polling: successive HTTP requests (<code>POST</code> for writing, <code>GET</code> for reading)</li>
<li><a class="externalLink" href="https://en.wikipedia.org/wiki/WebSocket">WebSocket</a></li>
</ul>
<p>The following example disables the HTTP long-polling transport:</p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setTransports(new String[] { WebSocket.NAME })
.build();
Socket socket = IO.socket(URI.create("https://example.com"), options);
</pre></div></div>
<p>Note: in that case, sticky sessions are not required on the server side (more information <a class="externalLink" href="https://socket.io/docs/v3/using-multiple-nodes/">here</a>).</p></section><section>
<h4><a name="upgrade"></a><code>upgrade</code></h4>
<p>Default value: <code>true</code></p>
<p>Whether the client should try to upgrade the transport from HTTP long-polling to something better.</p></section><section>
<h4><a name="rememberUpgrade"></a><code>rememberUpgrade</code></h4>
<p>Default value: <code>false</code></p>
<p>If true and if the previous WebSocket connection to the server succeeded, the connection attempt will bypass the normal upgrade process and will initially try WebSocket. A connection attempt following a transport error will use the normal upgrade process. It is recommended you turn this on only when using SSL/TLS connections, or if you know that your network does not block websockets.</p></section><section>
<h4><a name="path"></a><code>path</code></h4>
<p>Default value: <code>/socket.io/</code></p>
<p>It is the name of the path that is captured on the server side.</p>
<p>The server and the client values must match:</p>
<p><i>Server</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">import { Server } from "socket.io";
const io = new Server(8080, {
path: "/my-custom-path/"
});
io.on("connection", (socket) => {
// ...
});
</pre></div></div>
<p><i>Client</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setPath("/my-custom-path/")
.build();
Socket socket = IO.socket(URI.create("https://example.com"), options);
</pre></div></div>
<p>Please note that this is different from the path in the URI, which represents the <a class="externalLink" href="https://socket.io/docs/v3/namespaces/">Namespace</a>.</p>
<p>Example:</p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setPath("/my-custom-path/")
.build();
Socket socket = IO.socket(URI.create("https://example.com/order"), options);
</pre></div></div>
<ul>
<li>the Socket instance is attached to the “order” Namespace</li>
<li>the HTTP requests will look like: <code>GET https://example.com/my-custom-path/?EIO=4&transport=polling&t=ML4jUwU</code></li>
</ul></section><section>
<h4><a name="query"></a><code>query</code></h4>
<p>Default value: -</p>
<p>Additional query parameters (then found in <code>socket.handshake.query</code> object on the server-side).</p>
<p>Example:</p>
<p><i>Server</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">io.on("connection", (socket) => {
console.log(socket.handshake.query); // prints { x: '42', EIO: '4', transport: 'polling' }
});
</pre></div></div>
<p><i>Client</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setQuery("x=42")
.build();
Socket socket = IO.socket(URI.create("https://example.com"), options);
</pre></div></div>
<p>Note: The <code>socket.handshake.query</code> object contains the query parameters that were sent during the Socket.IO handshake, it won’t be updated for the duration of the current session, which means changing the <code>query</code> on the client-side will only be effective when the current session is closed and a new one is created:</p>
<div class="source">
<div class="source"><pre class="prettyprint">socket.io().on(Manager.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
@Override
public void call(Object... args) {
options.query = "y=43";
}
});
</pre></div></div>
</section><section>
<h4><a name="extraHeaders"></a><code>extraHeaders</code></h4>
<p>Default value: -</p>
<p>Additional headers (then found in <code>socket.handshake.headers</code> object on the server-side).</p>
<p>Example:</p>
<p><i>Server</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">io.on("connection", (socket) => {
console.log(socket.handshake.headers); // prints { accept: '*/*', authorization: 'bearer 1234', connection: 'Keep-Alive', 'accept-encoding': 'gzip', 'user-agent': 'okhttp/3.12.12' }
});
</pre></div></div>
<p><i>Client</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setExtraHeaders(singletonMap("authorization", singletonList("bearer 1234")))
.build();
Socket socket = IO.socket(URI.create("https://example.com"), options);
</pre></div></div>
<p>Note: Similar to the <code>query</code> option above, the <code>socket.handshake.headers</code> object contains the headers that were sent during the Socket.IO handshake, it won’t be updated for the duration of the current session, which means changing the <code>extraHeaders</code> on the client-side will only be effective when the current session is closed and a new one is created:</p>
<div class="source">
<div class="source"><pre class="prettyprint">socket.io().on(Manager.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
@Override
public void call(Object... args) {
options.extraHeaders.put("authorization", singletonList("bearer 5678"));
}
});
</pre></div></div>
</section></section><section>
<h3><a name="Socket_options"></a>Socket options</h3>
<p>These settings are specific to the given Socket instance.</p><section>
<h4><a name="auth"></a><code>auth</code></h4>
<p>Default value: -</p>
<p>Credentials that are sent when accessing a namespace (see also <a class="externalLink" href="https://socket.io/docs/v3/middlewares/#Sending-credentials">here</a>).</p>
<p>Example:</p>
<p><i>Server</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">io.on("connection", (socket) => {
console.log(socket.handshake.auth); // prints { token: 'abcd' }
});
</pre></div></div>
<p><i>Client</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options options = IO.Options.builder()
.setAuth(singletonMap("token", "abcd"))
.build();
Socket socket = IO.socket(URI.create("https://example.com"), options);
</pre></div></div>
<p>You can update the <code>auth</code> map when the access to the Namespace is denied:</p>
<div class="source">
<div class="source"><pre class="prettyprint">socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
@Override
public void call(Object... args) {
options.auth.put("token", "efgh");
socket.connect();
}
});
</pre></div></div>
<p>Or manually force the Socket instance to reconnect:</p>
<div class="source">
<div class="source"><pre class="prettyprint">options.auth.put("token", "efgh");
socket.disconnect().connect();
</pre></div></div></section></section></section>
</main>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row-fluid">
<p>© 2021
</p>
</div>
</div>
</footer>
</body>
</html>