Skip to content

Commit 1f90ffc

Browse files
add example with server to client ack
1 parent 9c62757 commit 1f90ffc

19 files changed

+89
-44
lines changed

changelog.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!DOCTYPE html>
22
<!--
3-
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from src/site/markdown/changelog.md at 2021-09-21
3+
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from src/site/markdown/changelog.md at 2021-11-24
44
| Rendered using Apache Maven Fluido Skin 1.9
55
-->
66
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
@@ -32,7 +32,7 @@
3232

3333
<div id="breadcrumbs">
3434
<ul class="breadcrumb">
35-
<li id="publishDate">Last Published: 2021-09-21<span class="divider">|</span>
35+
<li id="publishDate">Last Published: 2021-11-24<span class="divider">|</span>
3636
</li>
3737
<li id="projectVersion">Version: 2.0.2-SNAPSHOT</li>
3838
</ul>

dependencies.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!DOCTYPE html>
22
<!--
3-
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from org.apache.maven.plugins:maven-project-info-reports-plugin:3.1.1:dependencies at 2021-09-21
3+
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from org.apache.maven.plugins:maven-project-info-reports-plugin:3.1.1:dependencies at 2021-11-24
44
| Rendered using Apache Maven Fluido Skin 1.9
55
-->
66
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
@@ -32,7 +32,7 @@
3232

3333
<div id="breadcrumbs">
3434
<ul class="breadcrumb">
35-
<li id="publishDate">Last Published: 2021-09-21<span class="divider">|</span>
35+
<li id="publishDate">Last Published: 2021-11-24<span class="divider">|</span>
3636
</li>
3737
<li id="projectVersion">Version: 2.0.2-SNAPSHOT</li>
3838
</ul>

dependency-info.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!DOCTYPE html>
22
<!--
3-
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from org.apache.maven.plugins:maven-project-info-reports-plugin:3.1.1:dependency-info at 2021-09-21
3+
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from org.apache.maven.plugins:maven-project-info-reports-plugin:3.1.1:dependency-info at 2021-11-24
44
| Rendered using Apache Maven Fluido Skin 1.9
55
-->
66
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
@@ -32,7 +32,7 @@
3232

3333
<div id="breadcrumbs">
3434
<ul class="breadcrumb">
35-
<li id="publishDate">Last Published: 2021-09-21<span class="divider">|</span>
35+
<li id="publishDate">Last Published: 2021-11-24<span class="divider">|</span>
3636
</li>
3737
<li id="projectVersion">Version: 2.0.2-SNAPSHOT</li>
3838
</ul>

distribution-management.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!DOCTYPE html>
22
<!--
3-
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from org.apache.maven.plugins:maven-project-info-reports-plugin:3.1.1:distribution-management at 2021-09-21
3+
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from org.apache.maven.plugins:maven-project-info-reports-plugin:3.1.1:distribution-management at 2021-11-24
44
| Rendered using Apache Maven Fluido Skin 1.9
55
-->
66
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
@@ -32,7 +32,7 @@
3232

3333
<div id="breadcrumbs">
3434
<ul class="breadcrumb">
35-
<li id="publishDate">Last Published: 2021-09-21<span class="divider">|</span>
35+
<li id="publishDate">Last Published: 2021-11-24<span class="divider">|</span>
3636
</li>
3737
<li id="projectVersion">Version: 2.0.2-SNAPSHOT</li>
3838
</ul>

emitting_events.html

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!DOCTYPE html>
22
<!--
3-
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from src/site/markdown/emitting_events.md at 2021-09-21
3+
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from src/site/markdown/emitting_events.md at 2021-11-24
44
| Rendered using Apache Maven Fluido Skin 1.9
55
-->
66
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
@@ -32,7 +32,7 @@
3232

