-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathEventSource.java
More file actions
68 lines (57 loc) · 2.56 KB
/
EventSource.java
File metadata and controls
68 lines (57 loc) · 2.56 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
package com.github.eventsource.client;
import com.github.eventsource.client.impl.AsyncEventSourceHandler;
import com.github.eventsource.client.impl.netty.EventSourceChannelHandler;
import org.jboss.netty.channel.ChannelFuture;
import java.net.URI;
import java.util.concurrent.Executor;
public class EventSource {
public static final long DEFAULT_RECONNECTION_TIME_MILLIS = 2000;
private final EventSourceChannelHandler clientHandler;
/**
* Creates a new <a href="http://dev.w3.org/html5/eventsource/">EventSource</a> client. The client will reconnect on
* lost connections automatically, unless the connection is closed explicitly by a call to
* {@link com.github.eventsource.client.EventSource#close()}.
*
* For sample usage, see examples at <a href="https://github.com/aslakhellesoy/eventsource-java/tree/master/src/test/java/com/github/eventsource/client">GitHub</a>.
*
* @param eventSourceClient EventSourceClient to start event source at
* @param reconnectionTimeMillis delay before a reconnect is made - in the event of a lost connection
* @param uri where to connect
* @param eventSourceHandler receives events
* @see #close()
*/
public EventSource(EventSourceClient eventSourceClient, long reconnectionTimeMillis, final URI uri, EventSourceHandler eventSourceHandler) {
clientHandler = new EventSourceChannelHandler(new AsyncEventSourceHandler(eventSourceClient.getEventExecutor(), eventSourceHandler), reconnectionTimeMillis, eventSourceClient, uri);
}
public EventSource(Executor eventExecutor, long reconnectionTimeMillis, URI uri, EventSourceHandler eventSourceHandler) {
this(new EventSourceClient(eventExecutor), reconnectionTimeMillis, uri, eventSourceHandler);
}
public EventSource(String uri, EventSourceHandler eventSourceHandler) {
this(URI.create(uri), eventSourceHandler);
}
public EventSource(URI uri, EventSourceHandler eventSourceHandler) {
this(new EventSourceClient(), DEFAULT_RECONNECTION_TIME_MILLIS, uri, eventSourceHandler);
}
public ChannelFuture connect() {
return clientHandler.connect();
}
/**
* Close the connection
*
* @return self
*/
public EventSource close() {
clientHandler.close();
return this;
}
/**
* Wait until the connection is closed
*
* @return self
* @throws InterruptedException if waiting was interrupted
*/
public EventSource join() throws InterruptedException {
clientHandler.join();
return this;
}
}