简体   繁体   English

如何在Vaadin中动态推送更新页面?

[英]How to dynamically push-update pages in vaadin?

I have a working application with Vaadin and Spring (+Security) . 我有一个与VaadinSpring (+Security)一起工作的应用程序。 I'm now trying to dynamically push updates to the client interfaces using vaadin-push . 我现在正尝试使用vaadin-push将更新动态推送到客户端接口。

I want to create a "view" that is the same for all clients. 我想创建一个对所有客户端都相同的“视图”。 The view should get a counter incremented dynamically by push. 该视图应获得通过推送动态递增的计数器。 So, while the clients show the page, the counter updates itself (or in my example: appends multiple label components to the page). 因此,当客户端显示页面时,计数器会自动更新(或在我的示例中:将多个标签组件添加到页面)。 Every client should see the same value. 每个客户应该看到相同的价值。

But I don't see the changes pushed to the webpage. 但我看不到更改已推送到网页。 Why? 为什么?

//the static controller the dynamically changes content
@Controller
public class ViewPresenter {
    private static int counter = 0;

    @Autowired
    private void StaticView view;

    @Scheduled(fixedRate = 1000)
    public void refresh() {
        //push the content change periodically to frontend
        view.getUI().access(new Runnable() {
            @Override
            public void run() {
                view.addComponent(new Label("refresh: " + counter++);
                Sysout("update executed: " + counter); //OK, is printed
            }
        });
    }
}

//the static test component that gets a new label added on every content change
@VaadinComponent
public class StaticView extends CssLayout {
        //this is the view that every client should see
}

//the secured admin view
@VaadinView(name = "/secured")
@Scope("ui")
@Secured("user")
public class SecuredView extends VerticalLayout implements View {
    @Autowired
    private StaticView staticView;

    @Override
    public void enter(ViewChangeEvent event) {
        addComponent(staticView);
    }
}

//enable push
@Configuration
@Push
public class AppConfig {
}

@VaadinUI
@PreserveOnRefresh
public class ApplicationUI extends UI {

} 

I can see the changes on the client side only if I manually refresh the webpage. 仅当我手动刷新网页时,才能在客户端看到更改。 But the content itself does not change automatically. 但是内容本身不会自动更改。

Maybe @Push has to be placed on the UI class? 也许@Push必须放在UI类上? But in this case I'd get the following error: 但是在这种情况下,我会得到以下错误:

java.lang.IllegalStateException: Cannot suspend a response longer than the session timeout. java.lang.IllegalStateException:无法暂停响应的时间超过会话超时。 Increase the value of session-timeout in web.xml at org.atmosphere.cpr.AtmosphereResourceImpl.suspend(AtmosphereResourceImpl.java:314) at org.atmosphere.cpr.AtmosphereResourceImpl.suspend(AtmosphereResourceImpl.java:292) at com.vaadin.server.communication.PushHandler$2.run(PushHandler.java:129) at com.vaadin.server.communication.PushHandler.callWithUi(PushHandler.java:242) at com.vaadin.server.communication.PushHandler.access$200(PushHandler.java:55) at com.vaadin.server.communication.PushHandler$1.onRequest(PushHandler.java:73) 在com.vaadin的org.atmosphere.cpr.AtmosphereResourceImpl.suspend(AtmosphereResourceImpl.java:292)的org.atmosphere.cpr.AtmosphereResourceImpl.suspend(AtmosphereResourceImpl.java:314)处的web.xml中增加会话超时值。在com.vaadin.server.communication.PushHandler.callWithUi(PushHandler.java:242)处的server.communication.PushHandler $ 2.run(PushHandler.java:129)在com.vaadin.server.communication.PushHandler.access $ 200(PushHandler。 java:55)com.vaadin.server.communication.PushHandler $ 1.onRequest(PushHandler.java:73)

Solution based on https://github.com/peholmst/vaadin4spring/issues/51#issuecomment-46875255 : 基于https://github.com/peholmst/vaadin4spring/issues/51#issuecomment-46875255的解决方案:

server.sessionTimeout=30 with @Push(transport = Transport.LONG_POLLING) @Push(transport = Transport.LONG_POLLING) server.sessionTimeout=30 @Push(transport = Transport.LONG_POLLING)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM