Class UIWebsocket
- java.lang.Object
-
- jakarta.faces.component.UIComponent
-
- jakarta.faces.component.UIComponentBase
-
- jakarta.faces.component.UIWebsocket
-
- All Implemented Interfaces:
ClientBehaviorHolder
,PartialStateHolder
,StateHolder
,TransientStateHolder
,ComponentSystemEventListener
,FacesListener
,SystemEventListenerHolder
,EventListener
public class UIWebsocket extends UIComponentBase implements ClientBehaviorHolder
The
<f:websocket>
tag opens an one-way (server to client) websocket based push connection in client side which can be reached from server side viaPushContext
interface injected in any CDI/container managed artifact via@
Push
annotation.By default, the
rendererType
property must be set to "jakarta.faces.Websocket
". This value can be changed by calling thesetRendererType()
method.For detailed usage instructions, see
@
Push
javadoc.- Since:
- 2.3
- See Also:
Push
-
-
Field Summary
Fields Modifier and Type Field Description static String
COMPONENT_FAMILY
The standard component family for this component.static String
COMPONENT_TYPE
The standard component type for this component.-
Fields inherited from class jakarta.faces.component.UIComponent
ATTRS_WITH_DECLARED_DEFAULT_VALUES, BEANINFO_KEY, bindings, COMPOSITE_COMPONENT_TYPE_KEY, COMPOSITE_FACET_NAME, FACETS_KEY, VIEW_LOCATION_KEY
-
-
Constructor Summary
Constructors Constructor Description UIWebsocket()
Create a newUIWebsocket
instance with default property values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
getChannel()
Returns the name of the websocket channel.Collection<String>
getEventNames()
Returns a non-null, empty, unmodifiableCollection
which returnstrue
on anyCollection#contains()
invocation, indicating that all client behavior event names are acceptable.String
getFamily()
ReturnsCOMPONENT_FAMILY
.String
getOnclose()
Returns the JavaScript event handler function that is invoked when the websocket is closed and will not anymore attempt to reconnect.String
getOnerror()
Returns the JavaScript event handler function that is invoked when a connection error has occurred and the websocket will attempt to reconnect.String
getOnmessage()
Returns the JavaScript event handler function that is invoked when a push message is received from the server.String
getOnopen()
Returns the JavaScript event handler function that is invoked when the websocket is opened.String
getScope()
Returns the scope of the websocket channel.Serializable
getUser()
Returns the user identifier of the websocket channel.boolean
isConnected()
Returns whether to (auto)connect the websocket or not.void
setChannel(String channel)
Sets the name of the websocket channel.void
setConnected(boolean connected)
Sets whether to (auto)connect the websocket or not.void
setOnclose(String onclose)
Sets the JavaScript event handler function that is invoked when the websocket is closed and will not anymore attempt to reconnect.void
setOnerror(String onerror)
Sets the JavaScript event handler function that is invoked when a connection error has occurred and the websocket will attempt to reconnect.void
setOnmessage(String onmessage)
Sets the JavaScript event handler function that is invoked when a push message is received from the server.void
setOnopen(String onopen)
Sets the JavaScript event handler function that is invoked when the websocket is opened.void
setScope(String scope)
Sets the scope of the websocket channel.void
setUser(Serializable user)
Sets the user identifier of the websocket channel, so that user-targeted push messages can be sent.void
setValueExpression(String name, ValueExpression binding)
Set theValueExpression
used to calculate the value for the specified attribute or property name, if any.-
Methods inherited from class jakarta.faces.component.UIComponentBase
addClientBehavior, addFacesListener, broadcast, clearInitialState, decode, encodeBegin, encodeChildren, encodeEnd, findComponent, getAttributes, getChildCount, getChildren, getClientBehaviors, getClientId, getDefaultEventName, getFacesContext, getFacesListeners, getFacet, getFacetCount, getFacets, getFacetsAndChildren, getId, getListenersForEventClass, getParent, getPassThroughAttributes, getRenderer, getRendererType, getRendersChildren, invokeOnComponent, isRendered, isTransient, markInitialState, processDecodes, processRestoreState, processSaveState, processUpdates, processValidators, queueEvent, removeFacesListener, restoreAttachedState, restoreState, saveAttachedState, saveState, setId, setParent, setRendered, setRendererType, setTransient, subscribeToEvent, unsubscribeFromEvent
-
Methods inherited from class jakarta.faces.component.UIComponent
encodeAll, getClientId, getCompositeComponentParent, getContainerClientId, getCurrentComponent, getCurrentCompositeComponent, getNamingContainer, getPassThroughAttributes, getResourceBundleMap, getStateHelper, getStateHelper, getTransientStateHelper, getTransientStateHelper, getValueExpression, initialStateMarked, isCompositeComponent, isInView, isVisitable, popComponentFromEL, processEvent, pushComponentToEL, restoreTransientState, saveTransientState, setInView, visitTree
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jakarta.faces.component.behavior.ClientBehaviorHolder
addClientBehavior, getClientBehaviors, getDefaultEventName
-
-
-
-
Field Detail
-
COMPONENT_TYPE
public static final String COMPONENT_TYPE
The standard component type for this component.
- See Also:
- Constant Field Values
-
COMPONENT_FAMILY
public static final String COMPONENT_FAMILY
The standard component family for this component.
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
UIWebsocket
public UIWebsocket()
Create a new
UIWebsocket
instance with default property values.- Throws:
IllegalStateException
- When Websocket endpoint is not enabled.
-
-
Method Detail
-
getFamily
public String getFamily()
Returns
COMPONENT_FAMILY
.- Specified by:
getFamily
in classUIComponent
- Returns:
- the component family (not null).
-
setValueExpression
public void setValueExpression(String name, ValueExpression binding)
Set the
ValueExpression
used to calculate the value for the specified attribute or property name, if any. If aValueExpression
is set for thechannel
orscope
property, regardless of the value, throw an illegal argument exception. If aValueExpression
is set for theuser
property, and the non-null value is not an instance ofSerializable
, throw an illegal argument exception.- Overrides:
setValueExpression
in classUIComponent
- Parameters:
name
- Name of the attribute or property for which to set aValueExpression
binding
- TheValueExpression
to set, ornull
to remove any currently setValueExpression
- Throws:
IllegalArgumentException
- Ifname
is one ofid
,parent
,channel
orscope
, or itname
isuser
and the non-null value is not an instance ofSerializable
.NullPointerException
- Ifname
isnull
.
-
getEventNames
public Collection<String> getEventNames()
Returns a non-null, empty, unmodifiable
Collection
which returnstrue
on anyCollection#contains()
invocation, indicating that all client behavior event names are acceptable.- Specified by:
getEventNames
in interfaceClientBehaviorHolder
- Overrides:
getEventNames
in classUIComponentBase
- Returns:
- the collection of event names.
-
getChannel
public String getChannel()
Returns the name of the websocket channel.- Returns:
- The name of the websocket channel.
-
setChannel
public void setChannel(String channel)
Sets the name of the websocket channel. It may not be an Jakarta Expression Language expression and it may only contain alphanumeric characters, hyphens, underscores and periods. All open websockets on the same channel will receive the same push message from the server.- Parameters:
channel
- The name of the websocket channel.- Throws:
IllegalArgumentException
- When the value does not represent a valid channel name.
-
getScope
public String getScope()
Returns the scope of the websocket channel.- Returns:
- The scope of the websocket channel.
-
setScope
public void setScope(String scope)
Sets the scope of the websocket channel. It may not be an Jakarta Expression Language expression and allowed values areapplication
,session
andview
, case insensitive. When the value isapplication
, then all channels with the same name throughout the application will receive the same push message. When the value issession
, then only the channels with the same name in the current user session will receive the same push message. When the value isview
, then only the channel in the current view will receive the push message. The default scope isapplication
. When theuser
attribute is specified, then the default scope issession
.- Parameters:
scope
- The scope of the websocket channel.
-
getUser
public Serializable getUser()
Returns the user identifier of the websocket channel.- Returns:
- The user identifier of the websocket channel.
-
setUser
public void setUser(Serializable user)
Sets the user identifier of the websocket channel, so that user-targeted push messages can be sent. All open websockets on the same channel and user will receive the same push message from the server. It must implementSerializable
and preferably have low memory footprint. Suggestion: use#{request.remoteUser}
or#{someLoggedInUser.id}
.- Parameters:
user
- The user identifier of the websocket channel.
-
getOnopen
public String getOnopen()
Returns the JavaScript event handler function that is invoked when the websocket is opened.- Returns:
- The JavaScript event handler function that is invoked when the websocket is opened.
-
setOnopen
public void setOnopen(String onopen)
Sets the JavaScript event handler function that is invoked when the websocket is opened. The function will be invoked with one argument: the channel name.- Parameters:
onopen
- The JavaScript event handler function that is invoked when the websocket is opened.
-
getOnmessage
public String getOnmessage()
Returns the JavaScript event handler function that is invoked when a push message is received from the server.- Returns:
- The JavaScript event handler function that is invoked when a push message is received from the server.
-
setOnmessage
public void setOnmessage(String onmessage)
Sets the JavaScript event handler function that is invoked when a push message is received from the server. The function will be invoked with three arguments: the push message, the channel name and the raw MessageEvent itself.- Parameters:
onmessage
- The JavaScript event handler function that is invoked when a push message is received from the server.
-
getOnerror
public String getOnerror()
Returns the JavaScript event handler function that is invoked when a connection error has occurred and the websocket will attempt to reconnect.- Returns:
- The JavaScript event handler function that is invoked when a connection error has occurred and the websocket will attempt to reconnect.
- Since:
- 4.0
-
setOnerror
public void setOnerror(String onerror)
Sets the JavaScript event handler function that is invoked when a connection error has occurred and the websocket will attempt to reconnect. The function will be invoked with three arguments: the error reason code, the channel name and the rawCloseEvent
itself. Note that this will not be invoked on final close of the websocket, even when the final close is caused by an error. See also RFC 6455 section 7.4.1 andCloseReason.CloseCodes
API for an elaborate list of all close codes.- Parameters:
onerror
- The JavaScript event handler function that is invoked when a reconnection error has occurred.- Since:
- 4.0
-
getOnclose
public String getOnclose()
Returns the JavaScript event handler function that is invoked when the websocket is closed and will not anymore attempt to reconnect.- Returns:
- The JavaScript event handler function that is invoked when the websocket is closed and will not anymore attempt to reconnect.
-
setOnclose
public void setOnclose(String onclose)
Sets the JavaScript event handler function that is invoked when the websocket is closed and will not anymore attempt to reconnect. The function will be invoked with three arguments: the close reason code, the channel name and the rawCloseEvent
itself. Note that this will also be invoked when the close is caused by an error and that you can inspect the close reason code if an actual connection error occurred and which one (i.e. when the code is not 1000 or 1008). See also RFC 6455 section 7.4.1 andCloseReason.CloseCodes
API for an elaborate list of all close codes.- Parameters:
onclose
- The JavaScript event handler function that is invoked when the websocket is closed.
-
isConnected
public boolean isConnected()
Returns whether to (auto)connect the websocket or not.- Returns:
- Whether to (auto)connect the websocket or not.
-
setConnected
public void setConnected(boolean connected)
Sets whether to (auto)connect the websocket or not. Defaults totrue
. It's interpreted as a JavaScript instruction whether to open or close the websocket push connection. Note that this attribute is re-evaluated on every ajax request. You can also explicitly set it tofalse
and then manually control in JavaScript byfaces.push.open("channelName")
andfaces.push.close("channelName")
.- Parameters:
connected
- Whether to (auto)connect the websocket or not.
-
-