Top Description Methods
io.vertx.core.http

public Interface WebSocketBase

extends ReadStream<Buffer>, WriteStream<Buffer>
Known Direct Subinterfaces
io.vertx.core.http.impl.WebSocketInternal, io.vertx.core.http.ServerWebSocket, io.vertx.core.http.WebSocket
Annotations
@VertxGen
concrete:false
Imports
io.vertx.codegen.annotations.CacheReturn, .Fluent, .GenIgnore, .Nullable, .VertxGen, io.vertx.core.AsyncResult, .Future, .Handler, .MultiMap, io.vertx.core.buffer.Buffer, io.vertx.core.net.SocketAddress, io.vertx.core.streams.ReadStream, .WriteStream, javax.net.ssl.SSLPeerUnverifiedException, .SSLSession, javax.security.cert.X509Certificate, java.security.cert.Certificate, java.util.List

Base WebSocket implementation.

It implements both ReadStream and WriteStream so it can be used with io.vertx.core.streams.Pipe to pipe data with flow control.

Author
Tim Fox

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

the binary handler id
binaryHandlerID
()

When a WebSocket is created, it may register an event handler with the event bus - the ID of that handler is given by this method.

public WebSocketBase

Returns:

a reference to this, so the API can be used fluently
binaryMessageHandler
(@Nullable Handler<Buffer>
the handler
handler
)

Set a binary message handler on the connection.

public Future<Void>

Returns:

a future completed with the result
close
()

Close the WebSocket sending the default close frame.

public void
close(Handler<AsyncResult<Void>> handler)

Same as close() but with an handler called when the operation completes

public Future<Void>

Returns:

a future completed with the result
close
(short
the status code
statusCode
)

Close the WebSocket sending a close frame with specified status code.

public void
close(short statusCode, Handler<AsyncResult<Void>> handler)

Same as close(short) but with an handler called when the operation completes

public Future<Void>

Returns:

a future completed with the result
close
(short
the status code
statusCode
,
@Nullable String
reason of closure
reason
)

Close sending a close frame with specified status code and reason.

public void
close(short statusCode, @Nullable String reason, Handler<AsyncResult<Void>> handler)

Same as close(short, String) but with an handler called when the operation completes

public WebSocketBase

Returns:

a reference to this, so the API can be used fluently
closeHandler
(@Nullable Handler<Void>
the handler
handler
)

Set a close handler.

public String
closeReason()

Returns the close reason message from the remote endpoint or null when not yet received.

public Short
closeStatusCode()

Returns the close status code received from the remote endpoint or null when not yet received.

public WebSocketBase
public Future<Void>
end()

Redeclares as abstract: io.vertx.core.streams.WriteStream.end.

Ends the stream.

public void
end(Handler<AsyncResult<Void>> handler)

Redeclares io.vertx.core.streams.WriteStream.end.

Redeclares as abstract: io.vertx.core.streams.WriteStream.end.

Same as end() but with an handler called when the operation completes Calls close(Handler)

public WebSocketBase
endHandler(Handler<Void> endHandler)

public WebSocketBase
public WebSocketBase
fetch(long amount)

Redeclares io.vertx.core.streams.ReadStream.fetch.

Fetch the specified amount of elements.

public WebSocketBase

Returns:

a reference to this, so the API can be used fluently
frameHandler
(@Nullable Handler<WebSocketFrame>
the handler
handler
)

Set a frame handler on the connection.

public WebSocketBase
public MultiMap

Returns:

the headers
headers
()

Returns the HTTP headers when the WebSocket is first obtained in the handler.

public boolean

Returns:

true if the WebSocket is closed
isClosed
()

public boolean

Returns:

true if this io.vertx.core.http.HttpConnection is encrypted via SSL/TLS.
isSsl
()

public SocketAddress

Returns:

the local address for this connection, possibly null (e.g a server bound on a domain socket) If useProxyProtocol is set to true, the address returned will be of the proxy.
localAddress
()

public WebSocketBase
pause()

Redeclares io.vertx.core.streams.ReadStream.pause.

