Skip to content

Commit

Permalink
Add websockets example
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdgeisler committed Nov 22, 2022
1 parent a2b38bb commit d8dd076
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.socket.client.WebSocketClient;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;

import javax.net.ssl.SSLException;

public abstract class AbstractClientApi {

protected final SslContext sslContext;
protected final HttpClient httpClient;
protected final WebClient webClient;
protected final TokenStore tokenStore;

public AbstractClientApi(final String baseUrl, final TokenStore tokenStore) throws SSLException {
final SslContext sslContext;
final HttpClient httpClient;

sslContext = SslContextBuilder
this.sslContext = SslContextBuilder
.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE)
.build();
httpClient = HttpClient.create().secure(t -> t.sslContext(sslContext));
this.httpClient = HttpClient.create().secure(t -> t.sslContext(this.sslContext));

this.webClient = WebClient
.builder()
.baseUrl(baseUrl)
.clientConnector(new ReactorClientHttpConnector(httpClient))
.clientConnector(new ReactorClientHttpConnector(this.httpClient))
.build();
this.tokenStore = tokenStore;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.socket.WebSocketMessage;
import org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient;
import org.springframework.web.reactive.socket.client.WebSocketClient;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;

import javax.net.ssl.SSLException;
import java.io.IOException;
import java.net.URI;
import java.util.Map;

@Component
public class ClientApi extends AbstractClientApi {
private final static Logger log = LoggerFactory.getLogger(ClientApi.class);
private final String hostname;
private final short port;

public final ClientDeviceApi device;
public final ClientDeviceSetApi deviceSet;
Expand Down Expand Up @@ -43,6 +52,8 @@ public ClientApi(
final ClientUserApi user
) throws SSLException {
super(String.format("https://%s:%d/v1/", hostname, port), tokenStore);
this.hostname = hostname;
this.port = port;
this.device = device;
this.deviceSet = deviceSet;
this.gateway = gateway;
Expand Down Expand Up @@ -77,4 +88,32 @@ public Mono<Map> dump() {
.bodyToMono(Map.class);
}

public void websocket() {
final String token;
final String authorizationHeader;
final HttpClient httpClient;
final WebSocketClient client;



try {
token = this.tokenStore.getAccessToken();
authorizationHeader = String.format("Bearer %s", token);
httpClient = this.httpClient
.headers(headers -> headers.add(HttpHeaders.AUTHORIZATION, authorizationHeader))
.keepAlive(true);
client = new ReactorNettyWebSocketClient(httpClient);
client.execute(URI.create(String.format("https://%s:%d/v1/", hostname, port)), session ->
session.receive()
.map(WebSocketMessage::getPayloadAsText)
.doOnNext(message -> log.debug("Received websocket message: {}", message))
.repeat()
.then()
).block();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}


This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package de.dvdgeisler.iot.dirigera.client.examples.websockets;

import de.dvdgeisler.iot.dirigera.client.api.DirigeraApi;
import de.dvdgeisler.iot.dirigera.client.api.http.ClientApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import reactor.core.publisher.Flux;

/**
* Lists all known devices
*/
@SpringBootApplication
@ComponentScan(basePackageClasses = {DirigeraApi.class})
public class WebSockets {
private final static Logger log = LoggerFactory.getLogger(WebSockets.class);

@Bean
public CommandLineRunner run(final DirigeraApi api, final ClientApi capi) {

return (String... args) -> {
api.pairIfRequired().block(); // pair gateway if required

capi.websocket();
};
}

public static void main(String[] args) {
SpringApplication.run(WebSockets.class, args).close();
}


}

0 comments on commit d8dd076

Please sign in to comment.