@Override
  public void start() throws Exception {
    Router router = Router.router(vertx);

    SockJSHandler sockJSHandler = SockJSHandler.create(vertx);
    BridgeOptions options = new BridgeOptions();
    // todo filter messages
    options.addInboundPermitted(new PermittedOptions().setAddressRegex(".*"));
    options.addOutboundPermitted(new PermittedOptions().setAddressRegex(".*"));
    sockJSHandler.bridge(options);

    router.route("/eventbus/*").handler(sockJSHandler);
    vertx.createHttpServer().requestHandler(router::accept).listen(8077);

    logger.debug("successfully started handler");
  }
 @DocAnnotation$annotation$(
     description =
         " Set a SockJS socket handler. This handler will be called with a SockJS socket whenever a SockJS connection\n is made from a client\n")
 @TypeInfo("io.vertx.ceylon.web.handler.sockjs::SockJSHandler")
 public SockJSHandler socketHandler(
     final @TypeInfo("ceylon.language::Anything(io.vertx.ceylon.web.handler.sockjs::SockJSSocket)")
         @Name("handler") @DocAnnotation$annotation$(description = "the handler\n") Callable<?>
             handler) {
   io.vertx.core.Handler<io.vertx.ext.web.handler.sockjs.SockJSSocket> arg_0 =
       handler == null
           ? null
           : new io.vertx.core.Handler<io.vertx.ext.web.handler.sockjs.SockJSSocket>() {
             public void handle(io.vertx.ext.web.handler.sockjs.SockJSSocket event) {
               handler.$call$(
                   (Object)
                       io.vertx.ceylon.web.handler.sockjs.SockJSSocket.TO_CEYLON
                           .converter()
                           .safeConvert(event));
             }
           };
   SockJSHandler ret =
       io.vertx.ceylon.web.handler.sockjs.SockJSHandler.TO_CEYLON
           .converter()
           .safeConvert(delegate.socketHandler(arg_0));
   return this;
 }
 @TypeInfo("ceylon.language::Anything")
 public void handle(
     final @TypeInfo("io.vertx.ceylon.web::RoutingContext") @Name("arg0") RoutingContext arg0) {
   io.vertx.ext.web.RoutingContext arg_0 =
       io.vertx.ceylon.web.RoutingContext.TO_JAVA.safeConvert(arg0);
   delegate.handle(arg_0);
 }
 @DocAnnotation$annotation$(
     description =
         " Like [bridge](../../handler/sockjs/SockJSHandler.type.html#bridge) but specifying a handler\n that will receive bridge events.\n")
 @TypeInfo("io.vertx.ceylon.web.handler.sockjs::SockJSHandler")
 public SockJSHandler bridge(
     final @TypeInfo("io.vertx.ceylon.web.handler.sockjs::BridgeOptions") @Name("bridgeOptions")
         @DocAnnotation$annotation$(description = "options to configure the bridge with\n") io
                 .vertx.ceylon.web.handler.sockjs.BridgeOptions
             bridgeOptions,
     final @TypeInfo("ceylon.language::Anything(io.vertx.ceylon.web.handler.sockjs::BridgeEvent)")
         @Name("bridgeEventHandler") @DocAnnotation$annotation$(
             description = "handler to receive bridge events\n") Callable<?> bridgeEventHandler) {
   io.vertx.ext.web.handler.sockjs.BridgeOptions arg_0 =
       bridgeOptions == null
           ? null
           : new io.vertx.ext.web.handler.sockjs.BridgeOptions(
               io.vertx.lang.ceylon.ToJava.JsonObject.convert(bridgeOptions.toJson()));
   io.vertx.core.Handler<io.vertx.ext.web.handler.sockjs.BridgeEvent> arg_1 =
       bridgeEventHandler == null
           ? null
           : new io.vertx.core.Handler<io.vertx.ext.web.handler.sockjs.BridgeEvent>() {
             public void handle(io.vertx.ext.web.handler.sockjs.BridgeEvent event) {
               bridgeEventHandler.$call$(
                   (Object)
                       io.vertx.ceylon.web.handler.sockjs.BridgeEvent.TO_CEYLON
                           .converter()
                           .safeConvert(event));
             }
           };
   SockJSHandler ret =
       io.vertx.ceylon.web.handler.sockjs.SockJSHandler.TO_CEYLON
           .converter()
           .safeConvert(delegate.bridge(arg_0, arg_1));
   return this;
 }
  @Override
  public void start() throws Exception {
    final Router router = Router.router(vertx);

    router
        .route(HttpMethod.GET, "/eventbus/*")
        .handler(
            SockJSHandler.create(vertx)
                .bridge(
                    new BridgeOptions()
                        .addOutboundPermitted(new PermittedOptions().setAddress("random"))
                        .addOutboundPermitted(new PermittedOptions().setAddress("stocks"))));

    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
  }
 @DocAnnotation$annotation$(
     description =
         " Bridge the SockJS handler to the Vert.x event bus. This basically installs a built-in SockJS socket handler\n which takes SockJS traffic and bridges it to the event bus, thus allowing you to extend the server-side\n Vert.x event bus to browsers\n")
 @TypeInfo("io.vertx.ceylon.web.handler.sockjs::SockJSHandler")
 public SockJSHandler bridge(
     final @TypeInfo("io.vertx.ceylon.web.handler.sockjs::BridgeOptions") @Name("bridgeOptions")
         @DocAnnotation$annotation$(description = "options to configure the bridge with\n") io
                 .vertx.ceylon.web.handler.sockjs.BridgeOptions
             bridgeOptions) {
   io.vertx.ext.web.handler.sockjs.BridgeOptions arg_0 =
       bridgeOptions == null
           ? null
           : new io.vertx.ext.web.handler.sockjs.BridgeOptions(
               io.vertx.lang.ceylon.ToJava.JsonObject.convert(bridgeOptions.toJson()));
   SockJSHandler ret =
       io.vertx.ceylon.web.handler.sockjs.SockJSHandler.TO_CEYLON
           .converter()
           .safeConvert(delegate.bridge(arg_0));
   return this;
 }