3333
<div id="breadcrumbs">
3434
<ul class="breadcrumb">
35-
<li id="publishDate">Last Published: 2021-09-21<span class="divider">|</span>
35+
<li id="publishDate">Last Published: 2021-11-24<span class="divider">|</span>
3636
</li>
3737
<li id="projectVersion">Version: 2.0.2-SNAPSHOT</li>
3838
</ul>
@@ -72,7 +72,10 @@ <h1>Emitting events</h1>
7272
<p><b>Table of content</b></p>
7373
<ul>
7474
<li><a href="#Basic_emit">Basic emit</a></li>
75-
<li><a href="#Acknowledgements">Acknowledgements</a></li></ul>
75+
<li><a href="#Acknowledgements">Acknowledgements</a>
76+
<ul>
77+
<li><a href="#From_client_to_server">From client to server</a></li>
78+
<li><a href="#From_server_to_client">From server to client</a></li></ul></li></ul>
7679

7780
<p>There are several ways to send events between the server and the client.</p><section>
7881
<h2><a name="Basic_emit"></a>Basic emit</h2>
@@ -136,7 +139,27 @@ <h2><a name="Basic_emit"></a>Basic emit</h2>
136139
</section><section>
137140
<h2><a name="Acknowledgements"></a>Acknowledgements</h2>
138141
<p>Events are great, but in some cases you may want a more classic request-response API. In Socket.IO, this feature is named acknowledgements.</p>
139-
<p>You can add a callback as the last argument of the <code>emit()</code>, and this callback will be called once the other side acknowledges the event:</p>
142+
<p>You can add a callback as the last argument of the <code>emit()</code>, and this callback will be called once the other side acknowledges the event:</p><section>
143+
<h3><a name="From_client_to_server"></a>From client to server</h3>
144+
<p><i>Client</i></p>
145+
146+
<div class="source">
147+
<div class="source"><pre class="prettyprint">// Java 7
148+
socket.emit(&quot;update item&quot;, 1, new JSONObject(singletonMap(&quot;name&quot;, &quot;updated&quot;)), new Ack() {
149+
@Override
150+
public void call(Object... args) {
151+
JSONObject response = (JSONObject) args[0];
152+
System.out.println(response.getString(&quot;status&quot;)); // &quot;ok&quot;
153+
}
154+
});
155+
156+
// Java 8 and above
157+
socket.emit(&quot;update item&quot;, 1, new JSONObject(singletonMap(&quot;name&quot;, &quot;updated&quot;)), (Ack) args -&gt; {
158+
JSONObject response = (JSONObject) args[0];
159+
System.out.println(response.getString(&quot;status&quot;)); // &quot;ok&quot;
160+
});
161+
</pre></div></div>
162+
140163
<p><i>Server</i></p>
141164

142165
<div class="source">
@@ -150,18 +173,40 @@ <h2><a name="Acknowledgements"></a>Acknowledgements</h2>
150173
});
151174
});
152175
</pre></div></div>
176+
</section><section>
177+
<h3><a name="From_server_to_client"></a>From server to client</h3>
178+
<p><i>Server</i></p>
179+
180+
<div class="source">
181+
<div class="source"><pre class="prettyprint">io.on(&quot;connection&quot;, (socket) =&gt; {
182+
socket.emit(&quot;hello&quot;, &quot;please acknowledge&quot;, (response) =&gt; {
183+
console.log(response); // prints &quot;hi!&quot;
184+
});
185+
});
186+
</pre></div></div>
153187

154188
<p><i>Client</i></p>
155189

