Update p:growl from @ApplicationScoped bean listening on p:socket

夙愿已清 提交于 2019-12-12 06:17:19

问题


I have application scoped bean that is listening to web-socket. When message is received I would like to update growl. But something like below doesn't work because it is not in request / response time. It is possible to do this?

RequestContext.getCurrentInstance().update("growl");
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", "value"));

回答1:


What you need to do is send a PrimeFaces push event from the bean to all attached clients on a general websocket shared by all clients. In that event you send a message like in the PrimeFaces push notify example

 public void send() {
    EventBus eventBus = EventBusFactory.getDefault().eventBus();
    eventBus.publish(CHANNEL, new FacesMessage(StringEscapeUtils.escapeHtml(summary), StringEscapeUtils.escapeHtml(detail)));
}

And then in the page show that message

<p:growl widgetVar="growl" showDetail="true" />
<p:socket onMessage="handleMessage" channel="/notify" />

<script type="text/javascript">
    function handleMessage(facesmessage) {
        facesmessage.severity = 'info';

        PF('growl').show([facesmessage]);
    }
</script>


来源:https://stackoverflow.com/questions/29491403/update-pgrowl-from-applicationscoped-bean-listening-on-psocket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!