Version: 9.4.5.v20170502 |
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
The most basic form of WebSocket is a marked up POJO with annotations provided by the Jetty WebSocket API.
//
// ========================================================================
// Copyright (c) 1995-2017 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:
A required class level annotation.
Flags this POJO as being a WebSocket.
The class must be not abstract and public.
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.
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:
Session
(optional)int closeCode
(required)String closeReason
(required)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:
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:
Session
(optional)Throwable cause
(required)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: