Jetty Logo
Version: 9.1.3-SNAPSHOT
Contact the core Jetty developers at www.webtide.com

private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery

Send Messages to Remote Endpoint

Blocking Send Message
Send Partial Message
Send Ping / Pong Control Frame
Async Send Message

The most important feature of the Session is access to the org.eclipse.jetty.websocket.api.RemoteEndpoint needed to send messages.

With RemoteEndpoint you can choose to send TEXT or BINARY WebSocket messages, or the WebSocket PING and PONG control frames.

Blocking Send Message

Most calls are blocking in nature, and will not return until the send has completed (or has thrown an exception).

Example 30.1. Send Binary Message (Blocking)

How to send a simple Binary message using the RemoteEndpoint. This will block until the message is sent, possibly throwing an IOException if unable to send the message.


Example 30.2. Send Text Message (Blocking)

How to send a simple Text message using the RemoteEndpoint. This will block until the message is sent, possibly throwing an IOException if unable to send the message.


Send Partial Message

If you have a large message to send, and want to send it in pieces and parts, you can utilize the partial message sending methods of RemoteEndpoint. Just be sure you finish sending your message (isLast == true)

Example 30.3. Send Partial Binary Message (Blocking)

How to send a Binary message in 2 parts, using the partial message support in RemoteEndpoint. This will block until each part of the message is sent, possibly throwing an IOException if unable to send the partial message.


Example 30.4. Send Partial Text Message (Blocking)

How to send a Text message in 2 parts, using the partial message support in RemoteEndpoint. This will block until each part of the message is sent, possibly throwing an IOException if unable to send the partial message.


Send Ping / Pong Control Frame

You can also send Ping and Pong control frames using the RemoteEndpoint.

Example 30.5. Send Ping Control Frame (Blocking)

How to send a Ping control frame, with a payload of "You There?" (arriving at Remote Endpoint as a byte array payload). This will block until the message is sent, possibly throwing an IOException if unable to send the ping frame.


Example 30.6. Send Pong Control Frame (Blocking)

How to send a Pong control frame, with a payload of "Yup I'm here" (arriving at Remote Endpoint as a byte array payload). This will block until the message is sent, possibly throwing an IOException if unable to send the pong frame.

To be correct in your usage of Pong frames, you should return the same byte array data that you received in the Ping frame


Async Send Message

However there are also 2 Async send message methods available:

Both return a Future<Void> that can be used to test for success and failure of the message send using standard java.util.concurrent.Future behavior.

Example 30.7. Send Binary Message (Async Simple)

How to send a simple Binary message using the RemoteEndpoint. The message will be enqueued for outgoing write, but you will not know if it succeeded or failed.


Example 30.8. Send Binary Message (Async, Wait Till Success)

How to send a simple Binary message using the RemoteEndpoint, tracking the Future<Void> to know if the send succeeded or failed.


Example 30.9. Send Binary Message (Async, timeout of send)

How to send a simple Binary message using the RemoteEndpoint, tracking the Future<Void> and waiting only prescribed amount of time for the send to complete, cancelling the message if the timeout occurs.


Example 30.10. Send Text Message (Async Simple)

How to send a simple Text message using the RemoteEndpoint. The message will be enqueued for outgoing write, but you will not know if it succeeded or failed.


Example 30.11. Send Text Message (Async, Wait Till Success)

How to send a simple Binary message using the RemoteEndpoint, tracking the Future<Void> to know if the send succeeded or failed.


Example 30.12. Send Text Message (Async, timeout of send)

How to send a simple Binary message using the RemoteEndpoint, tracking the Future<Void> and waiting only prescribed amount of time for the send to complete, cancelling the message if the timeout occurs.


See an error or something missing? Contribute to this documentation at Github!(Generated: 2014-04-15T11:43:49-05:00)