我是 Vaadin 的新手,只是实现了一个原型(prototype)来测试功能。目前我正在尝试 Vaadin 服务器推送功能。 我喜欢做一个简单的计数器,可以通过按钮增加。之后,我喜欢从不同的浏览器打开这一面,当我计数时,我喜欢更新所有浏览器。
现在我已经知道当前浏览器是由一个单独的线程更新的。
public class PushTestView extends VerticalLayout implements View {
private static final long serialVersionUID = -8056849522760207972L;
private Label counter;
public void enter(ViewChangeEvent event) {
removeAllComponents();
PushTestController controller = PushTestController.getInstance();
counter = new Label("" + controller.getCounter());
Button button = new Button("Count up");
button.addClickListener(new ClickListener() {
private static final long serialVersionUID = -1437136914245802675L;
@Override
public void buttonClick(ClickEvent event) {
InitializerThread thread = new InitializerThread();
thread.run();
}
});
addComponent(counter);
addComponent(button);
}
class InitializerThread extends Thread {
@Override
public void run() {
// Init done, update the UI after doing locking
UI.getCurrent().access(new Runnable() {
@Override
public void run() {
// Here the UI is locked and can be updated
PushTestController.getInstance().countUp();
counter.setValue("" + PushTestController.getInstance().getCounter());
UI.getCurrent().notifyAll();
}
});
}
}
}
我将此 View 嵌入到我的 UI 中,并用 @Push
进行注释。
这是我的 Controller :
public class PushTestController {
private static PushTestController instance = new PushTestController();
private int counter = 0;
private PushTestController() {
//Private Constructor for Singleton
}
public static PushTestController getInstance() {
return instance;
}
public int getCounter() {
return counter;
}
public String getCounterString() {
return "" + counter;
}
public void countUp() {
counter++;
}
}
我如何扩展此示例以使其能够在不同浏览器中更新 UI?
最佳答案
看看这个,第 11.16.3 节:https://vaadin.com/book/vaadin7/-/page/advanced.push.html
关于java - Vaadin 7.1.0 推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17676645/