Class UIWebsocket
- All Implemented Interfaces:
ClientBehaviorHolder,PartialStateHolder,StateHolder,TransientStateHolder,ComponentSystemEventListener,FacesListener,SystemEventListenerHolder,EventListener
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 via PushContext 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 the setRendererType() method.
For detailed usage instructions, see @Push javadoc.
- Since:
- 2.3
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe standard component family for this component.static final StringThe 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, CURRENT_COMPONENT, CURRENT_COMPOSITE_COMPONENT, FACETS_KEY, HONOR_CURRENT_COMPONENT_ATTRIBUTES_PARAM_NAME, VIEW_LOCATION_KEY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the name of the websocket channel.Returns a non-null, empty, unmodifiableCollectionwhich returnstrueon anyCollection#contains()invocation, indicating that all client behavior event names are acceptable.ReturnsCOMPONENT_FAMILY.Returns the JavaScript event handler function that is invoked when the websocket is closed.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 the websocket is opened.getScope()Returns the scope of the websocket channel.getUser()Returns the user identifier of the websocket channel.booleanReturns whether to (auto)connect the websocket or not.voidsetChannel(String channel) Sets the name of the websocket channel.voidsetConnected(boolean connected) Sets whether to (auto)connect the websocket or not.voidsetOnclose(String onclose) Sets the JavaScript event handler function that is invoked when the websocket is closed.voidsetOnmessage(String onmessage) Sets the JavaScript event handler function that is invoked when a push message is received from the server.voidSets the JavaScript event handler function that is invoked when the websocket is opened.voidSets the scope of the websocket channel.voidsetUser(Serializable user) Sets the user identifier of the websocket channel, so that user-targeted push messages can be sent.voidsetValueExpression(String name, ValueExpression binding) Set theValueExpressionused 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, getValueBinding, invokeOnComponent, isRendered, isTransient, markInitialState, processDecodes, processRestoreState, processSaveState, processUpdates, processValidators, queueEvent, removeFacesListener, restoreAttachedState, restoreState, saveAttachedState, saveState, setId, setParent, setRendered, setRendererType, setTransient, setValueBinding, subscribeToEvent, unsubscribeFromEventMethods 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, visitTreeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface jakarta.faces.component.behavior.ClientBehaviorHolder
addClientBehavior, getClientBehaviors, getDefaultEventName
-
Field Details
-
COMPONENT_TYPE
The standard component type for this component.
- See Also:
-
COMPONENT_FAMILY
The standard component family for this component.
- See Also:
-
-
Constructor Details
-
UIWebsocket
public UIWebsocket()Create a new
UIWebsocketinstance with default property values.- Throws:
IllegalStateException- When Websocket endpoint is not enabled.
-
-
Method Details
-
getFamily
Returns
COMPONENT_FAMILY.- Specified by:
getFamilyin classUIComponent- Returns:
- the component family (not null).
-
setValueExpression
Set the
ValueExpressionused to calculate the value for the specified attribute or property name, if any. If aValueExpressionis set for thechannelorscopeproperty, regardless of the value, throw an illegal argument exception. If aValueExpressionis set for theuserproperty, and the non-null value is not an instance ofSerializable, throw an illegal argument exception.- Overrides:
setValueExpressionin classUIComponent- Parameters:
name- Name of the attribute or property for which to set aValueExpressionbinding- TheValueExpressionto set, ornullto remove any currently setValueExpression- Throws:
IllegalArgumentException- Ifnameis one ofid,parent,channelorscope, or itnameisuserand the non-null value is not an instance ofSerializable.NullPointerException- Ifnameisnull.
-
getEventNames
Returns a non-null, empty, unmodifiable
Collectionwhich returnstrueon anyCollection#contains()invocation, indicating that all client behavior event names are acceptable.- Specified by:
getEventNamesin interfaceClientBehaviorHolder- Overrides:
getEventNamesin classUIComponentBase- Returns:
- the collection of event names.
-
getChannel
Returns the name of the websocket channel.- Returns:
- The name of the websocket channel.
-
setChannel
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
Returns the scope of the websocket channel.- Returns:
- The scope of the websocket channel.
-
setScope
Sets the scope of the websocket channel. It may not be an Jakarta Expression Language expression and allowed values areapplication,sessionandview, 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 theuserattribute is specified, then the default scope issession.- Parameters:
scope- The scope of the websocket channel.
-
getUser
Returns the user identifier of the websocket channel.- Returns:
- The user identifier of the websocket channel.
-
setUser
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 implementSerializableand preferably have low memory footprint. Suggestion: use#{request.remoteUser}or#{someLoggedInUser.id}.- Parameters:
user- The user identifier of the websocket channel.
-
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
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
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
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.
-
getOnclose
Returns the JavaScript event handler function that is invoked when the websocket is closed.- Returns:
- The JavaScript event handler function that is invoked when the websocket is closed.
-
setOnclose
Sets the JavaScript event handler function that is invoked when the websocket is closed. The function will be invoked with three arguments: the close reason code, the channel name and the rawCloseEventitself. Note that this will also be invoked on errors and that you can inspect the close reason code if an error occurred and which one (i.e. when the code is not 1000). See also RFC 6455 section 7.4.1 andCloseReason.CloseCodesAPI 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 tofalseand then manually control in JavaScript byOmniFaces.Push.open("channelName")andOmniFaces.Push.close("channelName").- Parameters:
connected- Whether to (auto)connect the websocket or not.
-