Pause the ReadStream, it sets the buffer in fetch mode and clears the actual demand.

public X509Certificate[]

Returns:

an ordered array of the peer certificates. Returns null if connection is not SSL.
peerCertificateChain
()
Deprecated instead use peerCertificates() or sslSession()

public List<Certificate>

Returns:

an ordered list of the peer certificates. Returns null if connection is not SSL.
peerCertificates
()

public WebSocketBase

Returns:

a reference to this, so the API can be used fluently
pongHandler
(@Nullable Handler<Buffer>
the handler
handler
)

Set a pong frame handler on the connection.

public SocketAddress

Returns:

the remote address for this connection, possibly null (e.g a server bound on a domain socket). If useProxyProtocol is set to true, the address returned will be of the actual connecting client.
remoteAddress
()

public WebSocketBase
resume()

Redeclares io.vertx.core.streams.ReadStream.resume.

Resume reading, and sets the buffer in flowing mode.

public WebSocketBase
setWriteQueueMaxSize(int
the max size of the write stream
maxSize
)

Redeclares io.vertx.core.streams.WriteStream.setWriteQueueMaxSize.

Set the maximum size of the write queue to maxSize.

public SSLSession

Returns:

SSLSession associated with the underlying socket. Returns null if connection is not SSL.
sslSession
()

public String
subProtocol()

Returns the WebSocket sub protocol selected by the WebSocket handshake.

public String

Returns:

the text handler id
textHandlerID
()

When a WebSocket is created, it may register an event handler with the eventbus, the ID of that handler is given by textHandlerID.

public WebSocketBase

Returns:

a reference to this, so the API can be used fluently
textMessageHandler
(@Nullable Handler<String>
the handler
handler
)

Set a text message handler on the connection.

public Future<Void>

Returns:

a future completed with the result
writeBinaryMessage
(Buffer
the data to write
data
)
References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Writes a (potentially large) piece of binary data to the connection.

public WebSocketBase
writeBinaryMessage(Buffer data, Handler<AsyncResult<Void>> handler)
References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Same as writeBinaryMessage(Buffer) but with an handler called when the operation completes

public Future<Void>

Returns:

a future completed with the result
writeFinalBinaryFrame
(Buffer
The data to write
data
)
References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Write a final WebSocket binary frame to the connection

public WebSocketBase
writeFinalBinaryFrame(Buffer data, Handler<AsyncResult<Void>> handler)
References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Same as writeFinalBinaryFrame(Buffer, Handler) but with an handler called when the operation completes

public Future<Void>

Returns:

a future completed with the result
writeFinalTextFrame
(String
The text to write
text
)

Write a final WebSocket text frame to the connection

public WebSocketBase
writeFinalTextFrame(String text, Handler<AsyncResult<Void>> handler)

Same as writeFinalTextFrame(String, Handler) but with an handler called when the operation completes

public Future<Void>

Returns:

a future completed with the result
writeFrame
(WebSocketFrame
the frame to write
frame
)

Write a WebSocket frame to the connection

public WebSocketBase
writeFrame(WebSocketFrame frame, Handler<AsyncResult<Void>> handler)

Same as writeFrame(WebSocketFrame) but with an handler called when the operation completes

public WebSocketBase

Returns:

a reference to this, so the API can be used fluently
writePing
(Buffer
the data to write, may be at most 125 bytes
data
,
Handler<AsyncResult<Void>>
called when the ping frame has been successfully written
handler
)
References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Writes a ping frame to the connection.

public Future<Void>
writePing(Buffer data)
References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Like writePing(Buffer, Handler) but with an handler called when the message has been written or failed to be written.

public WebSocketBase

Returns:

a reference to this, so the API can be used fluently
writePong
(Buffer
the data to write, may be at most 125 bytes
data
,
Handler<AsyncResult<Void>>
called when the pong frame has been successfully written
handler
)
References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Writes a pong frame to the connection.

public Future<Void>
writePong(Buffer data)
References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Like writePong(Buffer, Handler) but with an handler called when the message has been written or failed to be written.

public Future<Void>

Returns:

a future completed with the result
writeTextMessage
(String
the data to write
text
)

Writes a (potentially large) piece of text data to the connection.

public WebSocketBase
writeTextMessage(String text, Handler<AsyncResult<Void>> handler)

Same as writeTextMessage(String) but with an handler called when the operation completes

Inherited from io.vertx.core.streams.ReadStream:
collectendHandlerexceptionHandlerhandlerpipepipeTopipeTo
Inherited from io.vertx.core.streams.WriteStream:
drainHandlerendexceptionHandlerwritewritewriteQueueFull

Method Detail

binaryHandlerIDback to summary
public String binaryHandlerID()

When a WebSocket is created, it may register an event handler with the event bus - the ID of that handler is given by this method.

By default, no handler is registered, the feature must be enabled via WebSocketConnectOptions#setRegisterWriteHandlers(boolean) or HttpServerOptions#setRegisterWebSocketWriteHandlers(boolean).

Given this ID, a different event loop can send a binary frame to that event handler using the event bus and that buffer will be received by this instance in its own event loop and written to the underlying connection. This allows you to write data to other WebSockets which are owned by different event loops.

Returns:String

the binary handler id

See Also
WebSocketConnectOptions#setRegisterWriteHandlers(boolean), HttpServerOptions#setRegisterWebSocketWriteHandlers(boolean)
binaryMessageHandlerback to summary
public WebSocketBase binaryMessageHandler(@Nullable Handler<Buffer> handler)

Set a binary message handler on the connection. This handler serves a similar purpose to handler(Handler) except that if a message comes into the socket in multiple frames, the data from the frames will be aggregated into a single buffer before calling the handler (using WebSocketFrame#isFinal() to find the boundaries).

Parameters
handler:@Nullable Handler<Buffer>

the handler

Returns:WebSocketBase

a reference to this, so the API can be used fluently

Annotations
@Fluent
closeback to summary
public Future<Void> close()

Close the WebSocket sending the default close frame.

No more messages can be sent.

Returns:Future<Void>

a future completed with the result

closeback to summary
public void close(Handler<AsyncResult<Void>> handler)

Same as close() but with an handler called when the operation completes

closeback to summary
public Future<Void> close(short statusCode)

Close the WebSocket sending a close frame with specified status code. You can give a look at various close payloads here: RFC6455 section 7.4.1

No more messages can be sent.

Parameters
statusCode:short

the status code

Returns:Future<Void>

a future completed with the result

closeback to summary
public void close(short statusCode, Handler<AsyncResult<Void>> handler)

Same as close(short) but with an handler called when the operation completes

closeback to summary
public Future<Void> close(short statusCode, @Nullable String reason)

Close sending a close frame with specified status code and reason. You can give a look at various close payloads here: RFC6455 section 7.4.1

No more messages can be sent.

Parameters
statusCode:short

the status code

reason:@Nullable String

reason of closure

Returns:Future<Void>

a future completed with the result

closeback to summary
public void close(short statusCode, @Nullable String reason, Handler<AsyncResult<Void>> handler)

Same as close(short, String) but with an handler called when the operation completes

closeHandlerback to summary
public WebSocketBase closeHandler(@Nullable Handler<Void> handler)

Set a close handler. This will be called when the WebSocket is closed.

After this callback, no more messages are expected. When the WebSocket received a close frame, the closeStatusCode() will return the status code and closeReason() will return the reason.

Parameters
handler:@Nullable Handler<Void>

the handler

Returns:WebSocketBase

a reference to this, so the API can be used fluently

Annotations
@Fluent
closeReasonback to summary
public String closeReason()

Returns the close reason message from the remote endpoint or null when not yet received.

closeStatusCodeback to summary
public Short closeStatusCode()

Returns the close status code received from the remote endpoint or null when not yet received.

drainHandlerback to summary
public WebSocketBase drainHandler(Handler<Void> handler)
Annotations
@Override
endback to summary
public Future<Void> end()

Redeclares as abstract: io.vertx.core.streams.WriteStream.end.

Doc from io.vertx.core.streams.WriteStream.end.

Ends the stream.

Once the stream has ended, it cannot be used any more. Calls close()

Returns:Future<Void>

a future completed with the result

Annotations
@Override
endback to summary
public void end(Handler<AsyncResult<Void>> handler)

Redeclares io.vertx.core.streams.WriteStream.end.

Redeclares as abstract: io.vertx.core.streams.WriteStream.end.

Doc from io.vertx.core.streams.WriteStream.end.

Same as end() but with an handler called when the operation completes Calls close(Handler)

Annotations
@Override
endHandlerback to summary
public WebSocketBase endHandler(Handler<Void> endHandler)
Annotations
@Override
exceptionHandlerback to summary
public WebSocketBase exceptionHandler(Handler<Throwable> handler)
Annotations
@Override
fetchback to summary
public WebSocketBase fetch(long amount)

Redeclares io.vertx.core.streams.ReadStream.fetch.

Doc from io.vertx.core.streams.ReadStream.fetch.

Fetch the specified amount of elements. If the ReadStream has been paused, reading will recommence with the specified amount of items, otherwise the specified amount will be added to the current stream demand.

Returns:WebSocketBase

a reference to this, so the API can be used fluently

Annotations
@Override
frameHandlerback to summary
public WebSocketBase frameHandler(@Nullable Handler<WebSocketFrame> handler)

Set a frame handler on the connection. This handler will be called when frames are read on the connection.

Parameters
handler:@Nullable Handler<WebSocketFrame>

the handler

Returns:WebSocketBase

a reference to this, so the API can be used fluently

Annotations
@Fluent
handlerback to summary
public WebSocketBase handler(Handler<Buffer> handler)
Annotations
@Override
headersback to summary
public MultiMap headers()

Returns the HTTP headers when the WebSocket is first obtained in the handler.

The headers will be null on subsequent interactions.

Returns:MultiMap

the headers

isClosedback to summary
public boolean isClosed()
Returns:boolean

true if the WebSocket is closed

isSslback to summary
public boolean isSsl()
Returns:boolean

true if this io.vertx.core.http.HttpConnection is encrypted via SSL/TLS.

localAddressback to summary
public SocketAddress localAddress()
Returns:SocketAddress

the local address for this connection, possibly null (e.g a server bound on a domain socket) If useProxyProtocol is set to true, the address returned will be of the proxy.

Annotations
@CacheReturn
pauseback to summary
public WebSocketBase pause()

Redeclares io.vertx.core.streams.ReadStream.pause.

Doc from io.vertx.core.streams.ReadStream.pause.

Pause the ReadStream, it sets the buffer in fetch mode and clears the actual demand.

While it's paused, no data will be sent to the data handler.

Returns:WebSocketBase

a reference to this, so the API can be used fluently

Annotations
@Override
peerCertificateChainback to summary
public X509Certificate[] peerCertificateChain() throws SSLPeerUnverifiedException

Deprecated

instead use peerCertificates() or sslSession()

Returns:X509Certificate[]

an ordered array of the peer certificates. Returns null if connection is not SSL.

Annotations
@Deprecated
@GenIgnore
Exceptions
SSLPeerUnverifiedException:
SSL peer's identity has not been verified.
See Also
javax.net.ssl.SSLSession#getPeerCertificateChain(), sslSession()
peerCertificatesback to summary
public List<Certificate> peerCertificates() throws SSLPeerUnverifiedException
Returns:List<Certificate>

an ordered list of the peer certificates. Returns null if connection is not SSL.

Annotations
@GenIgnore
Exceptions
SSLPeerUnverifiedException:
SSL peer's identity has not been verified.
See Also
javax.net.ssl.SSLSession#getPeerCertificateChain(), sslSession()
pongHandlerback to summary
public WebSocketBase pongHandler(@Nullable Handler<Buffer> handler)

Set a pong frame handler on the connection. This handler will be invoked every time a pong frame is received on the server, and can be used by both clients and servers since the RFC 6455 section 5.5.2 and section 5.5.3 do not specify whether the client or server sends a ping.

Pong frames may be at most 125 bytes (octets).

There is no ping handler since ping frames should immediately be responded to with a pong frame with identical content

Pong frames may be received unsolicited.

Parameters
handler:@Nullable Handler<Buffer>

the handler

Returns:WebSocketBase

a reference to this, so the API can be used fluently

Annotations
@Fluent
remoteAddressback to summary
public SocketAddress remoteAddress()
Returns:SocketAddress

the remote address for this connection, possibly null (e.g a server bound on a domain socket). If useProxyProtocol is set to true, the address returned will be of the actual connecting client.

Annotations
@CacheReturn
resumeback to summary
public WebSocketBase resume()

Redeclares io.vertx.core.streams.ReadStream.resume.

Doc from io.vertx.core.streams.ReadStream.resume.

Resume reading, and sets the buffer in flowing mode.

If the ReadStream has been paused, reading will recommence on it.

Returns:WebSocketBase

a reference to this, so the API can be used fluently

Annotations
@Override
setWriteQueueMaxSizeback to summary
public WebSocketBase setWriteQueueMaxSize(int maxSize)

Redeclares io.vertx.core.streams.WriteStream.setWriteQueueMaxSize.

Doc from io.vertx.core.streams.WriteStream.setWriteQueueMaxSize.

Set the maximum size of the write queue to maxSize. You will still be able to write to the stream even if there is more than maxSize items in the write queue. This is used as an indicator by classes such as Pipe to provide flow control.

The value is defined by the implementation of the stream, e.g in bytes for a io.vertx.core.net.NetSocket, etc...

Parameters
maxSize:int

the max size of the write stream

Returns:WebSocketBase

a reference to this, so the API can be used fluently

Annotations
@Override
sslSessionback to summary
public SSLSession sslSession()
Returns:SSLSession

SSLSession associated with the underlying socket. Returns null if connection is not SSL.

Annotations
@GenIgnore:permitted-type
See Also
javax.net.ssl.SSLSession
subProtocolback to summary
public String subProtocol()

Returns the WebSocket sub protocol selected by the WebSocket handshake.

On the server, the value will be null when the handler receives the WebSocket callback as the handshake will not be completed yet.

textHandlerIDback to summary
public String textHandlerID()

When a WebSocket is created, it may register an event handler with the eventbus, the ID of that handler is given by textHandlerID.

By default, no handler is registered, the feature must be enabled via WebSocketConnectOptions#setRegisterWriteHandlers(boolean) or HttpServerOptions#setRegisterWebSocketWriteHandlers(boolean).

Given this ID, a different event loop can send a text frame to that event handler using the event bus and that buffer will be received by this instance in its own event loop and written to the underlying connection. This allows you to write data to other WebSockets which are owned by different event loops.

Returns:String

the text handler id

See Also
WebSocketConnectOptions#setRegisterWriteHandlers(boolean), HttpServerOptions#setRegisterWebSocketWriteHandlers(boolean)
textMessageHandlerback to summary
public WebSocketBase textMessageHandler(@Nullable Handler<String> handler)

Set a text message handler on the connection. This handler will be called similar to the binaryMessageHandler(Handler), but the buffer will be converted to a String first

Parameters
handler:@Nullable Handler<String>

the handler

Returns:WebSocketBase

a reference to this, so the API can be used fluently

Annotations
@Fluent
writeBinaryMessageback to summary
public Future<Void> writeBinaryMessage(Buffer data)

References Deprecated

Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Writes a (potentially large) piece of binary data to the connection. This data might be written as multiple frames if it exceeds the maximum WebSocket frame size.

Parameters
data:Buffer

the data to write

Returns:Future<Void>

a future completed with the result

writeBinaryMessageback to summary
public WebSocketBase writeBinaryMessage(Buffer data, Handler<AsyncResult<Void>> handler)

References Deprecated

Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Same as writeBinaryMessage(Buffer) but with an handler called when the operation completes

Annotations
@Fluent
writeFinalBinaryFrameback to summary
public Future<Void> writeFinalBinaryFrame(Buffer data)

References Deprecated

Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Write a final WebSocket binary frame to the connection

Parameters
data:Buffer

The data to write

Returns:Future<Void>

a future completed with the result

writeFinalBinaryFrameback to summary
public WebSocketBase writeFinalBinaryFrame(Buffer data, Handler<AsyncResult<Void>> handler)

References Deprecated

Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Same as writeFinalBinaryFrame(Buffer, Handler) but with an handler called when the operation completes

Annotations
@Fluent
writeFinalTextFrameback to summary
public Future<Void> writeFinalTextFrame(String text)

Write a final WebSocket text frame to the connection

Parameters
text:String

The text to write

Returns:Future<Void>

a future completed with the result

writeFinalTextFrameback to summary
public WebSocketBase writeFinalTextFrame(String text, Handler<AsyncResult<Void>> handler)

Same as writeFinalTextFrame(String, Handler) but with an handler called when the operation completes

Annotations
@Fluent
writeFrameback to summary
public Future<Void> writeFrame(WebSocketFrame frame)

Write a WebSocket frame to the connection

Parameters
frame:WebSocketFrame

the frame to write

Returns:Future<Void>

a future completed with the result

writeFrameback to summary
public WebSocketBase writeFrame(WebSocketFrame frame, Handler<AsyncResult<Void>> handler)

Same as writeFrame(WebSocketFrame) but with an handler called when the operation completes

Annotations
@Fluent
writePingback to summary
public WebSocketBase writePing(Buffer data, Handler<AsyncResult<Void>> handler)

References Deprecated

Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Writes a ping frame to the connection. This will be written in a single frame. Ping frames may be at most 125 bytes (octets).

This method should not be used to write application data and should only be used for implementing a keep alive or to ensure the client is still responsive, see RFC 6455 Section section 5.5.2.

There is no handler for ping frames because RFC 6455 clearly states that the only response to a ping frame is a pong frame with identical contents.

Parameters
data:Buffer

the data to write, may be at most 125 bytes

handler:Handler<AsyncResult<Void>>

called when the ping frame has been successfully written

Returns:WebSocketBase

a reference to this, so the API can be used fluently

Annotations
@Fluent
writePingback to summary
public Future<Void> writePing(Buffer data)

References Deprecated

Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Like writePing(Buffer, Handler) but with an handler called when the message has been written or failed to be written.

writePongback to summary
public WebSocketBase writePong(Buffer data, Handler<AsyncResult<Void>> handler)

References Deprecated

Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Writes a pong frame to the connection. This will be written in a single frame. Pong frames may be at most 125 bytes (octets).

This method should not be used to write application data and should only be used for implementing a keep alive or to ensure the client is still responsive, see RFC 6455 section 5.5.2.

There is no need to manually write a pong frame, as the server and client both handle responding to a ping from with a pong from automatically and this is exposed to users. RFC 6455 section 5.5.3 states that pongs may be sent unsolicited in order to implement a one way heartbeat.

Parameters
data:Buffer

the data to write, may be at most 125 bytes

handler:Handler<AsyncResult<Void>>

called when the pong frame has been successfully written

Returns:WebSocketBase

a reference to this, so the API can be used fluently

Annotations
@Fluent
writePongback to summary
public Future<Void> writePong(Buffer data)

References Deprecated

Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Like writePong(Buffer, Handler) but with an handler called when the message has been written or failed to be written.

writeTextMessageback to summary
public Future<Void> writeTextMessage(String text)

Writes a (potentially large) piece of text data to the connection. This data might be written as multiple frames if it exceeds the maximum WebSocket frame size.

Parameters
text:String

the data to write

Returns:Future<Void>

a future completed with the result

writeTextMessageback to summary
public WebSocketBase writeTextMessage(String text, Handler<AsyncResult<Void>> handler)

Same as writeTextMessage(String) but with an handler called when the operation completes

Annotations
@Fluent