forked from Gottox/socket.io-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXhrTransport.java
More file actions
279 lines (249 loc) · 6.13 KB
/
XhrTransport.java
File metadata and controls
279 lines (249 loc) · 6.13 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
/*
* socket.io-java-client XhrTransport.java
*
* Copyright (c) 2012, Enno Boland
* socket.io-java-client is a implementation of the socket.io protocol in Java.
*
* See LICENSE file for more information
*/
package io.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
/**
* The Class XhrTransport.
*/
class XhrTransport implements IOTransport {
/** The String to identify this Transport. */
public static final String TRANSPORT_NAME = "xhr-polling";
/** The connection. */
private IOConnection connection;
/** The url. */
private URL url;
/** The queue holding elements to send. */
ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
/** background thread for managing the server connection. */
PollThread pollThread = null;
/** Indicates whether the {@link IOConnection} wants us to be connected. */
private boolean connect;
/** Indicates whether {@link PollThread} is blocked. */
private boolean blocked;
HttpURLConnection urlConnection;
/**
* The Class ReceiverThread.
*/
private class PollThread extends Thread {
private static final String CHARSET = "UTF-8";
/**
* Instantiates a new receiver thread.
*/
public PollThread() {
super(TRANSPORT_NAME);
}
/*
* (non-Javadoc)
*
* @see java.lang.Thread#run()
*/
@Override
public void run() {
connection.transportConnected();
while (isConnect()) {
try {
String line;
URL url = new URL(XhrTransport.this.url.toString() + "?t="
+ System.currentTimeMillis());
urlConnection = (HttpURLConnection) url.openConnection();
SSLContext context = IOConnection.getSslContext();
if(urlConnection instanceof HttpsURLConnection && context != null) {
((HttpsURLConnection)urlConnection).setSSLSocketFactory(context.getSocketFactory());
}
if (!queue.isEmpty()) {
urlConnection.setDoOutput(true);
OutputStream output = urlConnection.getOutputStream();
if (queue.size() == 1) {
line = queue.poll();
output.write(line.getBytes(CHARSET));
} else {
Iterator<String> iter = queue.iterator();
while (iter.hasNext()) {
String junk = iter.next();
line = IOConnection.FRAME_DELIMITER + junk.length()
+ IOConnection.FRAME_DELIMITER + junk;
output.write(line.getBytes(CHARSET));
iter.remove();
}
}
output.close();
InputStream input = urlConnection.getInputStream();
byte[] buffer = new byte[1024];
while(input.read(buffer) > 0) {
}
input.close();
} else {
setBlocked(true);
InputStream plainInput = urlConnection.getInputStream();
BufferedReader input = new BufferedReader(
new InputStreamReader(plainInput, CHARSET));
while ((line = input.readLine()) != null) {
if (connection != null)
connection.transportData(line);
}
setBlocked(false);
}
} catch (IOException e) {
if (connection != null && interrupted() == false) {
connection.transportError(e);
return;
}
}
try {
sleep(100);
} catch (InterruptedException e) {
}
}
connection.transportDisconnected();
}
}
/**
* Creates a new Transport for the given url an {@link IOConnection}.
*
* @param url
* the url
* @param connection
* the connection
* @return the iO transport
*/
public static IOTransport create(URL url, IOConnection connection) {
try {
URL xhrUrl = new URL(url.toString() + IOConnection.SOCKET_IO_1
+ TRANSPORT_NAME + "/" + connection.getSessionId());
return new XhrTransport(xhrUrl, connection);
} catch (MalformedURLException e) {
throw new RuntimeException(
"Malformed Internal url. This should never happen. Please report a bug.",
e);
}
}
/**
* Instantiates a new xhr transport.
*
* @param url
* the url
* @param connection
* the connection
*/
public XhrTransport(URL url, IOConnection connection) {
this.connection = connection;
this.url = url;
}
/*
* (non-Javadoc)
*
* @see io.socket.IOTransport#connect()
*/
@Override
public void connect() {
this.setConnect(true);
pollThread = new PollThread();
pollThread.start();
}
/*
* (non-Javadoc)
*
* @see io.socket.IOTransport#disconnect()
*/
@Override
public void disconnect() {
this.setConnect(false);
pollThread.interrupt();
}
/*
* (non-Javadoc)
*
* @see io.socket.IOTransport#send(java.lang.String)
*/
@Override
public void send(String text) throws IOException {
sendBulk(new String[] { text });
}
/*
* (non-Javadoc)
*
* @see io.socket.IOTransport#canSendBulk()
*/
@Override
public boolean canSendBulk() {
return true;
}
/*
* (non-Javadoc)
*
* @see io.socket.IOTransport#sendBulk(java.lang.String[])
*/
@Override
public void sendBulk(String[] texts) throws IOException {
queue.addAll(Arrays.asList(texts));
if (isBlocked()) {
pollThread.interrupt();
urlConnection.disconnect();
}
}
/*
* (non-Javadoc)
*
* @see io.socket.IOTransport#invalidate()
*/
@Override
public void invalidate() {
this.connection = null;
}
/**
* Checks if is connect.
*
* @return true, if is connect
*/
private synchronized boolean isConnect() {
return connect;
}
/**
* Sets the connect.
*
* @param connect
* the new connect
*/
private synchronized void setConnect(boolean connect) {
this.connect = connect;
}
/**
* Checks if is blocked.
*
* @return true, if is blocked
*/
private synchronized boolean isBlocked() {
return blocked;
}
/**
* Sets the blocked.
*
* @param blocked
* the new blocked
*/
private synchronized void setBlocked(boolean blocked) {
this.blocked = blocked;
}
@Override
public String getName() {
return TRANSPORT_NAME;
}
}