Top Description Methods
org.springframework.web.socket

public Interface WebSocketSession

extends Closeable
Known Direct Subinterfaces
org.springframework.web.socket.adapter.NativeWebSocketSession, org.springframework.web.socket.sockjs.transport.SockJsSession
Known Direct Implementers
org.springframework.web.socket.handler.WebSocketSessionDecorator, org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession
Imports
java.io.Closeable, .IOException, java.net.InetSocketAddress, .URI, java.security.Principal, java.util.List, .Map, org.springframework.http.HttpHeaders, org.springframework.lang.Nullable

A WebSocket session abstraction. Allows sending messages over a WebSocket connection and closing it.
Author
Rossen Stoyanchev
Since
4.0

Method Summary

Modifier and TypeMethod and Description
public void
close()

Redeclares java.io.Closeable.close.

Close the WebSocket connection with status 1000, i.e. equivalent to:

session.close(CloseStatus.NORMAL);
public void
close(CloseStatus status)

Close the WebSocket connection with the given close status.

public String

Returns:

the protocol identifier, or null if no protocol was specified or negotiated successfully
getAcceptedProtocol
()

Return the negotiated sub-protocol.

public Map<String, Object>

Returns:

a Map with the session attributes (never null)
getAttributes
()

Return the map with attributes associated with the WebSocket session.

public int
getBinaryMessageSizeLimit()

Get the configured maximum size for an incoming binary message.

public List<WebSocketExtension>

Returns:

the list of extensions, or an empty list if no extension was specified or negotiated successfully
getExtensions
()

Determine the negotiated extensions.

public HttpHeaders
getHandshakeHeaders()

Return the headers used in the handshake request (never null).

public String
getId()

Return a unique session identifier.

public InetSocketAddress
getLocalAddress()

Return the address on which the request was received.

public Principal
getPrincipal()

Return a java.security.Principal instance containing the name of the authenticated user.

public InetSocketAddress
getRemoteAddress()

Return the address of the remote client.

public int
getTextMessageSizeLimit()

Get the configured maximum size for an incoming text message.

public URI
getUri()

Return the URI used to open the WebSocket connection.

public boolean
isOpen()

Whether the underlying connection is open.

public void
sendMessage(WebSocketMessage<?> message)

Send a WebSocket message: either TextMessage or BinaryMessage.

public void
setBinaryMessageSizeLimit(int messageSizeLimit)

Configure the maximum size for an incoming binary message.

public void
setTextMessageSizeLimit(int messageSizeLimit)

Configure the maximum size for an incoming text message.

Method Detail

closeback to summary
public void close() throws IOException

Redeclares java.io.Closeable.close.

Close the WebSocket connection with status 1000, i.e. equivalent to:

session.close(CloseStatus.NORMAL);
Annotations
@Override
Exceptions
IOException:

Doc from java.io.Closeable.close.

if an I/O error occurs

closeback to summary
public void close(CloseStatus status) throws IOException

Close the WebSocket connection with the given close status.

getAcceptedProtocolback to summary
public String getAcceptedProtocol()

Return the negotiated sub-protocol.

Returns:String

the protocol identifier, or null if no protocol was specified or negotiated successfully

Annotations
@Nullable
getAttributesback to summary
public Map<String, Object> getAttributes()

Return the map with attributes associated with the WebSocket session.

On the server side the map can be populated initially through a HandshakeInterceptor. On the client side the map can be populated via WebSocketClient handshake methods.

Returns:Map<String, Object>

a Map with the session attributes (never null)

getBinaryMessageSizeLimitback to summary
public int getBinaryMessageSizeLimit()

Get the configured maximum size for an incoming binary message.

getExtensionsback to summary
public List<WebSocketExtension> getExtensions()

Determine the negotiated extensions.

Returns:List<WebSocketExtension>

the list of extensions, or an empty list if no extension was specified or negotiated successfully

getHandshakeHeadersback to summary
public HttpHeaders getHandshakeHeaders()

Return the headers used in the handshake request (never null).

getIdback to summary
public String getId()

Return a unique session identifier.

getLocalAddressback to summary
public InetSocketAddress getLocalAddress()

Return the address on which the request was received.

Annotations
@Nullable
getPrincipalback to summary
public Principal getPrincipal()

Return a java.security.Principal instance containing the name of the authenticated user.

If the user has not been authenticated, the method returns null.

Annotations
@Nullable
getRemoteAddressback to summary
public InetSocketAddress getRemoteAddress()

Return the address of the remote client.

Annotations
@Nullable
getTextMessageSizeLimitback to summary
public int getTextMessageSizeLimit()

Get the configured maximum size for an incoming text message.

getUriback to summary
public URI getUri()

Return the URI used to open the WebSocket connection.

Annotations
@Nullable
isOpenback to summary
public boolean isOpen()

Whether the underlying connection is open.

sendMessageback to summary
public void sendMessage(WebSocketMessage<?> message) throws IOException

Send a WebSocket message: either TextMessage or BinaryMessage.

Note

The underlying standard WebSocket session (JSR-356) does not allow concurrent sending. Therefore, sending must be synchronized. To ensure that, one option is to wrap the WebSocketSession with the ConcurrentWebSocketSessionDecorator.

See Also
org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator
setBinaryMessageSizeLimitback to summary
public void setBinaryMessageSizeLimit(int messageSizeLimit)

Configure the maximum size for an incoming binary message.

setTextMessageSizeLimitback to summary
public void setTextMessageSizeLimit(int messageSizeLimit)

Configure the maximum size for an incoming text message.