Complete push example
This commit is contained in:
parent
fcccce33e8
commit
0e34a27d24
@ -1,6 +1,5 @@
|
|||||||
package com.baeldung.introduction;
|
package com.baeldung.introduction;
|
||||||
|
|
||||||
import com.vaadin.flow.component.html.H1;
|
|
||||||
import com.vaadin.flow.component.html.Span;
|
import com.vaadin.flow.component.html.Span;
|
||||||
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
|
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
|
||||||
import com.vaadin.flow.router.Route;
|
import com.vaadin.flow.router.Route;
|
||||||
@ -16,12 +15,18 @@ public class PushView extends VerticalLayout {
|
|||||||
public PushView() {
|
public PushView() {
|
||||||
var output = new Span();
|
var output = new Span();
|
||||||
|
|
||||||
add(new H1("Server Push"), output);
|
// Publish server time once a second
|
||||||
|
var serverTime = Flux.interval(Duration.ofSeconds(1))
|
||||||
|
.map(o -> "Server time: " + Instant.now());
|
||||||
|
|
||||||
Flux.interval(Duration.ofSeconds(1))
|
|
||||||
.map(o -> "Server time: " + Instant.now())
|
serverTime.subscribe(time ->
|
||||||
.subscribe(time -> getUI().ifPresent(ui -> ui.access(() -> {
|
// ui.access is required to update the UI from a background thread
|
||||||
output.setText(time);
|
getUI().ifPresent(ui ->
|
||||||
})));
|
ui.access(() -> output.setText(time))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
add(output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user