Search in sources :

Example 1 with Server

use of de.datasecs.hydra.server.Server in project Hydra by DataSecs.

the class ChatServer method main.

public static void main(String[] args) {
    HydraServer hydraServer = new Server.Builder("localhost", 8888, new ChatServerProtocol()).addListener(new HydraSessionListener() {

        @Override
        public void onConnected(Session session) {
            System.out.printf("User with ip %s connected!%n", session.getAddress());
            session.send(new ServerPacket("Welcome at localhost user!"));
            for (Session s : sessions) {
                if (!s.equals(session)) {
                    s.send(new ServerPacket("Client with ip " + session.getAddress() + " connected to the chat!"));
                }
            }
            sessions.add(session);
        }

        @Override
        public void onDisconnected(Session session) {
            System.out.printf("User with ip %s disconnected!%n", session.getAddress());
            for (Session s : sessions) {
                if (!s.equals(session)) {
                    s.send(new ServerPacket("Client with ip " + session.getAddress() + " left the chat!"));
                }
            }
            sessions.remove(session);
        }
    }).option(ChannelOption.SO_BACKLOG, 200).childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true).build();
    System.out.println("Server started!");
}
Also used : ServerPacket(de.datasecs.hydra.example.shared.chat.ServerPacket) Server(de.datasecs.hydra.server.Server) HydraServer(de.datasecs.hydra.server.HydraServer) HydraServer(de.datasecs.hydra.server.HydraServer) HydraSessionListener(de.datasecs.hydra.shared.handler.listener.HydraSessionListener) Session(de.datasecs.hydra.shared.handler.Session)

Example 2 with Server

use of de.datasecs.hydra.server.Server in project Hydra by DataSecs.

the class UdpServer method main.

public static void main(String[] args) {
    HydraServer server = new Server.Builder("localhost", 8888, new UdpServerProtocol()).useUDP(true).childOption(ChannelOption.SO_BROADCAST, true).build();
    // Check if server is actively running (not obligatory)
    if (server.isActive()) {
        System.out.println("Server is online!");
        // Returns the local address of the server that was set in the constructor
        System.out.printf("Socket address: %s%n", server.getLocalAdress());
    }
    // As soon as a channel with a client is initialized it is added to the set of sessions
    // If no clients are connected the set is empty
    System.out.println("Sessions: " + server.getSessions());
}
Also used : Server(de.datasecs.hydra.server.Server) HydraServer(de.datasecs.hydra.server.HydraServer) HydraServer(de.datasecs.hydra.server.HydraServer)

Aggregations

HydraServer (de.datasecs.hydra.server.HydraServer)2 Server (de.datasecs.hydra.server.Server)2 ServerPacket (de.datasecs.hydra.example.shared.chat.ServerPacket)1 Session (de.datasecs.hydra.shared.handler.Session)1 HydraSessionListener (de.datasecs.hydra.shared.handler.listener.HydraSessionListener)1