There is some way to refresh a primeface component from a class annotated as websocket ServerEndpoint

1

I am developing an application where from my android app it triggers an event about my JSF web application, I have made it through the use of websockets.

My problem is showing the data received by the websocket in my xhtml view when the method annotated with @onMessage is executed.

Then my endpoint class

 @ServerEndpoint(value = "/despacho")
 public class DespachoNotificationEndPoint implements Serializable{
    public static List<String> listaValores = new ArrayList<>();

    @OnMessage
    public void messageRecive(Session s, String message) {
    getListaValores().add(message);
    System.out.println(message);
    }

    @OnOpen
    public void onOpen(Session s) {
    /*código cuando se abre la conexión websocket*/
    }

    @OnClose
    public void onClose(Session s) {
     /*método cuando se cierra la conexión websocket*/
    }

    public static void resetResource() {
        getListaValores().clear();
    }

    public static List<String> getListaValores() {
        return listaValores;
    }

    public static void setListaValores(List<String> listaValores) {
        DespachoNotificationEndPoint.listaValores = listaValores;
    }
}
    
asked by Axel Latorre Villalobos 28.03.2017 в 22:34
source

1 answer

0

Yes, you have to add a socket on your page that listens on a channel that you are going to post:

*. xhtml

<p:socket onMessage="handleMessage" channel="/refreshComp" />

<p:remoteCommand name="updateComp"  update=":component" />


<script type="text/javascript">
     function handleMessage(data) {
             updateComp();
     }
</script>

Bean

private final EventBus eventBus = EventBusFactory.getDefault().eventBus();

@OnMessage
public void messageRecive(Session s, String message) {
     getListaValores().add(message);
     eventBus.publish("/refreshComp", "");
}

In case of multiple concurrent users, the ideal is to create an univocal channel for each client, to avoid updating all clients.

Greetings

    
answered by 29.03.2017 в 16:22