Jetty Logo
Version: 9.2.2-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

Using WebSocket Annotations

The most basic form of WebSocket is a marked up POJO with annotations provided by the Jetty WebSocket API.

Example 31.13. AnnotatedEchoSocket.java


The above example is a simple WebSocket echo endpoint that will echo back any TEXT messages it receives.

This implementation is using a stateless approach to a Echo socket, as the Session is being passed into the Message event as the event occurs. This would allow you to reuse the single instance of the AnnotatedEchoSocket for working with multiple endpoints.

The annotations you have available:

@WebSocket

A required class level annotation.

Flags this POJO as being a WebSocket.

The class must be not abstract and public.

@OnWebSocketConnect

An optional method level annotation.

Flags one method in the class as receiving the On Connect event.

Method must be public, not abstract, return void, and have a single Session parameter.

@OnWebSocketClose

An optional method level annotation.

Flags one method in the class as receiving the On Close event.

Method signature must be public, not abstract, and return void.

The method parameters:

  1. Session (optional)

  2. int closeCode (required)

  3. String closeReason (required)

@OnWebSocketMessage

An optional method level annotation.

Flags up to 2 methods in the class as receiving On Message events.

You can have 1 method for TEXT messages, and 1 method for BINARY messages.

Method signature must be public, not abstract, and return void.

The method parameters for Text messages:

  1. Session (optional)

  2. String text (required)

The method parameters for Binary messages:

  1. Session (optional)

  2. byte buf[] (required)

  3. int offset (required)

  4. int length (required)

@OnWebSocketError

An optional method level annotation.

Flags one method in the class as receiving Error events from the WebSocket implementation.

Method signatures must be public, not abstract, and return void.

The method parameters:

  1. Session (optional)

  2. Throwable cause (required)

@OnWebSocketFrame

An optional method level annotation.

Flags one method in the class as receiving Frame events from the WebSocket implementation after they have been processed by any extensions declared during the Upgrade handshake.

Method signatures must be public, not abstract, and return void.

The method parameters:

  1. Session (optional)

  2. Frame (required)

The Frame received will be notified on this method, then be processed by Jetty, possibly resulting in another event, such as On Close, or On Message. Changes to the Frame will not be seen by Jetty.

See an error or something missing? Contribute to this documentation at Github!(Generated: 2014-08-30T01:00:41-07:00)