static {
   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);
                 }
               });
         }
       });
 }
  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();
  }
示例#3
0
 @Scheduled(fixedRate = 3000)
 public void tick() {
   server.all().send("chat", "tick: " + System.currentTimeMillis());
 }