Read also:
JSF
2.3 - Explicitly open/close a websocket channel
JSF 2.3 - Firing one-time push when the web socket channel has been opened
JSF 2.3 - Multiple File Upload with HTML 5, AJAX and upload progress bar via web sockets
JSF 2.3 - Firing one-time push when the web socket channel has been opened
JSF 2.3 - Multiple File Upload with HTML 5, AJAX and upload progress bar via web sockets
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