156190
<div class="source">
157-
<div class="source"><pre class="prettyprint">socket.emit(&quot;update item&quot;, 1, new JSONObject(singletonMap(&quot;name&quot;, &quot;updated&quot;)), new Ack() {
191+
<div class="source"><pre class="prettyprint">// Java 7
192+
socket.on(&quot;hello&quot;, new Emitter.Listener() {
158193
@Override
159194
public void call(Object... args) {
160-
JSONObject response = (JSONObject) args[0];
161-
System.out.println(response.getString(&quot;status&quot;)); // &quot;ok&quot;
195+
System.out.println(args[0]); // &quot;please acknowledge&quot;
196+
if (args.length &gt; 1 &amp;&amp; args[1] instanceof Ack) {
197+
((Ack) args[1]).call(&quot;hi!&quot;);
198+
}
199+
}
200+
});
201+
202+
// Java 8 and above
203+
socket.on(&quot;hello&quot;, args -&gt; {
204+
System.out.println(args[0]); // &quot;please acknowledge&quot;
205+
if (args.length &gt; 1 &amp;&amp; args[1] instanceof Ack) {
206+
((Ack) args[1]).call(&quot;hi!&quot;);
162207
}
163208
});
164-
</pre></div></div></section>
209+
</pre></div></div></section></section>
165210
</main>
166211
</div>
167212
</div>

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!DOCTYPE html>
22
<!--
3-
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from org.apache.maven.plugins:maven-project-info-reports-plugin:3.1.1:index at 2021-09-21
3+
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from org.apache.maven.plugins:maven-project-info-reports-plugin:3.1.1:index at 2021-11-24
44
| Rendered using Apache Maven Fluido Skin 1.9
55
-->
66
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
@@ -32,7 +32,7 @@
3232

3333
<div id="breadcrumbs">
3434
<ul class="breadcrumb">
35-
<li id="publishDate">Last Published: 2021-09-21<span class="divider">|</span>
35+
<li id="publishDate">Last Published: 2021-11-24<span class="divider">|</span>
3636
</li>
3737
<li id="projectVersion">Version: 2.0.2-SNAPSHOT</li>
3838
</ul>

initialization.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!DOCTYPE html>
22
<!--
3-
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from src/site/markdown/initialization.md at 2021-09-21
3+
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from src/site/markdown/initialization.md at 2021-11-24
44
| Rendered using Apache Maven Fluido Skin 1.9
55
-->
66
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
@@ -32,7 +32,7 @@
3232

3333
<div id="breadcrumbs">
3434
<ul class="breadcrumb">
35-
<li id="publishDate">Last Published: 2021-09-21<span class="divider">|</span>
35+
<li id="publishDate">Last Published: 2021-11-24<span class="divider">|</span>
3636
</li>
3737
<li id="projectVersion">Version: 2.0.2-SNAPSHOT</li>
3838
</ul>

installation.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!DOCTYPE html>
22
<!--
3-
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from src/site/markdown/installation.md at 2021-09-21
3+
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from src/site/markdown/installation.md at 2021-11-24
44
| Rendered using Apache Maven Fluido Skin 1.9
55
-->
66
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
@@ -32,7 +32,7 @@
3232

3333
<div id="breadcrumbs">
3434
<ul class="breadcrumb">
35-
<li id="publishDate">Last Published: 2021-09-21<span class="divider">|</span>
35+
<li id="publishDate">Last Published: 2021-11-24<span class="divider">|</span>
3636
</li>
3737
<li id="projectVersion">Version: 2.0.2-SNAPSHOT</li>
3838
</ul>

licenses.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!DOCTYPE html>
22
<!--
3-
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from org.apache.maven.plugins:maven-project-info-reports-plugin:3.1.1:licenses at 2021-09-21
3+
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from org.apache.maven.plugins:maven-project-info-reports-plugin:3.1.1:licenses at 2021-11-24
44
| Rendered using Apache Maven Fluido Skin 1.9
55
-->
66
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
@@ -32,7 +32,7 @@
3232

3333
<div id="breadcrumbs">
3434
<ul class="breadcrumb">
35-
<li id="publishDate">Last Published: 2021-09-21<span class="divider">|</span>
35+
<li id="publishDate">Last Published: 2021-11-24<span class="divider">|</span>
3636
</li>
3737
<li id="projectVersion">Version: 2.0.2-SNAPSHOT</li>
3838
</ul>

listening_to_events.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!DOCTYPE html>
22
<!--
3-
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from src/site/markdown/listening_to_events.md at 2021-09-21
3+
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from src/site/markdown/listening_to_events.md at 2021-11-24
44
| Rendered using Apache Maven Fluido Skin 1.9
55
-->
66
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
@@ -32,7 +32,7 @@
3232

3333
<div id="breadcrumbs">
3434
<ul class="breadcrumb">
35-
<li id="publishDate">Last Published: 2021-09-21<span class="divider">|</span>
35+
<li id="publishDate">Last Published: 2021-11-24<span class="divider">|</span>
3636
</li>
3737
<li id="projectVersion">Version: 2.0.2-SNAPSHOT</li>
3838
</ul>

0 commit comments

Comments
 (0)