|
1 | 1 | package com.example; |
2 | 2 |
|
| 3 | +import javafx.application.Platform; |
| 4 | + |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.InputStreamReader; |
| 7 | +import java.net.URI; |
| 8 | +import java.net.http.HttpClient; |
| 9 | +import java.net.http.HttpRequest; |
| 10 | +import java.net.http.HttpResponse; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.UUID; |
| 14 | +import java.util.concurrent.ConcurrentHashMap; |
| 15 | +import java.util.function.Consumer; |
| 16 | +import java.util.regex.Matcher; |
| 17 | +import java.util.regex.Pattern; |
| 18 | + |
3 | 19 | public class ChatModel { |
4 | 20 |
|
| 21 | + private final String sendUrl; |
| 22 | + private final String subscribeUrl; |
| 23 | + private final String clientId; |
| 24 | + private final Set<String> sentMessages = Collections.newSetFromMap(new ConcurrentHashMap<>()); |
| 25 | + |
| 26 | + public ChatModel() { |
| 27 | + String topic = System.getenv().getOrDefault("NTFY_TOPIC", "https://ntfy.sh/newchatroom3"); |
| 28 | + |
| 29 | + sendUrl = topic; |
| 30 | + subscribeUrl = topic + "/sse"; |
| 31 | + clientId = UUID.randomUUID().toString(); |
| 32 | + } |
| 33 | + |
5 | 34 | public void sendMessage(String message) { |
6 | | - // TODO: send POST JSON to ntfy |
7 | | - System.out.println("Sending message: " + message); |
| 35 | + sentMessages.add(message); |
| 36 | + |
| 37 | + new Thread(() -> { |
| 38 | + try { Thread.sleep(5000); } catch (Exception ignored) {} |
| 39 | + sentMessages.remove(message); |
| 40 | + }).start(); |
| 41 | + |
| 42 | + HttpClient client = HttpClient.newHttpClient(); |
| 43 | + |
| 44 | + HttpRequest request = HttpRequest.newBuilder() |
| 45 | + .uri(URI.create(sendUrl)) |
| 46 | + .header("Content-Type", "application/json") |
| 47 | + .header("X-Client-ID", clientId) |
| 48 | + .header("Title", "Friend: ") |
| 49 | + .POST(HttpRequest.BodyPublishers.ofString(message)) |
| 50 | + .build(); |
| 51 | + |
| 52 | + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) |
| 53 | + .thenAccept(response -> System.out.println("Sent, status=" + response.statusCode())) |
| 54 | + .exceptionally(e -> { e.printStackTrace(); return null; }); |
8 | 55 | } |
| 56 | + |
| 57 | + |
| 58 | + public void subscribe(Consumer<String> onMessageReceived) { |
| 59 | + HttpClient client = HttpClient.newHttpClient(); |
| 60 | + HttpRequest request = HttpRequest.newBuilder() |
| 61 | + .uri(URI.create(subscribeUrl)) |
| 62 | + .header("Accept", "text/event-stream") |
| 63 | + .build(); |
| 64 | + |
| 65 | + client.sendAsync(request, HttpResponse.BodyHandlers.ofInputStream()) |
| 66 | + .thenAccept(response -> { |
| 67 | + try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) { |
| 68 | + String line; |
| 69 | + boolean firstMessageSkipped = false; |
| 70 | + while ((line = reader.readLine()) != null) { |
| 71 | + if (line.startsWith("data:")) { |
| 72 | + if (!firstMessageSkipped) { |
| 73 | + firstMessageSkipped = true; |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + String raw = line.substring(5).trim(); |
| 78 | + String msg = parseMessage(raw); |
| 79 | + |
| 80 | + if (msg != null && !sentMessages.contains(msg)) { |
| 81 | + Platform.runLater(() -> onMessageReceived.accept(msg)); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } catch (Exception e) { |
| 86 | + e.printStackTrace(); |
| 87 | + } |
| 88 | + }) |
| 89 | + .exceptionally(e -> { e.printStackTrace(); return null; }); |
| 90 | + } |
| 91 | + |
| 92 | + private String parseMessage(String data) { |
| 93 | + try { |
| 94 | + Matcher eventMatcher = Pattern.compile("\"event\"\\s*:\\s*\"(.*?)\"").matcher(data); |
| 95 | + if (eventMatcher.find()) { |
| 96 | + String event = eventMatcher.group(1); |
| 97 | + |
| 98 | + if (!"message".equals(event)) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + Matcher msgMatcher = Pattern.compile("\"message\"\\s*:\\s*\"(.*?)\"").matcher(data); |
| 104 | + if (msgMatcher.find()) { |
| 105 | + return msgMatcher.group(1).replace("\\\"", "\""); |
| 106 | + } |
| 107 | + } catch (Exception ignored) {} |
| 108 | + |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + |
9 | 114 | } |
| 115 | + |
| 116 | + |
| 117 | + |
0 commit comments