| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>Using WebSocket Annotations</title><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><meta name="keywords" content="jetty, servlet, servlet-api, cometd, http, websocket, eclipse, maven, java, server, software"><link rel="home" href="index.html" title="Jetty"><link rel="up" href="websocket-jetty.html" title="Chapter 27. Jetty Websocket API"><link rel="prev" href="jetty-websocket-api-send-message.html" title="Send Messages to Remote Endpoint"><link rel="next" href="jetty-websocket-api-listener.html" title="Using WebSocketListener"><link xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" rel="shortcut icon" href="images/favicon.ico"><link rel="stylesheet" href="css/highlighter/foundation.css"><script src="js/highlight.pack.js"></script><script> |
| hljs.initHighlightingOnLoad(); |
| </script><link type="text/css" rel="stylesheet" href="css/font-awesome/font-awesome.min.css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><tr><td style="width: 25%"><a href="http://www.eclipse.org/jetty"><img src="images/jetty-header-logo.png" alt="Jetty Logo"></a><br><span style="font-size: small"> |
| Version: 9.3.28.v20191105</span></td><td style="width: 50%"></td></tr></table><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Using WebSocket Annotations</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="jetty-websocket-api-send-message.html"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</a> </td><th width="60%" align="center">Chapter 27. Jetty Websocket API<br><a accesskey="p" href="index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></th><td width="20%" align="right"> <a accesskey="n" href="jetty-websocket-api-listener.html">Next <i class="fa fa-chevron-right" aria-hidden="true"></i></a></td></tr></table><hr></div><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="jetty-callout"><h5 class="callout"><a href="http://www.webtide.com/">Contact the core Jetty developers at |
| <span class="website">www.webtide.com</span></a></h5><p> |
| 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 |
| </p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="jetty-websocket-api-annotations"></a>Using WebSocket Annotations</h2></div></div></div><p>The most basic form of WebSocket is a marked up POJO with annotations |
| provided by the Jetty WebSocket API.</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code>// |
| // ======================================================================== |
| // Copyright (c) 1995-2018 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); |
| } |
| } |
| }</code></pre><p>The above example is a simple WebSocket echo endpoint that will echo back any TEXT messages it receives.</p><p>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.</p><p>The annotations you have available:</p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/websocket/api/annotations/WebSocket.html" target="_top">@WebSocket</a></span></dt><dd><p class="simpara">A required class level annotation.</p><p class="simpara">Flags this POJO as being a WebSocket.</p><p class="simpara">The class must be not abstract and public.</p></dd><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/websocket/api/annotations/OnWebSocketConnect.html" target="_top">@OnWebSocketConnect</a></span></dt><dd><p class="simpara">An optional method level annotation.</p><p class="simpara">Flags one method in the class as receiving the On Connect event.</p><p class="simpara">Method must be public, not abstract, return void, and have a single <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/websocket/api/Session.html" target="_top">Session</a> parameter.</p></dd><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/websocket/api/annotations/OnWebSocketClose.html" target="_top">@OnWebSocketClose</a></span></dt><dd><p class="simpara">An optional method level annotation.</p><p class="simpara">Flags one method in the class as receiving the On Close event.</p><p class="simpara">Method signature must be public, not abstract, and return void.</p><p class="simpara">The method parameters:</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/websocket/api/Session.html" target="_top"><code class="literal">Session</code></a> (optional)</li><li class="listitem"><code class="literal">int closeCode</code> (required)</li><li class="listitem"><code class="literal">String closeReason</code> (required)</li></ol></div></dd><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/websocket/api/annotations/OnWebSocketMessage.html" target="_top">@OnWebSocketMessage</a></span></dt><dd><p class="simpara">An optional method level annotation.</p><p class="simpara">Flags up to 2 methods in the class as receiving On Message events.</p><p class="simpara">You can have 1 method for TEXT messages, and 1 method for BINARY messages.</p><p class="simpara">Method signature must be public, not abstract, and return void.</p><p class="simpara">The method parameters for Text messages:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/websocket/api/Session.html" target="_top"><code class="literal">Session</code></a> (optional)</li><li class="listitem"><p class="simpara"><code class="literal">String text</code> (required)</p><p class="simpara">The method parameters for Binary messages:</p></li><li class="listitem"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/websocket/api/Session.html" target="_top"><code class="literal">Session</code></a> (optional)</li><li class="listitem"><code class="literal">byte buf[]</code> (required)</li><li class="listitem"><code class="literal">int offset</code> (required)</li><li class="listitem"><code class="literal">int length</code> (required)</li></ul></div></dd><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/websocket/api/annotations/OnWebSocketError.html" target="_top">@OnWebSocketError</a></span></dt><dd><p class="simpara">An optional method level annotation.</p><p class="simpara">Flags one method in the class as receiving Error events from the WebSocket implementation.</p><p class="simpara">Method signatures must be public, not abstract, and return void.</p><p class="simpara">The method parameters:</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/websocket/api/Session.html" target="_top"><code class="literal">Session</code></a> (optional)</li><li class="listitem"><code class="literal">Throwable cause</code> (required)</li></ol></div></dd><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/websocket/api/annotations/OnWebSocketFrame.html" target="_top">@OnWebSocketFrame</a></span></dt><dd><p class="simpara">An optional method level annotation.</p><p class="simpara">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.</p><p class="simpara">Method signatures must be public, not abstract, and return void.</p><p class="simpara">The method parameters:</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/websocket/api/Session.html" target="_top"><code class="literal">Session</code></a> (optional)</li><li class="listitem"><p class="simpara"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/websocket/api/extensions/Frame.html" target="_top"><code class="literal">Frame</code></a> (required)</p><p class="simpara">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.</p></li></ol></div></dd></dl></div></div><script type="text/javascript"> |
| SyntaxHighlighter.all() |
| </script><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="jetty-websocket-api-send-message.html"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</a> </td><td width="20%" align="center"><a accesskey="u" href="websocket-jetty.html"><i class="fa fa-chevron-up" aria-hidden="true"></i> Top</a></td><td width="40%" align="right"> <a accesskey="n" href="jetty-websocket-api-listener.html">Next <i class="fa fa-chevron-right" aria-hidden="true"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Send Messages to Remote Endpoint </td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></td><td width="40%" align="right" valign="top"> Using WebSocketListener</td></tr></table></div><p xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><div class="jetty-callout"> |
| See an error or something missing? |
| <span class="callout"><a href="http://github.com/eclipse/jetty.project">Contribute to this documentation at |
| <span class="website"><i class="fa fa-github" aria-hidden="true"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2019-11-05)</i></span></div></p></body></html> |