Search in sources :

Example 1 with MessagePacket

use of de.datasecs.hydra.example.shared.chat.MessagePacket 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

Client (de.datasecs.hydra.client.Client)1 HydraClient (de.datasecs.hydra.client.HydraClient)1 MessagePacket (de.datasecs.hydra.example.shared.chat.MessagePacket)1 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 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1