Top Description Fields Constructors Methods
org.glassfish.grizzly.websockets

public abstract Class WebSocketApplication

extends WebSocketAdapter
Class Inheritance
Imports
java.util.ArrayList, .List, .Set, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap, org.glassfish.grizzly.http.HttpRequestPacket, org.glassfish.grizzly.http.util.Header

Abstract server-side WebSocket application, which will handle application WebSockets events.
Author
Alexey Stashok

Field Summary

Modifier and TypeField and Description
private final ConcurrentMap<WebSocket, Boolean>
private final List<Extension>
private final List<String>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected boolean

Returns:

true if the socket was successfully associated, otherwise returns false.
add
(WebSocket
the WebSocket to associate with this application.
socket
)

Associates the specified WebSocket with this application.

public WebSocket

Returns:

a new WebSocket instance.
createSocket
(ProtocolHandler
the ProtocolHandler to use with the newly created WebSocket.
handler
,
WebSocketListener...
the WebSocketListeners to associate with the new WebSocket.
listeners
)

Factory method to create new WebSocket instances.

public WebSocket
createSocket(final ProtocolHandler
the ProtocolHandler to use with the newly created WebSocket.
handler
,
final HttpRequestPacket
the HttpRequestPacket that triggered the creation of the WebSocket connection.
requestPacket
,
final WebSocketListener...
the WebSocketListeners to associate with the new WebSocket.
listeners
)

Factory method to create new WebSocket instances.

public List<Extension>

Returns:

the websocket extensions supported by this WebSocketApplication.
getSupportedExtensions
()

Return the websocket extensions supported by this WebSocketApplication.

public List<String>
protected Set<WebSocket>

Returns:

a set of WebSockets, registered with the application.
getWebSockets
()

Returns a set of WebSockets, registered with the application.

protected void
handshake(HandShake handshake)

This method will be called, when initial WebSocket handshake process has been completed, but allows the application to perform further negotiation/validation.

public boolean

Returns:

true if this application can service this request

isApplicationRequest
(HttpRequestPacket
the incoming HTTP request.
request
)
Deprecated URI mapping shouldn't be intrinsic to the application.

Checks application specific criteria to determine if this application can process the request as a WebSocket connection.

public void
onClose(WebSocket
the WebSocket being closed.
socket
,
DataFrame
the closing frame.
frame
)

Overrides org.glassfish.grizzly.websockets.WebSocketAdapter.onClose.

Implements org.glassfish.grizzly.websockets.WebSocketListener.onClose.

When a WebSocket#onClose(DataFrame) is invoked, the WebSocket will be unassociated with this application and closed.

public void
onConnect(WebSocket
the new WebSocket connection.
socket
)

Overrides org.glassfish.grizzly.websockets.WebSocketAdapter.onConnect.

Implements org.glassfish.grizzly.websockets.WebSocketListener.onConnect.

When a new WebSocket connection is made to this application, the WebSocket will be associated with this application.

protected boolean

Returns:

true if the WebSocket should be closed otherwise false.
onError
(final WebSocket
the websocket being processed at the time the exception occurred.
webSocket
,
final Throwable
the unexpected exception.
t
)

This method will be invoked if an unexpected exception is caught by the WebSocket runtime.

public void
onExtensionNegotiation(List<Extension>
the intersection of extensions between client and application.
extensions
)

Invoked during the handshake if the client has advertised extensions it may use and one or more extensions intersect with those returned by getSupportedExtensions().

public boolean

Returns:

true if the socket was successfully unassociated, otherwise returns false.
remove
(WebSocket
the WebSocket to unassociate with this application.
socket
)

Unassociates the specified WebSocket with this application.

public final boolean

Returns:

true if the request should be upgraded to a WebSocket connection
upgrade
(HttpRequestPacket request)

Checks protocol specific information can and should be upgraded.

Inherited from org.glassfish.grizzly.websockets.WebSocketAdapter:
onFragmentonFragmentonMessageonMessageonPingonPong

Field Detail

socketsback to summary
private final ConcurrentMap<WebSocket, Boolean> sockets
supportedExtensionsback to summary
private final List<Extension> supportedExtensions
supportedProtocolsback to summary
private final List<String> supportedProtocols

Constructor Detail

WebSocketApplicationback to summary
public WebSocketApplication()

Method Detail

addback to summary
protected boolean add(WebSocket socket)

Associates the specified WebSocket with this application.

Parameters
socket:WebSocket

the WebSocket to associate with this application.

Returns:boolean

true if the socket was successfully associated, otherwise returns false.

createSocketback to summary
public WebSocket createSocket(ProtocolHandler handler, WebSocketListener... listeners)

Factory method to create new WebSocket instances. Developers may wish to override this to return customized WebSocket implementations.

Parameters
handler:ProtocolHandler

the ProtocolHandler to use with the newly created WebSocket.

listeners:WebSocketListener[]

the WebSocketListeners to associate with the new WebSocket.

Returns:WebSocket

a new WebSocket instance.

Annotations
@Deprecated
createSocketback to summary
public WebSocket createSocket(final ProtocolHandler handler, final HttpRequestPacket requestPacket, final WebSocketListener... listeners)

Factory method to create new WebSocket instances. Developers may wish to override this to return customized WebSocket implementations.

Parameters
handler:ProtocolHandler

the ProtocolHandler to use with the newly created WebSocket.

requestPacket:HttpRequestPacket

the HttpRequestPacket that triggered the creation of the WebSocket connection.

listeners:WebSocketListener[]

the WebSocketListeners to associate with the new WebSocket.

getSupportedExtensionsback to summary
public List<Extension> getSupportedExtensions()

Return the websocket extensions supported by this WebSocketApplication. The Extensions added to this List should not include any Extension.Parameters as they will be ignored. This is used exclusively for matching the requested extensions.

Returns:List<Extension>

the websocket extensions supported by this WebSocketApplication.

getSupportedProtocolsback to summary
public List<String> getSupportedProtocols(List<String> subProtocol)
getWebSocketsback to summary
protected Set<WebSocket> getWebSockets()

Returns a set of WebSockets, registered with the application. The returned set is unmodifiable, the possible modifications may cause exceptions.

Returns:Set<WebSocket>

a set of WebSockets, registered with the application.

handshakeback to summary
protected void handshake(HandShake handshake) throws HandshakeException

This method will be called, when initial WebSocket handshake process has been completed, but allows the application to perform further negotiation/validation.

Exceptions
HandshakeException:
error occurred during the handshake.
isApplicationRequestback to summary
public boolean isApplicationRequest(HttpRequestPacket request)

Deprecated

URI mapping shouldn't be intrinsic to the application. WebSocketApplications should be registered using WebSocketEngine#register(String, String, WebSocketApplication) using standard Servlet url-pattern rules.

Checks application specific criteria to determine if this application can process the request as a WebSocket connection.

Parameters
request:HttpRequestPacket

the incoming HTTP request.

Returns:boolean

true if this application can service this request

Annotations
@Deprecated
onCloseback to summary
public void onClose(WebSocket socket, DataFrame frame)

Overrides org.glassfish.grizzly.websockets.WebSocketAdapter.onClose.

Implements org.glassfish.grizzly.websockets.WebSocketListener.onClose.

When a WebSocket#onClose(DataFrame) is invoked, the WebSocket will be unassociated with this application and closed. If this method is overridden, the overriding method must call remove(WebSocket). This is necessary to ensure WebSocket instances are not leaked nor are message operations against closed sockets are performed.

Parameters
socket:WebSocket

the WebSocket being closed.

frame:DataFrame

the closing frame.

Annotations
@Override
onConnectback to summary
public void onConnect(WebSocket socket)

Overrides org.glassfish.grizzly.websockets.WebSocketAdapter.onConnect.

Implements org.glassfish.grizzly.websockets.WebSocketListener.onConnect.

When a new WebSocket connection is made to this application, the WebSocket will be associated with this application. If this method is overridden, the overriding method must call add(WebSocket). This is necessary to ensure bulk message sending via facilities such as Broadcaster function properly.

Parameters
socket:WebSocket

the new WebSocket connection.

Annotations
@Override
onErrorback to summary
protected boolean onError(final WebSocket webSocket, final Throwable t)

This method will be invoked if an unexpected exception is caught by the WebSocket runtime.

Parameters
webSocket:WebSocket

the websocket being processed at the time the exception occurred.

t:Throwable

the unexpected exception.

Returns:boolean

true if the WebSocket should be closed otherwise false.

onExtensionNegotiationback to summary
public void onExtensionNegotiation(List<Extension> extensions)

Invoked during the handshake if the client has advertised extensions it may use and one or more extensions intersect with those returned by getSupportedExtensions(). The Extensions passed to this method will include any extension parameters included by the client. It's up to this method to re-order and or adjust any parameter values within the list. This method must not add any extensions that weren't originally in the list, but it is acceptable to remove one or all extensions if for some reason they can't be supported. If not overridden, the List will be sent as-is back to the client.

Parameters
extensions:List<Extension>

the intersection of extensions between client and application.

Since
2.3
removeback to summary
public boolean remove(WebSocket socket)

Unassociates the specified WebSocket with this application.

Parameters
socket:WebSocket

the WebSocket to unassociate with this application.

Returns:boolean

true if the socket was successfully unassociated, otherwise returns false.

upgradeback to summary
public final boolean upgrade(HttpRequestPacket request)

Checks protocol specific information can and should be upgraded. The default implementation will check for the presence of the Upgrade header with a value of WebSocket. If present, isApplicationRequest(org.glassfish.grizzly.http.HttpRequestPacket) will be invoked to determine if the request is a valid websocket request.

Returns:boolean

true if the request should be upgraded to a WebSocket connection