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!");
}
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());
}
Aggregations