My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

duminică, 27 martie 2016

JSF 2.3 - Conditionally open/close a websocket channel

Read also:

WebSocket integration by Arjan Tijms


In this post, you will see how to conditionally open/close a channel. In order to accomplish this, we place the condition on the connected attribute via a ValueExpression. This attribute (condition) will be evaluated at each AJAX request, and if the value of this EL expression becomes false during an AJAX request then the push connection will explicitly be closed during oncomplete of that AJAX request.

In the JSF 2.3 - The WebSocket Quickstart under Payara post, we are pushing a message that indicates the current server time in format, hh:mm:ss. Let's re-write that example for limiting the pushing service only between 8-9 AM (PM).

The relevant part of the JSF page will look like this (notice the connected attribute at work):

<h:form>               
 <h:commandButton value="Clock" action="#{pushBean.clockAction()}">
  <f:ajax render="@form"/>
 </h:commandButton>
 Service status:
 <h:outputText value="#{pushBean.info}"/>
</h:form>

<f:websocket channel="clock"
             connected="#{pushBean.connected}"
             onopen="websocketOpenListener"
             onclose="websocketCloseListener"
             onmessage="socketListener" />     

On server-side we implement the condition logic as follows:

@Named
@ApplicationScoped
public class PushBean implements Serializable {

 private static final Logger LOG = Logger.getLogger(PushBean.class.getName());

 @Inject
 @Push(channel = "clock")
 private PushContext push;

 private boolean connected;
 private String info;
 private String time;

 private void pingClock() {
  Calendar now = Calendar.getInstance();

  int hour = now.get(Calendar.HOUR_OF_DAY);
  connected = (hour >= 8) && (hour < 9);

  if (connected) {
      time = now.get(Calendar.HOUR_OF_DAY) + ":"
              + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND);
      info = "Service is available";
  } else {
      time = null;
      info = "Service is available only between 8-9 AM/PM";
  }
 }

 public void clockAction() {

  pingClock();

  if (time != null) {
      LOG.log(Level.INFO, "Time: {0}", time);
      push.send(time);
  }
 }

 public boolean isConnected() {
  return connected;
 }

 public void setConnected(boolean connected) {
  this.connected = connected;
 }

 public String getInfo() {
  return info;
 }
}

So, the connected attribute will be set initially to false (default it is true meaning auto-connect). Furthermore, the attribute is evaluated to true only between the 8:00:00 - 8:59:59 server-time. This means that the first AJAX request that take place after 8:00:00 and before 8:59:59 will open the connection, while the first AJAX request that take place after 8:59:59 will close the connection.

The figures below shows a flow of usage:

-request fired before 8:00:00


-request fired after 8:00:00 opens the connection


- between 8:00:00 and 8:59:59 the connection is open

-after 8:59:59, the first request close the connection


The complete application is available here.

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors