Exemplo n.º 1
0
  void processChatUpdate(ChatUpdateMessage message) {
    String g, m, k;
    m = message.getUsername();
    g = message.getMessage();
    k = message.getRec();

    try {

      String url = "jdbc:mysql://localhost:3306/chat";
      Connection conn = DriverManager.getConnection(url, "root", "root");
      Statement st = conn.createStatement();
      st.executeUpdate(
          "INSERT INTO Log(User,Message,Recepient) "
              + "VALUE ('"
              + m
              + "','"
              + g
              + "','"
              + k
              + "')");

    } catch (Exception e) {
      System.err.println("Got an exception! ");
      System.err.println(e.getMessage());
    }

    this.addMessage(message.getMessage(), message.getRec());
  }
Exemplo n.º 2
0
  private boolean processWatchKey(WatchKey watchKey) {
    for (WatchEvent<?> event : watchKey.pollEvents()) {
      if (StandardWatchEventKinds.OVERFLOW == event.kind()) continue;
      try {
        session.getBasicRemote().sendObject(((Path) event.context()).toFile());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    return watchKey.reset();
  }
Exemplo n.º 3
0
  void processNewUser(NewUserMessage message) {
    String newUsername = this.validateUsername(message.getUsername());
    NewUserMessage uMessage = new NewUserMessage(newUsername);
    try {
      session.getBasicRemote().sendObject(uMessage);
    } catch (IOException | EncodeException ioe) {
      System.out.println(
          "Error signing " + message.getUsername() + " into chat : " + ioe.getMessage());
    }
    this.registerUser(newUsername);

    try {
      ResultSet rs = null, ds = null;
      String z, x;
      String url1 = "jdbc:mysql://localhost:3306/chat";
      Connection conn1 = DriverManager.getConnection(url1, "root", "root");
      Statement st1 = conn1.createStatement();
      Statement st2 = conn1.createStatement();
      String myQuery = "SELECT Message,Recepient FROM Log WHERE User = '******'";
      System.out.println(myQuery);
      rs =
          st1.executeQuery("SELECT Message,Recepient FROM Log WHERE User = '******';");
      while (rs.next()) {
        z = rs.getString("Message");
        x = rs.getString("Recepient");
        ChatUpdateMessage cdm1 = new ChatUpdateMessage(newUsername, z, x);
        try {
          session.getBasicRemote().sendObject(cdm1);
        } catch (IOException | EncodeException ex) {
          System.out.println("Error updating a client : " + ex.getMessage());
        }
      }

      ds =
          st2.executeQuery("SELECT Message,User FROM Log WHERE Recepient = '" + newUsername + "';");
      while (ds.next()) {
        z = ds.getString("Message");
        x = ds.getString("User");
        ChatUpdateMessage cdm2 = new ChatUpdateMessage(x, z, newUsername);
        try {
          session.getBasicRemote().sendObject(cdm2);
        } catch (IOException | EncodeException ex) {
          System.out.println("Error updating a client : " + ex.getMessage());
        }
      }

    } catch (Exception e) {
      System.err.println("Got an exception! ");
      System.err.println(e.getMessage());
    }

    this.broadcastUserListUpdate();
  }
  @OnOpen
  public void onOpen(Session session) {
    try {
      User person = new User();
      person.setName("john");
      person.setSurname("smith");
      person.setEmail("*****@*****.**");

      session.getBasicRemote().sendObject(person);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }