All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.femo.ws.test.TestApp Maven / Gradle / Ivy

package io.femo.ws.test;

import com.google.gson.JsonElement;
import io.femo.http.Http;
import io.femo.http.handlers.FileHandler;
import io.femo.http.handlers.LoggingHandler;
import io.femo.http.middleware.EnvironmentReplacerMiddleware;
import io.femo.ws.Constants;
import io.femo.ws.WebSocket;
import io.femo.ws.WebSocketConnection;
import io.femo.ws.WebSocketEventHandler;
import io.femo.ws.lib.WebSocketLibraryConnection;
import io.femo.ws.lib.WebSocketLibraryHandler;

import java.io.File;
import java.io.IOException;

/**
 * Created by felix on 6/4/16.
 */
public class TestApp {

    public static void main(String[] args) {
        Http.server(7000)
                .get("/test", FileHandler.buffered(new File("test.html"), true, "text/html"))
                .get("/script", WebSocket.library())
                .use("/", Http.router()
                        .get("/", FileHandler.buffered(new File("index.html"), false, "text/html"))
                        .after(new EnvironmentReplacerMiddleware())
                )
                .get("/ws", WebSocket.handler().handler(new WebSocketEventHandler() {
                    @Override
                    public void handleMessage(Constants.WEBSOCKET.FRAME.DataType dataType, byte[] data, WebSocketConnection webSocketConnection) {
                        String msg = new String(data);
                        System.out.printf("Received message: %s\n", msg);
                        try {
                            webSocketConnection.send(msg);
                        } catch (IOException ignored) {
                        }
                        webSocketConnection.handler().sendToAll("Somebody connected");
                    }
                }))
                .get("/indexws", WebSocket
                        .on("test", new WebSocketLibraryHandler.Handler() {

                            @Override
                            public void handle(JsonElement data, WebSocketLibraryConnection con) {
                                System.out.println(data.getAsString());
                                con.send("test", data);
                            }
                        })
                        .on("msg", new WebSocketLibraryHandler.Handler() {

                            @Override
                            public void handle(JsonElement data, WebSocketLibraryConnection con) {
                                con.broadcast("msg", data, con);
                            }
                        })
                        .on("joined", new WebSocketLibraryHandler.Handler() {

                            @Override
                            public void handle(JsonElement data, WebSocketLibraryConnection con) {
                                con.broadcast("joined", data, con);
                            }
                        })
                        .on("renamed", new WebSocketLibraryHandler.Handler() {

                            @Override
                            public void handle(JsonElement data, WebSocketLibraryConnection con) {
                                con.broadcast("renamed", data, con);
                            }
                        })
                )
                .after(LoggingHandler.log())
                .start();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy