Version: 9.4.19.v20190610 |

Using WebSocket Annotations
Previous | Chapter 27. Jetty Websocket API Home | Next

##### 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 for sponsored feature development

## Using WebSocket Annotations

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

    //
    //  ========================================================================
    //  Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
    //  ------------------------------------------------------------------------
    //  All rights reserved. This program and the accompanying materials
    //  are made available under the terms of the Eclipse Public License v1.0
    //  and Apache License v2.0 which accompanies this distribution.
    //
    //      The Eclipse Public License is available at
    //      http://www.eclipse.org/legal/epl-v10.html
    //
    //      The Apache License v2.0 is available at
    //      http://www.opensource.org/licenses/apache2.0.php
    //
    //  You may elect to redistribute this code under either of these licenses.
    //  ========================================================================
    //

    package examples.echo;

    import org.eclipse.jetty.websocket.api.Session;
    import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
    import org.eclipse.jetty.websocket.api.annotations.WebSocket;

    /**
     * Example EchoSocket using Annotations.
     */
    @WebSocket(maxTextMessageSize = 64 * 1024)
    public class AnnotatedEchoSocket
    {
        @OnWebSocketMessage
        public void onText(Session session, String message)
        {
            if (session.isOpen())
            {
                System.out.printf("Echoing back message [%s]%n",message);
                // echo the message back
                session.getRemote().sendString(message,null);
            }
        }
    }

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:

  - `Session` (optional)

  - `String text` (required)

    The method parameters for Binary messages:

  - `Session` (optional)
  - `byte buf[]` (required)
  - `int offset` (required)
  - `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.

Previous | Top | Next
Send Messages to Remote Endpoint | Home | Using WebSocketListener

See an error or something missing? Contribute to this documentation at Github!(Generated: 2019-06-10)