Exemple #1
0
  /**
   * Create a HTTP proxy.
   *
   * @param host hostName or IP, where the proxy will listen.
   * @param port port, where the proxy will listen.
   */
  public GrizzlyModProxy(String host, int port) {
    server = HttpServer.createSimpleServer("/", host, port);
    proxyFilter = new ProxyFilter();
    server
        .getListener("grizzly")
        .registerAddOn(
            (networkListener, builder) -> {
              int httpServerFilterIdx = builder.indexOfType(HttpServerFilter.class);

              if (httpServerFilterIdx >= 0) {
                // Insert the WebSocketFilter right before HttpServerFilter
                builder.add(httpServerFilterIdx, proxyFilter);
              }
            });
  }
  public static void main(String[] args) throws Exception {
    final Server server = new DefaultServer();
    server.socketAction(
        new Action<ServerSocket>() {
          @Override
          public void on(final ServerSocket socket) {
            System.out.println("on socket: " + socket.uri());
            socket.on(
                "echo",
                new Action<Object>() {
                  @Override
                  public void on(Object data) {
                    System.out.println("on echo event: " + data);
                    socket.send("echo", data);
                  }
                });
            socket.on(
                "chat",
                new Action<Object>() {
                  @Override
                  public void on(Object data) {
                    System.out.println("on chat event: " + data);
                    server.all().send("chat", data);
                  }
                });
          }
        });

    HttpTransportServer httpTransportServer = new HttpTransportServer().transportAction(server);
    WebSocketTransportServer wsTransportServer =
        new WebSocketTransportServer().transportAction(server);

    HttpServer httpServer = HttpServer.createSimpleServer();
    ServerConfiguration config = httpServer.getServerConfiguration();
    config.addHttpHandler(new VibeHttpHandler().httpAction(httpTransportServer), "/vibe");
    NetworkListener listener = httpServer.getListener("grizzly");
    listener.registerAddOn(new WebSocketAddOn());
    WebSocketEngine.getEngine()
        .register("", "/vibe", new VibeWebSocketApplication().wsAction(wsTransportServer));
    httpServer.start();
    System.in.read();
  }
Exemple #3
0
  public static void main(String[] args) throws Exception {

    BufferedReader br = null;
    String line = null;

    SocketServer socketServer = new SocketServer();

    HttpServer server = HttpServer.createSimpleServer();
    server.getListener("grizzly").registerAddOn(new WebSocketAddOn());
    WebSocketEngine.getEngine().register("/", socketServer);
    server.start();

    List<WebSocket> sockets = socketServer.getSockets();
    br = new BufferedReader(new InputStreamReader(System.in));

    while ((line = br.readLine()) != null) {
      // desde el servidor al los browsers
      for (WebSocket webSocket : sockets) {
        webSocket.send(line);
      }
    }

    server.stop();
  }