forked from socketio/socket.io-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.html
More file actions
230 lines (201 loc) · 8.57 KB
/
usage.html
File metadata and controls
230 lines (201 loc) · 8.57 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
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 1.9.2 from src/site/markdown/usage.md at 2021-09-21
| 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 – Usage</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-09-21<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><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><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" >
<section>
<h2><a name="Usage"></a>Usage</h2>
<p>Socket.IO-client Java has almost the same api and features with the original JS client. You use <code>IO#socket</code> to initialize <code>Socket</code>:</p>
<div class="source">
<div class="source"><pre class="prettyprint">import io.socket.client.IO;
import io.socket.client.Socket;
...
Socket socket = IO.socket("http://localhost");
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
socket.emit("foo", "hi");
socket.disconnect();
}
}).on("event", new Emitter.Listener() {
@Override
public void call(Object... args) {}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {}
});
socket.connect();
</pre></div></div>
<p>This Library uses <a class="externalLink" href="https://github.com/stleary/JSON-java">org.json</a> to parse and compose JSON strings:</p>
<div class="source">
<div class="source"><pre class="prettyprint">// Sending an object
JSONObject obj = new JSONObject();
obj.put("hello", "server");
obj.put("binary", new byte[42]);
socket.emit("foo", obj);
// Receiving an object
socket.on("foo", new Emitter.Listener() {
@Override
public void call(Object... args) {
JSONObject obj = (JSONObject)args[0];
}
});
</pre></div></div>
<p>Options are supplied as follows:</p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.reconnection = false;
socket = IO.socket("http://localhost", opts);
</pre></div></div>
<p>You can supply query parameters with the <code>query</code> option. NB: if you don’t want to reuse a cached socket instance when the query parameter changes, you should use the <code>forceNew</code> option, the use case might be if your app allows for a user to logout, and a new user to login again:</p>
<div class="source">
<div class="source"><pre class="prettyprint">IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.query = "auth_token=" + authToken;
Socket socket = IO.socket("http://localhost", opts);
</pre></div></div>
<p>You can get a callback with <code>Ack</code> when the server received a message:</p>
<div class="source">
<div class="source"><pre class="prettyprint">socket.emit("foo", "woot", new Ack() {
@Override
public void call(Object... args) {}
});
</pre></div></div>
<p>And vice versa:</p>
<div class="source">
<div class="source"><pre class="prettyprint">// ack from client to server
socket.on("foo", new Emitter.Listener() {
@Override
public void call(Object... args) {
Ack ack = (Ack) args[args.length - 1];
ack.call();
}
});
</pre></div></div>
<p>SSL (HTTPS, WSS) settings:</p>
<div class="source">
<div class="source"><pre class="prettyprint">OkHttpClient okHttpClient = new OkHttpClient.Builder()
.hostnameVerifier(myHostnameVerifier)
.sslSocketFactory(mySSLContext.getSocketFactory(), myX509TrustManager)
.build();
// default settings for all sockets
IO.setDefaultOkHttpWebSocketFactory(okHttpClient);
IO.setDefaultOkHttpCallFactory(okHttpClient);
// set as an option
opts = new IO.Options();
opts.callFactory = okHttpClient;
opts.webSocketFactory = okHttpClient;
socket = IO.socket("https://localhost", opts);
</pre></div></div>
<p>See the Javadoc for more details.</p>
<p><a class="externalLink" href="http://socketio.github.io/socket.io-client-java/apidocs/">http://socketio.github.io/socket.io-client-java/apidocs/</a></p><section>
<h3><a name="Transports_and_HTTP_Headers"></a>Transports and HTTP Headers</h3>
<p>You can access transports and their HTTP headers as follows.</p>
<div class="source">
<div class="source"><pre class="prettyprint">// Called upon transport creation.
socket.io().on(Manager.EVENT_TRANSPORT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Transport transport = (Transport)args[0];
transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() {
@Override
public void call(Object... args) {
@SuppressWarnings("unchecked")
Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
// modify request headers
headers.put("Cookie", Arrays.asList("foo=1;"));
}
});
transport.on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
@Override
public void call(Object... args) {
@SuppressWarnings("unchecked")
Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
// access response headers
String cookie = headers.get("Set-Cookie").get(0);
}
});
}
});
</pre></div></div>
</section></section><section>
<h2><a name="Features"></a>Features</h2>
<p>This library supports all of the features the JS client does, including events, options and upgrading transport. Android is fully supported.</p></section>
</main>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row-fluid">
<p>© 2021
</p>
</div>
</div>
</footer>
</body>
</html>