@@ -2,45 +2,53 @@ package com.makingdevs
2
2
3
3
import io.vertx.core.AbstractVerticle
4
4
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
5
12
6
13
class MainVerticle extends AbstractVerticle {
7
14
8
- private List clients = []
9
-
10
15
@Override
11
16
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())
37
32
}
38
33
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
+
39
47
vertx. createHttpServer()
40
48
.requestHandler(router)
41
49
.listen(8082 ) { http ->
42
50
if (http. succeeded())
43
- println " Servidor HTTP en http://localhost:8082"
51
+ println " Servidor HTTP en http://localhost:8082/eventbus "
44
52
else
45
53
println " Error al iniciar el server: ${ http.cause()} "
46
54
0 commit comments