Skip to content

Commit 7cf2350

Browse files
committed
Agrego el EB en el cliente JS y el Veticle Principal
1 parent f061648 commit 7cf2350

File tree

3 files changed

+136
-28
lines changed

3 files changed

+136
-28
lines changed

Diff for: webserver/src/main/groovy/com/makingdevs/MainVerticle.groovy

+36-28
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,53 @@ package com.makingdevs
22

33
import io.vertx.core.AbstractVerticle
44
import io.vertx.ext.web.Router
5+
import io.vertx.ext.bridge.PermittedOptions
6+
import io.vertx.ext.web.handler.sockjs.SockJSBridgeOptions
7+
import io.vertx.ext.web.handler.sockjs.SockJSHandler
8+
import io.vertx.reactivex.ext.shell.*
9+
import io.vertx.ext.shell.term.TelnetTermOptions
10+
import io.vertx.ext.shell.ShellServiceOptions
11+
import io.vertx.ext.shell.ShellService
512

613
class MainVerticle extends AbstractVerticle {
714

8-
private List clients = []
9-
1015
@Override
1116
void start() {
12-
Router router = Router.router(vertx);
13-
14-
router.route("/ws").handler { ctx ->
15-
ctx.request().toWebSocket().onSuccess { ws ->
16-
println "Nuevo cliente ${ws.properties}"
17-
clients << ws
18-
19-
ws.textMessageHandler { message ->
20-
println "Mensaje recibido: ${message}"
21-
// broadcastMessage("Cliente: $message")
22-
clients.each { c ->
23-
c.writeTextMessage(message)
24-
}
25-
}
26-
27-
ws.closeHandler {
28-
println "Cliente desconectado"
29-
clients.remove(ws)
30-
}
31-
32-
ws.exceptionHandler { error ->
33-
println "ERROR en WS: ${error.message}"
34-
clients.remove(ws)
35-
}
36-
}
17+
Router router = Router.router(vertx)
18+
19+
PermittedOptions permittedOptions = new PermittedOptions()
20+
.setAddressRegex("chat\\..+");
21+
22+
SockJSHandler sockJSHandler = SockJSHandler.create(vertx)
23+
SockJSBridgeOptions options = new SockJSBridgeOptions()
24+
.addInboundPermitted(permittedOptions)
25+
.addOutboundPermitted(permittedOptions)
26+
27+
router.route("/eventbus/*").subRouter(sockJSHandler.bridge(options))
28+
29+
vertx.eventBus().consumer("chat.publish") { msg ->
30+
println "Msg recibido en 'chat.general': ${msg.body()}"
31+
vertx.eventBus().publish("chat.general", msg.body())
3732
}
3833

34+
// vertx.setPeriodic(10000) { id ->
35+
// vertx.eventBus().publish("chat.general", "Timestamp: ${new Date()}")
36+
// }
37+
38+
ShellService service = ShellService.create(vertx,
39+
new ShellServiceOptions().setTelnetOptions(
40+
new TelnetTermOptions().
41+
setHost("localhost").
42+
setPort(4000)
43+
)
44+
);
45+
service.start();
46+
3947
vertx.createHttpServer()
4048
.requestHandler(router)
4149
.listen(8082) { http ->
4250
if(http.succeeded())
43-
println "Servidor HTTP en http://localhost:8082"
51+
println "Servidor HTTP en http://localhost:8082/eventbus"
4452
else
4553
println "Error al iniciar el server: ${http.cause()}"
4654

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.makingdevs
2+
3+
import io.vertx.core.AbstractVerticle
4+
import io.vertx.ext.web.Router
5+
6+
class MainVerticleModulo7 extends AbstractVerticle {
7+
8+
private List clients = []
9+
10+
@Override
11+
void start() {
12+
Router router = Router.router(vertx);
13+
14+
router.route("/ws").handler { ctx ->
15+
ctx.request().toWebSocket().onSuccess { ws ->
16+
println "Nuevo cliente ${ws.properties}"
17+
clients << ws
18+
19+
ws.textMessageHandler { message ->
20+
println "Mensaje recibido: ${message}"
21+
// broadcastMessage("Cliente: $message")
22+
clients.each { c ->
23+
c.writeTextMessage(message)
24+
}
25+
}
26+
27+
ws.closeHandler {
28+
println "Cliente desconectado"
29+
clients.remove(ws)
30+
}
31+
32+
ws.exceptionHandler { error ->
33+
println "ERROR en WS: ${error.message}"
34+
clients.remove(ws)
35+
}
36+
}
37+
}
38+
39+
vertx.createHttpServer()
40+
.requestHandler(router)
41+
.listen(8082) { http ->
42+
if(http.succeeded())
43+
println "Servidor HTTP en http://localhost:8082"
44+
else
45+
println "Error al iniciar el server: ${http.cause()}"
46+
47+
}
48+
}
49+
}

Diff for: webserver/src/main/resources/eventbus-client.html

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>WS Client</title>
6+
<script src="https://unpkg.io/[email protected]/dist/sockjs.min.js"></script>
7+
<script src='https://unpkg.io/@vertx/[email protected]/vertx-eventbus.js'></script>
8+
</head>
9+
<body>
10+
<h1>Chat de WS</h1>
11+
<div>
12+
<label for="messae">Message:</label>
13+
<input type="text" id="message" />
14+
<button onclick="sendMessage()">Enviar</button>
15+
</div>
16+
<div id="messages">
17+
<h2>Messages</h2>
18+
</div>
19+
20+
<script>
21+
22+
const eventBus = new EventBus('http://localhost:8082/eventbus');
23+
const messages = document.getElementById("messages");
24+
25+
eventBus.onopen = () => {
26+
console.log("Conexión al EB establecida");
27+
28+
eventBus.registerHandler("chat.general", (err, message) => {
29+
if(err){
30+
console.error("Error:", err);
31+
} else {
32+
const p = document.createElement("p");
33+
p.textContent = message.body;
34+
messages.appendChild(p);
35+
}
36+
});
37+
};
38+
39+
function sendMessage() {
40+
const message = document.getElementById("message");
41+
const textMessage = message.value;
42+
if(message) {
43+
eventBus.publish("chat.publish", textMessage);
44+
message.value = "";
45+
}
46+
}
47+
48+
</script>
49+
50+
</body>
51+
</html>

0 commit comments

Comments
 (0)