How does Primfaces Push <p: socket> work?

I'm trying to work with the PF Push Counter example and I'm not sure. I have two portlets. One of them is the counter portlet. Another counter-visor portlet. How can I subscribe (register) to the "channel"?

My code is pretty much straight out of the PF Showcase example.

1) Counter (publisher) Counter.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<h:body>


    <h:form id="form">
        <h:outputText id="out" value="#{counterBean.count}"
            styleClass="ui-widget display" />

        <p:commandButton value="Click"
            actionListener="#{counterBean.increment}" />
    </h:form>



</h:body>

</html>

      


CounterBean.java

@ApplicationScoped
@ManagedBean
public class CounterBean {

private volatile int count;

public int getCount() {
    return count;
}

public void setCount(int count) {
    this.count = count;
}

public void increment() {
    count++;
    System.out.println("increment() " + count);
    EventBus eventBus = EventBusFactory.getDefault().eventBus();
    eventBus.publish("/counter", String.valueOf(count));
}

      

2) CounterView (consumer) CounterView.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<h:body>
    <h:form id="form1">
        <h:outputText id="out" value="What my count?" styleClass="display" />

        <p:remoteCommand name="updateWidgets"
            actionListener="#{consumerBean.printMessage()}" update=":form1:out" />
    </h:form>
    <p:socket onMessage="handleMessage" channel="/counter" />

    <script type="text/javascript">
    function handleMessage(data) {
        updateWidgets();
    }
</script>
</h:body>

</html>

      

Using this example

How does Primeface Socket work?

CounterResource.java

@PushEndpoint("/counter")
public class CounterResource {

    @OnMessage(encoders = { JSONEncoder.class })
    public String onMessage(String count) {
        System.out.println("OnMessage " + count );
        return count;
    }
}

      


@SessionScoped
@ManagedBean
public class ConsumerBean {
private int count;
private String message;


public int getCount() {
    return count;
}
public void setCount(int count) {
    this.count = count;
}
public String getMessage() {
    return message;
}
public void setMessage(String message) {
    this.message = message;
}

public void printMessage(){
    System.out.println("Consumer Bean Listener!");

}



}

      

Q: the counter on count.xhtml is incremented, but nothing happens on counterView.html. I put in a few breakpoints but nothing stopped at the Consumer bean.

How can I get this job? This seems to be a simple example, but it might make it work. You can help?

+3


source to share


1 answer


1st: What is your ConsumerBean for? You have a CounterBean application that contains the counter that you want to display with "push". So why do you have another counter in the ConsumerBean?

2nd: in CounterView.xhtml you don't show the counter at all, not even the ConsumerBean counter.



Use CounterBean (and its counter) in the CounterView.xhtml page. In h: outputText instead of "What my count?" fix value uses # {counterBean.count} (same as counter.xhtml).

By the way, check the official demo, it works nicely: http://www.primefaces.org/showcase/push/counter.xhtml

0


source







All Articles