示例#7
0
  public static void main(String[] args) {
    System.out.println("Attempting to start the application.");

    Vertx vertx = Vertx.vertx();
    Router router = Router.router(vertx);
    EventBus bus = vertx.eventBus();

    new Thread(
            () -> {
              Connection<?> conn = null;

              try {
                conn = r.connection().hostname(DBHOST).connect();
                Cursor<HashMap> cur =
                    r.db("chat")
                        .table("messages")
                        .changes()
                        .getField("new_val")
                        .without("time")
                        .run(conn);

                while (cur.hasNext()) bus.publish("chat", new JsonObject(cur.next()));
              } catch (Exception e) {
                System.err.println("Error: changefeed failed");
              } finally {
                conn.close();
              }
            })
        .start();

    router
        .route("/eventbus/*")
        .handler(
            SockJSHandler.create(vertx)
                .bridge(
                    new BridgeOptions()
                        .addOutboundPermitted(new PermittedOptions().setAddress("chat"))));

    router
        .route(HttpMethod.GET, "/messages")
        .blockingHandler(
            ctx -> {
              Connection<?> conn = null;

              try {
                conn = r.connection().hostname(DBHOST).connect();

                List<HashMap> items =
                    r.db("chat")
                        .table("messages")
                        .orderBy()
                        .optArg("index", r.desc("time"))
                        .limit(20)
                        .orderBy("time")
                        .run(conn);

                ctx.response()
                    .putHeader("content-type", "application/json")
                    .end(Json.encodePrettily(items));
              } catch (Exception e) {
                ctx.response()
                    .setStatusCode(500)
                    .putHeader("content-type", "application/json")
                    .end("{\"success\": false}");
              } finally {
                conn.close();
              }
            });

    router.route(HttpMethod.POST, "/send").handler(BodyHandler.create());
    router
        .route(HttpMethod.POST, "/send")
        .blockingHandler(
            ctx -> {
              JsonObject data = ctx.getBodyAsJson();

              if (data.getString("user") == null || data.getString("text") == null) {
                ctx.response()
                    .setStatusCode(500)
                    .putHeader("content-type", "application/json")
                    .end("{\"success\": false, \"err\": \"Invalid message\"}");

                return;
              }

              Connection<?> conn = null;

              try {
                conn = r.connection().hostname(DBHOST).connect();

                r.db("chat")
                    .table("messages")
                    .insert(
                        r.hashMap("text", data.getString("text"))
                            .with("user", data.getString("user"))
                            .with("time", r.now()))
                    .run(conn);

                ctx.response()
                    .putHeader("content-type", "application/json")
                    .end("{\"success\": true}");
              } catch (Exception e) {
                ctx.response()
                    .setStatusCode(500)
                    .putHeader("content-type", "application/json")
                    .end("{\"success\": false}");
              } finally {
                conn.close();
              }
            });

    router.route().handler(StaticHandler.create().setWebRoot("public").setCachingEnabled(false));
    vertx.createHttpServer().requestHandler(router::accept).listen(8000);
  }