Skip to content

Commit 5b6be35

Browse files
committed
Merge branch 'galderz-t_sockjs_java'
2 parents 0ff949b + 26a6a66 commit 5b6be35

File tree

3 files changed

+99
-4
lines changed

3 files changed

+99
-4
lines changed

web-examples/README.adoc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ Before running the example you need to download the dependencies, the easiest fo
134134

135135
Run the server either in your IDE or on the command line, then run the node client `npm start`.
136136

137+
== EventBus Bridge - Java client
138+
139+
The link:src/main/java/io/vertx/example/web/vertxbus/java contains an example of communication between a client and a service using the SockJS event bus. This example is written in java, and the client interacts using SockJS (in Java, using the web socket protocol).
140+
137141
== Real-time - client side event bus
138142

139143
This example demonstrates a full duplex connection between the browser and the server side.
@@ -167,18 +171,18 @@ When you get the index page in your browser you should see it update every secon
167171
== Real-time - chat service
168172

169173
This example demonstrates 2-way communication between the client and the server using the event bus bridge
170-
and web sockets.
174+
and web sockets.
171175

172176
The link:src/main/java/io/vertx/example/web/chat/Server.java[Java real-time chat example]
173177

174178
The link:src/main/java/io/vertx/example/web/chat/webroot/index.html[index.html] file
175179
bootstraps the vertxbus.js bridge from the client and uses jQuery to handle manipulating
176180
the DOM and registering event handlers.
177181

178-
When you load the index page in a browser, you should see a div for chat messages and
179-
an input field where you can enter your own messages. Typing in the input field and
182+
When you load the index page in a browser, you should see a div for chat messages and
183+
an input field where you can enter your own messages. Typing in the input field and
180184
pressing ENTER will cause the input to be sent via the event bus to the server. The server
181-
will accept the message, prepend it with a timestamp and publish back to all registered
185+
will accept the message, prepend it with a timestamp and publish back to all registered
182186
listeners (e.g. All connected clients). Take note of the addInboundPermitted and addOutboundPermitted
183187
settings on the BridgeOptions object to be sure that you authorize the correct messages
184188
to traverse the event bus bridge in the appropriate direction.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.vertx.example.web.vertxbus.java;
2+
3+
import io.vertx.core.Vertx;
4+
import io.vertx.core.http.HttpClient;
5+
import io.vertx.core.http.WebSocket;
6+
import io.vertx.core.json.JsonObject;
7+
8+
public class Client {
9+
10+
public static void main(String[] args) {
11+
Vertx vertx = Vertx.vertx();
12+
HttpClient client = vertx.createHttpClient();
13+
14+
client.websocket(8080, "localhost", "/eventbus/websocket", ws -> {
15+
System.out.println("Connected");
16+
sendPing(ws);
17+
18+
vertx.setPeriodic(5000, id -> sendPing(ws));
19+
20+
// Register
21+
JsonObject msg = new JsonObject().put("type", "register").put("address", "feed");
22+
ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(msg.encode(), true));
23+
24+
ws.handler(System.out::println);
25+
}, fail -> System.out.println("Failed: " + fail));
26+
}
27+
28+
private static void sendPing(WebSocket ws) {
29+
JsonObject msg = new JsonObject().put("type", "ping");
30+
ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(msg.encode(), true));
31+
}
32+
33+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package io.vertx.example.web.vertxbus.java;
2+
3+
import java.text.DateFormat;
4+
import java.time.Instant;
5+
import java.util.Date;
6+
7+
import io.vertx.core.AbstractVerticle;
8+
import io.vertx.core.eventbus.EventBus;
9+
import io.vertx.core.json.JsonObject;
10+
import io.vertx.example.util.Runner;
11+
import io.vertx.ext.web.Router;
12+
import io.vertx.ext.web.handler.sockjs.BridgeOptions;
13+
import io.vertx.ext.web.handler.sockjs.PermittedOptions;
14+
import io.vertx.ext.web.handler.sockjs.SockJSHandler;
15+
16+
/**
17+
* A {@link io.vertx.core.Verticle} which bridges Java to the @{link EventBus}.
18+
*/
19+
public class Server extends AbstractVerticle {
20+
21+
// Convenience method so you can run it in your IDE
22+
public static void main(String[] args) {
23+
Runner.runExample(Server.class);
24+
}
25+
26+
@Override
27+
public void start() throws Exception {
28+
29+
Router router = Router.router(vertx);
30+
31+
// Allow events for the designated addresses in/out of the event bus bridge
32+
BridgeOptions opts = new BridgeOptions()
33+
.addOutboundPermitted(new PermittedOptions().setAddress("feed"));
34+
35+
// Create the event bus bridge and add it to the router.
36+
SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts);
37+
router.route("/eventbus/*").handler(ebHandler);
38+
39+
// Start the web server and tell it to use the router to handle requests.
40+
vertx.createHttpServer().requestHandler(router::accept)
41+
.listen(8080, "localhost", ar -> {
42+
if (ar.succeeded()) {
43+
System.out.printf("Server started on localhost:%s", ar.result().actualPort());
44+
} else if (ar.failed()) {
45+
System.out.printf("Failed: %s", ar.cause());
46+
}
47+
});
48+
49+
EventBus eb = vertx.eventBus();
50+
51+
vertx.setPeriodic(1000L, t -> {
52+
// Create a timestamp string
53+
String timestamp = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(Date.from(Instant.now()));
54+
55+
eb.send("feed", new JsonObject().put("now", timestamp));
56+
});
57+
}
58+
}

0 commit comments

Comments
 (0)