@@ -12,11 +12,7 @@ See also:
12
12
## Table of content
13
13
14
14
- [ Compatibility] ( #compatibility )
15
- - [ Installation] ( #installation )
16
- - [ Maven] ( #maven )
17
- - [ Gradle] ( #gradle )
18
- - [ Usage] ( #usage )
19
- - [ Features] ( #features )
15
+ - [ Documentation] ( #documentation )
20
16
- [ License] ( #license )
21
17
22
18
## Compatibility
@@ -27,180 +23,11 @@ See also:
27
23
| 1.x | 2.x |
28
24
| 2.x | 3.x |
29
25
30
- ## Installation
31
- The latest artifact is available on Maven Central.
26
+ ## Documentation
32
27
33
- ### Maven
34
- Add the following dependency to your ` pom.xml ` .
28
+ The documentation can be found [ here] ( https://socketio.github.io/socket.io-client-java/installation.html ) .
35
29
36
- ``` xml
37
- <dependencies >
38
- <dependency >
39
- <groupId >io.socket</groupId >
40
- <artifactId >socket.io-client</artifactId >
41
- <version >2.0.0</version >
42
- </dependency >
43
- </dependencies >
44
- ```
45
-
46
- ### Gradle
47
- Add it as a gradle dependency for Android Studio, in ` build.gradle ` :
48
-
49
- ``` groovy
50
- compile ('io.socket:socket.io-client:2.0.0') {
51
- // excluding org.json which is provided by Android
52
- exclude group: 'org.json', module: 'json'
53
- }
54
- ```
55
-
56
- ## Usage
57
- Socket.IO-client Java has almost the same api and features with the original JS client. You use ` IO#socket ` to initialize ` Socket ` :
58
-
59
- ``` java
60
- import io.socket.client.IO ;
61
- import io.socket.client.Socket ;
62
- ...
63
-
64
- Socket socket = IO . socket(" http://localhost" );
65
- socket. on(Socket . EVENT_CONNECT , new Emitter .Listener () {
66
-
67
- @Override
68
- public void call (Object ... args ) {
69
- socket. emit(" foo" , " hi" );
70
- socket. disconnect();
71
- }
72
-
73
- }). on(" event" , new Emitter .Listener () {
74
-
75
- @Override
76
- public void call (Object ... args ) {}
77
-
78
- }). on(Socket . EVENT_DISCONNECT , new Emitter .Listener () {
79
-
80
- @Override
81
- public void call (Object ... args ) {}
82
-
83
- });
84
- socket. connect();
85
- ```
86
-
87
- This Library uses [ org.json] ( https://github.com/stleary/JSON-java ) to parse and compose JSON strings:
88
-
89
- ``` java
90
- // Sending an object
91
- JSONObject obj = new JSONObject ();
92
- obj. put(" hello" , " server" );
93
- obj. put(" binary" , new byte [42 ]);
94
- socket. emit(" foo" , obj);
95
-
96
- // Receiving an object
97
- socket. on(" foo" , new Emitter .Listener () {
98
- @Override
99
- public void call (Object ... args ) {
100
- JSONObject obj = (JSONObject )args[0 ];
101
- }
102
- });
103
- ```
104
-
105
- Options are supplied as follows:
106
-
107
- ``` java
108
- IO . Options opts = new IO .Options ();
109
- opts. forceNew = true ;
110
- opts. reconnection = false ;
111
-
112
- socket = IO . socket(" http://localhost" , opts);
113
- ```
114
-
115
- You can supply query parameters with the ` query ` option. NB: if you don't want to reuse a cached socket instance when the query parameter changes, you should use the ` forceNew ` option, the use case might be if your app allows for a user to logout, and a new user to login again:
116
-
117
- ``` java
118
- IO . Options opts = new IO .Options ();
119
- opts. forceNew = true ;
120
- opts. query = " auth_token=" + authToken;
121
- Socket socket = IO . socket(" http://localhost" , opts);
122
- ```
123
-
124
- You can get a callback with ` Ack ` when the server received a message:
125
-
126
- ``` java
127
- socket. emit(" foo" , " woot" , new Ack () {
128
- @Override
129
- public void call (Object ... args ) {}
130
- });
131
- ```
132
-
133
- And vice versa:
134
-
135
- ``` java
136
- // ack from client to server
137
- socket. on(" foo" , new Emitter .Listener () {
138
- @Override
139
- public void call (Object ... args ) {
140
- Ack ack = (Ack ) args[args. length - 1 ];
141
- ack. call();
142
- }
143
- });
144
- ```
145
-
146
- SSL (HTTPS, WSS) settings:
147
-
148
- ``` java
149
- OkHttpClient okHttpClient = new OkHttpClient .Builder ()
150
- .hostnameVerifier(myHostnameVerifier)
151
- .sslSocketFactory(mySSLContext. getSocketFactory(), myX509TrustManager)
152
- .build();
153
-
154
- // default settings for all sockets
155
- IO . setDefaultOkHttpWebSocketFactory(okHttpClient);
156
- IO . setDefaultOkHttpCallFactory(okHttpClient);
157
-
158
- // set as an option
159
- opts = new IO .Options ();
160
- opts. callFactory = okHttpClient;
161
- opts. webSocketFactory = okHttpClient;
162
- socket = IO . socket(" https://localhost" , opts);
163
- ```
164
-
165
- See the Javadoc for more details.
166
-
167
- http://socketio.github.io/socket.io-client-java/apidocs/
168
-
169
- ### Transports and HTTP Headers
170
- You can access transports and their HTTP headers as follows.
171
-
172
- ``` java
173
- // Called upon transport creation.
174
- socket. io(). on(Manager . EVENT_TRANSPORT , new Emitter .Listener () {
175
- @Override
176
- public void call (Object ... args ) {
177
- Transport transport = (Transport )args[0 ];
178
-
179
- transport. on(Transport . EVENT_REQUEST_HEADERS , new Emitter .Listener () {
180
- @Override
181
- public void call (Object ... args ) {
182
- @SuppressWarnings (" unchecked" )
183
- Map<String , List<String > > headers = (Map<String , List<String > > )args[0 ];
184
- // modify request headers
185
- headers. put(" Cookie" , Arrays . asList(" foo=1;" ));
186
- }
187
- });
188
-
189
- transport. on(Transport . EVENT_RESPONSE_HEADERS , new Emitter .Listener () {
190
- @Override
191
- public void call (Object ... args ) {
192
- @SuppressWarnings (" unchecked" )
193
- Map<String , List<String > > headers = (Map<String , List<String > > )args[0 ];
194
- // access response headers
195
- String cookie = headers. get(" Set-Cookie" ). get(0 );
196
- }
197
- });
198
- }
199
- });
200
- ```
201
-
202
- ## Features
203
- This library supports all of the features the JS client does, including events, options and upgrading transport. Android is fully supported.
30
+ The source of this documentation is in the ` src/site/ ` directory of the repository. Pull requests are welcome!
204
31
205
32
## License
206
33
0 commit comments