Created
January 19, 2024 10:29
-
-
Save shvyrev/401a40b7983d310f7739bf748a8d95af 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.emitter.Emitter; | |
import io.socket.engineio.client.transports.Polling; | |
import io.socket.engineio.client.transports.WebSocket; | |
import io.vertx.core.json.JsonObject; | |
import io.vertx.mutiny.core.http.HttpHeaders; | |
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 + "") | |
); | |
URI uri = URI.create("https://qa-sockets.kaller.app/data"); | |
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(); | |
Socket socket = IO.socket(uri, options); | |
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"); | |
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 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