Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 981
Expand file tree
/
Copy pathsocket_instance.html
More file actions
256 lines (236 loc) · 9.9 KB
/
Copy pathsocket_instance.html
File metadata and controls
256 lines (236 loc) · 9.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
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from src/site/markdown/socket_instance.md at 2025-02-18
| 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 – The Socket instance</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: 2025-02-18<span class="divider">|</span>
</li>
<li id="projectVersion">Version: 2.1.2</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><a href="initialization.html" title="Initialization"><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 class="active"><a href="#"><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><a href="logging.html" title="Logging"><span class="none"></span>Logging</a></li>
<li><a href="faq.html" title="FAQ"><span class="none"></span>FAQ</a></li>
<li><a href="android.html" title="Android"><span class="none"></span>Android</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>The Socket instance</h1>
<p><b>Table of content</b></p>
<ul>
<li><a href="#Socket.23id">Socket#id</a></li>
<li><a href="#Socket.23connected">Socket#connected</a></li>
<li><a href="#Lifecycle">Lifecycle</a></li>
<li><a href="#Events">Events</a>
<ul>
<li><a href="#Socket.EVENT_CONNECT">Socket.EVENT_CONNECT</a></li>
<li><a href="#Socket.EVENT_CONNECT_ERROR">Socket.EVENT_CONNECT_ERROR</a></li>
<li><a href="#Socket.EVENT_DISCONNECT">Socket.EVENT_DISCONNECT</a></li></ul></li></ul>
<ul>
<li><a href="apidocs/index.html?io/socket/client/Socket.html">Javadoc</a></li>
</ul>
<p>Besides <a href="emitting_events.html">emitting</a> and <a href="listening_to_events.html">listening to</a> events, the Socket instance has a few attributes that may be of use in your application:</p><section>
<h2><a name="Socket.23id"></a>Socket#id</h2>
<p>Each new connection is assigned a random 20-characters identifier.</p>
<p>This identifier is synced with the value on the server-side.</p>
<p><i>Server</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">io.on("connection", (socket) => {
console.log(socket.id); // x8WIv7-mJelg7on_ALbx
});
</pre></div></div>
<p><i>Client</i></p>
<div class="source">
<div class="source"><pre class="prettyprint">socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println(socket.id()); // x8WIv7-mJelg7on_ALbx
}
});
socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println(socket.id()); // null
}
});
</pre></div></div>
</section><section>
<h2><a name="Socket.23connected"></a>Socket#connected</h2>
<p>This attribute describes whether the socket is currently connected to the server.</p>
<div class="source">
<div class="source"><pre class="prettyprint">socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println(socket.connected()); // true
}
});
socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println(socket.connected()); // false
}
});
</pre></div></div>
</section><section>
<h2><a name="Lifecycle"></a>Lifecycle</h2>
<p><img src="images/client_socket_events.png" alt="Lifecycle diagram" style="max-width: 800px" /></p></section><section>
<h2><a name="Events"></a>Events</h2><section>
<h3><a name="Socket.EVENT_CONNECT"></a><code>Socket.EVENT_CONNECT</code></h3>
<p>This event is fired by the Socket instance upon connection / reconnection.</p>
<div class="source">
<div class="source"><pre class="prettyprint">socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
// ...
}
});
</pre></div></div>
<p>Please note that you shouldn’t register event handlers in the <code>connect</code> handler itself, as a new handler will be registered every time the Socket reconnects:</p>
<div class="source">
<div class="source"><pre class="prettyprint">// BAD
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.on("data", new Emitter.Listener() {
@Override
public void call(Object... args) {
// ...
}
});
}
});
// GOOD
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
// ...
}
});
socket.on("data", new Emitter.Listener() {
@Override
public void call(Object... args) {
// ...
}
});
</pre></div></div>
</section><section>
<h3><a name="Socket.EVENT_CONNECT_ERROR"></a><code>Socket.EVENT_CONNECT_ERROR</code></h3>
<p>This event is fired when the server does not accept the connection (in a <a class="externalLink" href="https://socket.io/docs/v4/middlewares/#sending-credentials">middleware function</a>).</p>
<p>You need to manually reconnect. You might need to update the credentials:</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("authorization", "bearer 1234");
socket.connect();
}
});
</pre></div></div>
</section><section>
<h3><a name="Socket.EVENT_DISCONNECT"></a><code>Socket.EVENT_DISCONNECT</code></h3>
<p>This event is fired upon disconnection.</p>
<div class="source">
<div class="source"><pre class="prettyprint">socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println(socket.id()); // null
}
});
</pre></div></div>
<p>Here is the list of possible reasons:</p>
<table border="0" class="table table-striped">
<thead>
<tr class="a">
<th>Reason </th>
<th> Description</th></tr>
</thead><tbody>
<tr class="b">
<td><code>io server disconnect</code> </td>
<td> The server has forcefully disconnected the socket with <a class="externalLink" href="https://socket.io/docs/v4/server-api/#socketdisconnectclose">socket.disconnect()</a></td></tr>
<tr class="a">
<td><code>io client disconnect</code> </td>
<td> The socket was manually disconnected using <code>socket.disconnect()</code></td></tr>
<tr class="b">
<td><code>ping timeout</code> </td>
<td> The server did not respond in the <code>pingTimeout</code> range</td></tr>
<tr class="a">
<td><code>transport close</code> </td>
<td> The connection was closed (example: the user has lost connection, or the network was changed from WiFi to 4G)</td></tr>
<tr class="b">
<td><code>transport error</code> </td>
<td> The connection has encountered an error (example: the server was killed during a HTTP long-polling cycle)</td></tr>
</tbody>
</table>
<p>Note: those events, along with <code>disconnecting</code>, <code>newListener</code> and <code>removeListener</code>, are special events that shouldn’t be used in your application:</p>
<div class="source">
<div class="source"><pre class="prettyprint">// BAD, will throw an error
socket.emit("disconnect");
</pre></div></div></section></section>
</main>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row-fluid">
<p>© 2025
</p>
</div>
</div>
</footer>
</body>
</html>
You can’t perform that action at this time.
