Created
January 19, 2024 10:31
-
-
Save shvyrev/dc9584d5f9f557ec4ad71554bfc0a7fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.kaller; | |
import io.quarkus.runtime.StartupEvent; | |
import io.socket.client.AckWithTimeout; | |
import io.socket.client.IO; | |
import io.socket.client.Socket; | |
import io.socket.engineio.client.transports.Polling; | |
import io.socket.engineio.client.transports.WebSocket; | |
import io.vertx.core.json.JsonObject; | |
import jakarta.annotation.Priority; | |
import jakarta.enterprise.context.ApplicationScoped; | |
import jakarta.enterprise.event.Observes; | |
import jakarta.inject.Inject; | |
import jakarta.interceptor.Interceptor; | |
import org.eclipse.microprofile.rest.client.inject.RestClient; | |
import org.jboss.logging.Logger; | |
import java.net.URI; | |
import java.time.OffsetDateTime; | |
import java.time.ZoneOffset; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.UUID; | |
@ApplicationScoped | |
public class App { | |
@RestClient | |
KeycloakAuthService keycloakAuthService; | |
@Inject | |
KeycloakConfig keycloakConfig; | |
private static final Logger log = Logger.getLogger( App.class ); | |
public void onStart(@Observes @Priority(Interceptor.Priority.LIBRARY_BEFORE) StartupEvent startupEvent){ | |
log.info("$ "+ "onStart() called with: startupEvent = [" + startupEvent + "]"); | |
UUID device = UUID.randomUUID(); | |
String user = "+79970000017#10"; | |
String password = "e686be66d5e7dcecb68060da8fbdcdb8704c3fa7c4529a1e3f7e946472bd68e4"; | |
var auth = authUser(user, password, device); | |
var accessToken = auth.access_token(); | |
long ifModifiedSince = OffsetDateTime.now(ZoneOffset.UTC) | |
.minusMonths(1) | |
.toEpochSecond(); | |
var headers = Map.of( | |
"Authorization", List.of("Bearer " + accessToken) | |
, "User-Agent", List.of("os=iOS&os_version=1978.6.1&device=iPhone 111 Pro Max") | |
, "Content-Type", List.of("application/json") | |
, "If-Unmodified-Since", List.of(ifModifiedSince + "") | |
); | |
var contact = new JsonObject() | |
.put("number","+79773021878#10") | |
.put("name","user name"); | |
var event = new JsonObject() | |
.put("aggregateType", "Contacts") | |
.put("payload", contact.encode()) | |
.put("type", "CreateContact"); | |
var socket = createSocket("https://qa-sockets.kaller.app/data", headers); | |
socket.connect() | |
.onAnyIncoming(objects -> log.info("$ on any incoming " + Arrays.toString(objects))); | |
socket.emit("contacts", event.encode(), new AckWithTimeout(5_000) { | |
@Override | |
public void onSuccess(Object... args) { | |
log.info("$ "+ "onSuccess() called with: args = [" + Arrays.toString(args) + "]"); | |
} | |
@Override | |
public void onTimeout() { | |
log.error("$ onTimeout " + ""); | |
} | |
}) | |
; | |
} | |
private Socket createSocket(String path, Map<String, List<String>> headers) { | |
URI uri = URI.create(path); | |
IO.Options options = IO.Options.builder() | |
// IO factory options | |
.setForceNew(false) | |
.setMultiplex(true) | |
// low-level engine options | |
.setTransports(new String[] { Polling.NAME, WebSocket.NAME }) | |
.setUpgrade(true) | |
.setRememberUpgrade(false) | |
// .setPath("/socket.io/") | |
.setQuery(null) | |
.setExtraHeaders(null) | |
// Manager options | |
.setReconnection(true) | |
.setReconnectionAttempts(Integer.MAX_VALUE) | |
.setReconnectionDelay(1_000) | |
.setReconnectionDelayMax(5_000) | |
.setRandomizationFactor(0.5) | |
.setTimeout(20_000) | |
.setExtraHeaders(headers) | |
.setReconnection(true) | |
.build(); | |
return IO.socket(uri, options); | |
} | |
private KeycloakAuthResult authUser(String user, String password, UUID device) { | |
log.info("$ "+ "authUser() called with: user = [" + user + "], password = [" + password + "]"); | |
return keycloakAuthService.auth(KeycloakAuthForm.of(keycloakConfig, user, password), device); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment