SHA256
107 lines
4.1 KiB
Java
107 lines
4.1 KiB
Java
import java.net.URI;
|
|
import java.net.http.HttpClient;
|
|
import java.net.http.WebSocket;
|
|
import java.net.http.WebSocket.Listener;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.concurrent.CompletionStage;
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
|
public class TestJsonWsClient {
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
String uri = "ws://localhost:7070/ws";
|
|
|
|
|
|
String jsonRequestSessionRefresh = """
|
|
{
|
|
"op": "SessionRefresh",
|
|
"requestId": "test-1",
|
|
"sessionId": 123,
|
|
"sessionPwd": "test-password"
|
|
}
|
|
""";
|
|
|
|
String jsonRequestAddUser = """
|
|
{
|
|
"op": "AddUser",
|
|
"requestId": "test-add-1",
|
|
"login": "anya1111",
|
|
"loginId": 100211,
|
|
"bchId": 4222,
|
|
"pubkey0": "PUB0",
|
|
"pubkey1": "PUB1",
|
|
"bchLimit": 1000000
|
|
}
|
|
""";
|
|
|
|
String jsonRequestAuthSessionNewStep1 = """
|
|
{
|
|
"op": "AuthSessionNewStep1",
|
|
"requestId": "test-auth-1",
|
|
"login": "anya1111"
|
|
}
|
|
""";
|
|
|
|
|
|
// Тестовый JSON-пакет SessionRefresh
|
|
String jsonRequest = jsonRequestAuthSessionNewStep1;
|
|
|
|
System.out.println("Подключаемся к " + uri);
|
|
|
|
CountDownLatch latch = new CountDownLatch(1);
|
|
|
|
HttpClient client = HttpClient.newHttpClient();
|
|
|
|
WebSocket webSocket = client.newWebSocketBuilder()
|
|
.buildAsync(URI.create(uri), new Listener() {
|
|
@Override
|
|
public void onOpen(WebSocket webSocket) {
|
|
System.out.println("✅ WebSocket подключен");
|
|
|
|
// Отправляем JSON сразу после подключения
|
|
System.out.println("📤 Отправляем JSON-запрос:");
|
|
System.out.println(jsonRequest);
|
|
|
|
webSocket.sendText(jsonRequest, true);
|
|
Listener.super.onOpen(webSocket);
|
|
}
|
|
|
|
@Override
|
|
public CompletionStage<?> onText(WebSocket webSocket,
|
|
CharSequence data,
|
|
boolean last) {
|
|
String message = data.toString();
|
|
System.out.println("📥 Получен TEXT-ответ от сервера:");
|
|
System.out.println(message);
|
|
|
|
// После получения первого ответа — закрываем соединение
|
|
webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "test done");
|
|
latch.countDown();
|
|
return Listener.super.onText(webSocket, data, last);
|
|
}
|
|
|
|
@Override
|
|
public void onError(WebSocket webSocket, Throwable error) {
|
|
System.out.println("❌ Ошибка WebSocket-клиента: " + error.getMessage());
|
|
error.printStackTrace(System.out);
|
|
latch.countDown();
|
|
}
|
|
|
|
|
|
@Override
|
|
public CompletionStage<?> onClose(WebSocket webSocket,
|
|
int statusCode,
|
|
String reason) {
|
|
System.out.println("🔚 Соединение закрыто. Код=" + statusCode + ", причина=" + reason);
|
|
latch.countDown();
|
|
return Listener.super.onClose(webSocket, statusCode, reason);
|
|
}
|
|
}).join();
|
|
|
|
// Ждём, пока получим ответ/ошибку/закрытие
|
|
latch.await();
|
|
System.out.println("Тест завершён, выходим.");
|
|
}
|
|
}
|