예제 #1
0
  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();
  }
  public static void main(String[] args) {

    // create a basic server that listens on port 8080.
    final HttpServer server = HttpServer.createSimpleServer();

    final ServerConfiguration config = server.getServerConfiguration();

    // Map the path, /echo, to the NonBlockingEchoHandler
    config.addHttpHandler(new NonBlockingEchoHandler(), "/echo");

    try {
      server.start();
      Client client = new Client();
      client.run();
    } catch (IOException ioe) {
      LOGGER.log(Level.SEVERE, ioe.toString(), ioe);
    } finally {
      server.shutdownNow();
    }
  }