gpt4 book ai didi

java - Vaadin 7.1.0 推送

转载 作者:行者123 更新时间:2023-11-30 06:23:55 25 4
gpt4 key购买 nike

我是 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?

最佳答案

关于java - Vaadin 7.1.0 推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17676645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com