I am looking for simple implementation of server and client by using SSE (simple sent event) protocol by JAVA language.I know that may possible by jetty or jersy, but looking for simple example to start.
我正在通过使用JAVA语言的SSE(简单发送事件)协议来寻找服务器和客户端的简单实现。我知道可能通过jetty或jersy,但寻找简单的示例来启动。
1 个解决方案
#1
3
You can do something like this:
你可以这样做:
Add dependency of jersey or add jar
添加球衣的依赖性或添加jar
Add dependency of or add jar in your project like
在项目中添加依赖项或添加jar
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-sse</artifactId>
</dependency>
Then create a class for server like:
然后为服务器创建一个类,如:
@Path("events")
public static class SseResource {
@GET
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput getServerSentEvents() {
final EventOutput eventOutput = new EventOutput();
new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
// ... code that waits 1 second
final OutboundEvent.Builder eventBuilder
= new OutboundEvent.Builder();
eventBuilder.name("message-to-client");
eventBuilder.data(String.class,
"Hello world " + i + "!");
final OutboundEvent event = eventBuilder.build();
eventOutput.write(event);
}
} catch (IOException e) {
throw new RuntimeException(
"Error when writing the event.", e);
} finally {
try {
eventOutput.close();
} catch (IOException ioClose) {
throw new RuntimeException(
"Error when closing the event output.", ioClose);
}
}
}
}).start();
return eventOutput;
}
}
Then on Client use following code:
然后在客户端上使用以下代码:
Client client = ClientBuilder.newBuilder()
.register(SseFeature.class).build();
WebTarget target = client.target("http://localhost:9998/events");
EventInput eventInput = target.request().get(EventInput.class);
while (!eventInput.isClosed()) {
final InboundEvent inboundEvent = eventInput.read();
if (inboundEvent == null) {
// connection has been closed
break;
}
System.out.println(inboundEvent.getName() + "; "
+ inboundEvent.readData(String.class));
}
Check the following link as well as they have detailed information with simple example.
检查以下链接以及它们的详细信息和简单示例。
https://jersey.java.net/documentation/latest/sse.html
http://viralpatel.net/blogs/html5-server-sent-events-java-servlets-example/
http://en.kodcu.com/2013/11/jaxrs-2-html-5-server-sent-events-on-glassfish-4/
#1
3
You can do something like this:
你可以这样做:
Add dependency of jersey or add jar
添加球衣的依赖性或添加jar
Add dependency of or add jar in your project like
在项目中添加依赖项或添加jar
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-sse</artifactId>
</dependency>
Then create a class for server like:
然后为服务器创建一个类,如:
@Path("events")
public static class SseResource {
@GET
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput getServerSentEvents() {
final EventOutput eventOutput = new EventOutput();
new Thread(new Runnable() {
@Override
public void run() {
try {
for (int i = 0; i < 10; i++) {
// ... code that waits 1 second
final OutboundEvent.Builder eventBuilder
= new OutboundEvent.Builder();
eventBuilder.name("message-to-client");
eventBuilder.data(String.class,
"Hello world " + i + "!");
final OutboundEvent event = eventBuilder.build();
eventOutput.write(event);
}
} catch (IOException e) {
throw new RuntimeException(
"Error when writing the event.", e);
} finally {
try {
eventOutput.close();
} catch (IOException ioClose) {
throw new RuntimeException(
"Error when closing the event output.", ioClose);
}
}
}
}).start();
return eventOutput;
}
}
Then on Client use following code:
然后在客户端上使用以下代码:
Client client = ClientBuilder.newBuilder()
.register(SseFeature.class).build();
WebTarget target = client.target("http://localhost:9998/events");
EventInput eventInput = target.request().get(EventInput.class);
while (!eventInput.isClosed()) {
final InboundEvent inboundEvent = eventInput.read();
if (inboundEvent == null) {
// connection has been closed
break;
}
System.out.println(inboundEvent.getName() + "; "
+ inboundEvent.readData(String.class));
}
Check the following link as well as they have detailed information with simple example.
检查以下链接以及它们的详细信息和简单示例。
https://jersey.java.net/documentation/latest/sse.html
http://viralpatel.net/blogs/html5-server-sent-events-java-servlets-example/
http://en.kodcu.com/2013/11/jaxrs-2-html-5-server-sent-events-on-glassfish-4/