Beispiel #1
0
  public void start() {
    server = new HttpServer();

    server.requestHandler(
        new Handler<HttpServerRequest>() {
          public void handle(HttpServerRequest req) {
            if (req.path.equals("/")) req.response.sendFile("sockjs/index.html"); // Serve the html
          }
        });

    SockJSServer sockServer = new SockJSServer(server);

    sockServer.installApp(
        new AppConfig().setPrefix("/testapp"),
        new Handler<SockJSSocket>() {
          public void handle(final SockJSSocket sock) {
            sock.dataHandler(
                new Handler<Buffer>() {
                  public void handle(Buffer data) {
                    sock.writeBuffer(data); // Echo it back
                  }
                });
          }
        });

    server.listen(8080);
  }
Beispiel #2
0
  public void startup(@Observes ProviderStartupEvent event) {
    server = westtyVertx.createHttpServer();
    JsonArray permitted = new JsonArray();
    // Let everything through
    permitted.add(new JsonObject());

    SockJSServer sockJSServer = westtyVertx.createSockJSServer(server);
    sockJSServer.bridge(new JsonObject().putString("prefix", "/eventbus"), permitted, permitted);
    server.listen(config.getHttpPort());
    log.info("SockJs started on port " + config.getHttpPort());
    endpoints.start(bus);
    log.info("EventBus started on port " + config.getEventBusPort());
  }
  /*
   * The following method is called when either a HTTP handler (for use with SockJS) or a SockJS handler has been
   * added. Null must be passed in as the other service reference. If there is a matching pair of handlers, then a suitable
   * server is created, both handlers are registered, and the server is set listening. If there is no SockJS handler yet, then
   * a HTTP server is created and SockJS support added, the HTTP handler registered, and the server is set listening. If there
   * is a SockJS handler but no HTTP handler yet, then no state change occurs as the HTTP handler needs to arrive before the
   * HTTP server is set up.
   */
  @SuppressWarnings("unchecked")
  private void sockJSHandlerAdded(
      final int portNumber,
      final ServiceReference<Handler<?>> httpHandlerServiceReference,
      final ServiceReference<Handler<?>> sockJSHandlerServiceReference) {

    final Set<ServiceReference<?>> sockJSHandlerRefs =
        findHandlerRefs(portNumber, "(type=" + SOCK_JS_HANDLER_TYPE + ")");
    if (sockJSHandlerServiceReference != null) {
      sockJSHandlerRefs.add(sockJSHandlerServiceReference);
    }

    final Set<ServiceReference<?>> httpHandlerRefs =
        findHandlerRefs(portNumber, "(type=" + HTTP_SERVER_REQUEST_HANDLER_TYPE + ")");
    final Set<ServiceReference<?>> discardRefs = new HashSet<ServiceReference<?>>();
    for (final ServiceReference<?> httpHandlerRef : httpHandlerRefs) {
      if (!hasProtocol(httpHandlerRef, SOCKJS_PROTOCOL)) {
        discardRefs.add(httpHandlerRef);
      }
    }
    httpHandlerRefs.removeAll(discardRefs);
    if (httpHandlerServiceReference != null) {
      httpHandlerRefs.add(httpHandlerServiceReference);
    }

    if (!httpHandlerRefs.isEmpty() && sockJSHandlerRefs.isEmpty()) {
      /* Could create HTTP server, but then we would need to keep track of it, so
       * defer until fixing github issue 1. */
    } else if (httpHandlerRefs.isEmpty() && !sockJSHandlerRefs.isEmpty()) {
      // Defer until HTTP handler is available - see github issue 1.
    } else if (!httpHandlerRefs.isEmpty() && !sockJSHandlerRefs.isEmpty()) {
      // One of each - proceed
      final ServiceReference<?> sRef = sockJSHandlerRefs.iterator().next();
      final ServiceReference<Handler<SockJSSocket>> sockJSRef =
          (ServiceReference<Handler<SockJSSocket>>) sRef;

      final ServiceReference<?> hRef = httpHandlerRefs.iterator().next();
      final ServiceReference<Handler<HttpServerRequest>> httpRef =
          (ServiceReference<Handler<HttpServerRequest>>) hRef;

      final Handler<SockJSSocket> sockJSHandler = this.bundleContext.getService(sockJSRef);
      final Handler<HttpServerRequest> httpHandler = this.bundleContext.getService(httpRef);
      try {
        final HttpServer httpServer = registerHandler(portNumber, httpHandler);

        final SockJSServer sockServer = this.vertx.createSockJSServer(httpServer);

        sockServer.installApp(
            new JsonObject()
                .putString(
                    "prefix",
                    getHandlerPrefix(
                        (ServiceReference<Handler<?>>) (ServiceReference<?>) sockJSRef)),
            sockJSHandler);

        listen(httpServer, portNumber);
      } finally {
        this.bundleContext.ungetService(sockJSRef);
        this.bundleContext.ungetService(httpRef);
      }
    }
  }