Search in sources :

Example 11 with Session

use of de.datasecs.hydra.shared.handler.Session in project Hydra by DataSecs.

the class ChatClient method main.

public static void main(String[] args) {
    HydraClient hydraClient = new Client.Builder("localhost", 8888, new ChatClientProtocol()).addSessionListener(new HydraSessionListener() {

        @Override
        public void onConnected(Session session) {
            System.out.println("You are connected to the server!");
        }

        @Override
        public void onDisconnected(Session session) {
            System.out.printf("You were disconnected from the server with ip: %s%n", session.getAddress());
        }
    }).option(ChannelOption.TCP_NODELAY, true).build();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    String input;
    MessagePacket messagePacket = new MessagePacket();
    while (true) {
        try {
            input = bufferedReader.readLine();
            if (input.equalsIgnoreCase("#end")) {
                System.out.println("Disconnecting from chat...");
                hydraClient.send(new ServerPacket("disconnect"));
                hydraClient.close();
                System.out.println("Disconnected!");
                return;
            }
            messagePacket.setMessage(String.format("%s;%s;%s", hydraClient.getLocalAddress(), Calendar.getInstance().getTime().toString(), input));
            hydraClient.send(messagePacket);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) HydraSessionListener(de.datasecs.hydra.shared.handler.listener.HydraSessionListener) MessagePacket(de.datasecs.hydra.example.shared.chat.MessagePacket) HydraClient(de.datasecs.hydra.client.HydraClient) ServerPacket(de.datasecs.hydra.example.shared.chat.ServerPacket) BufferedReader(java.io.BufferedReader) HydraClient(de.datasecs.hydra.client.HydraClient) Client(de.datasecs.hydra.client.Client) Session(de.datasecs.hydra.shared.handler.Session)

Aggregations

Session (de.datasecs.hydra.shared.handler.Session)11 HydraSessionListener (de.datasecs.hydra.shared.handler.listener.HydraSessionListener)11 Client (de.datasecs.hydra.client.Client)5 HydraClient (de.datasecs.hydra.client.HydraClient)5 HydraServer (de.datasecs.hydra.server.HydraServer)5 ServerPacket (de.datasecs.hydra.example.shared.chat.ServerPacket)2 ArrayList (java.util.ArrayList)2 SampleProtocol (client.SampleProtocol)1 SamplePacket (client.packets.SamplePacket)1 SampleSerializationPacket (client.packets.SampleSerializationPacket)1 TestClientProtocol (de.datasecs.hydra.client.TestClientProtocol)1 ExamplePacket (de.datasecs.hydra.example.shared.ExamplePacket)1 MessagePacket (de.datasecs.hydra.example.shared.chat.MessagePacket)1 CustomClass (de.datasecs.hydra.example.shared.serialization.CustomClass)1 CustomClassExtended (de.datasecs.hydra.example.shared.serialization.CustomClassExtended)1 ExampleSerializationPacket (de.datasecs.hydra.example.shared.serialization.ExampleSerializationPacket)1 Server (de.datasecs.hydra.server.Server)1 TestServerProtocol (de.datasecs.hydra.server.TestServerProtocol)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1