RealTime and openChat

This commit is contained in:
2025-06-30 22:22:08 +03:30
parent 5afe3662d4
commit ee037532a1
20 changed files with 2222 additions and 275 deletions
@@ -9,6 +9,8 @@ import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class TelegramClient {
private static final String SERVER_HOST = "localhost";
@@ -18,6 +20,7 @@ public class TelegramClient {
private PrintWriter out;
private final Scanner scanner;
ActionHandler handler = null;
public static BlockingQueue<JSONObject> responseQueue = new LinkedBlockingQueue<>();
public TelegramClient() {
this.scanner = new Scanner(System.in);
@@ -28,76 +31,52 @@ public class TelegramClient {
this.socket = new Socket(SERVER_HOST, SERVER_PORT);
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
this.out = new PrintWriter(this.socket.getOutputStream(), true);
System.out.println(" Connected to Telegram Server");
System.out.println(" Connected to Telegram Server");
this.handler = new ActionHandler(this.out, this.in, this.scanner);
this.showMainMenu();
} catch (IOException e) {
System.err.println("Error connecting to server: " + e.getMessage());
}
// 👂 فقط این ترد مجاز به خواندن از in است
Thread listenerThread = new Thread(new IncomingMessageListener(in));
listenerThread.setDaemon(true);
listenerThread.start();
showMainMenu();
} catch (IOException e) {
System.err.println("❌ Error connecting to server: " + e.getMessage());
}
}
private void showMainMenu() {
while(true) {
while (true) {
System.out.println("Main Menu:");
System.out.println("1. Register");
System.out.println("2. Login");
System.out.println("3. Exit");
System.out.print("Choose an option: ");
switch (this.scanner.nextLine()) {
case "1":
this.handler.register();
break;
case "2":
this.handler.loginHandler();
String choice = scanner.nextLine();
switch (choice) {
case "1" -> handler.register();
case "2" -> {
handler.loginHandler();
if (Session.currentUser != null) {
System.out.println("Login successful.");
UUID internalId = UUID.fromString(Session.currentUser.getString("internalUUID"));
//Thread listenerThread = new Thread(new IncomingMessageListener(in));
//listenerThread.setDaemon(true);
//listenerThread.start();
this.handler.userMenu(internalId);
System.out.println("Login successful.");
UUID internalId = UUID.fromString(Session.currentUser.getString("internal_uuid"));
handler.userMenu(internalId);
} else {
System.out.println("Login failed.");
System.out.println("Login failed.");
}
break;
case "3":
System.out.println("Disconnecting...");
if (Session.currentUser != null && Session.currentUser.has("internalUUID")) {
try {
JSONObject logoutRequest = new JSONObject();
logoutRequest.put("action", "logout");
logoutRequest.put("user_id", Session.currentUser.getString("internalUUID"));
out.println(logoutRequest.toString());
in.readLine();
} catch (Exception e) {
System.err.println("Failed to notify server on logout: " + e.getMessage());
}
}
try {
if (socket != null) socket.close();
if (in != null) in.close();
if (out != null) out.close();
System.out.println("Disconnected.");
} catch (IOException e) {
System.err.println("Error closing connection: " + e.getMessage());
}
}
case "3" -> {
System.out.println("Exiting...");
return;
default:
System.out.println("Invalid choice. Please try again.");
}
default -> System.out.println("Invalid choice.");
}
}
}
public static void main(String[] args) {
TelegramClient client = new TelegramClient();
client.start();
new TelegramClient().start();
}
}