blob: f925a5d837d4eaeab90d83e7e04597467a6c12d1 [file]
<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-NS Stylesheets V1.76.1"><meta name="keywords" content="jetty, servlet, servlet-api, cometd, http, spdy, websocket, eclipse, maven, java, server, software"><link rel="home" href="index.html" title="Jetty : The Definitive Reference"><link rel="up" href="websocket-jetty.html" title="Chapter&nbsp;30.&nbsp;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"><script type="text/javascript" src="js/shCore.js"></script><script type="text/javascript" src="js/shBrushJava.js"></script><script type="text/javascript" src="js/shBrushXml.js"></script><script type="text/javascript" src="js/shBrushBash.js"></script><script type="text/javascript" src="js/shBrushJScript.js"></script><script type="text/javascript" src="js/shBrushSql.js"></script><script type="text/javascript" src="js/shBrushProperties.js"></script><script type="text/javascript" src="js/shBrushPlain.js"></script><link type="text/css" rel="stylesheet" href="css/shCore.css"><link type="text/css" rel="stylesheet" href="css/shThemeEclipse.css"><link type="text/css" rel="stylesheet" href="css/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.2.21.v20170120</span></td><td style="width: 50%"><script type="text/javascript"> (function() {
var cx = '016459005284625897022:obd4lsai2ds';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script><gcse:search></gcse:search></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="icon-chevron-left"></i> Previous</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;30.&nbsp;Jetty Websocket API<br><a accesskey="p" href="index.html"><i class="icon-home"></i> Home</a></th><td width="20%" align="right">&nbsp;<a accesskey="n" href="jetty-websocket-api-listener.html">Next <i class="icon-chevron-right"></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 from 1 day to full product delivery
</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><div class="example"><a name="d0e21978"></a><p class="title"><b>Example&nbsp;30.13.&nbsp;AnnotatedEchoSocket.java</b></p><div class="example-contents"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[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);
session.getRemote().sendString(message, null);
}
}
}
]]>
</script></div></div><br class="example-break"><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><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/annotations/WebSocket.html" target="_top">@WebSocket</a></span></dt><dd><p>A required class level annotation.</p><p>Flags this POJO as being a WebSocket.</p><p>The class must be not abstract and public.</p></dd><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/annotations/OnWebSocketConnect.html" target="_top">@OnWebSocketConnect</a></span></dt><dd><p>An optional method level annotation.</p><p>Flags one method in the class as receiving the On Connect
event.</p><p>Method must be public, not abstract, return void, and have a
single <a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/Session.html" target="_top">Session</a>
parameter.</p></dd><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/annotations/OnWebSocketClose.html" target="_top">@OnWebSocketClose</a></span></dt><dd><p>An optional method level annotation.</p><p>Flags one method in the class as receiving the On Close
event.</p><p>Method signature must be public, not abstract, and return
void. </p><p>The method parameters:</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p><a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/Session.html" target="_top"><code class="code">Session</code></a>
(optional)</p></li><li class="listitem"><p><code class="code">int closeCode</code> (required)</p></li><li class="listitem"><p><code class="code">String closeReason</code> (required)</p></li></ol></div></dd><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/annotations/OnWebSocketMessage.html" target="_top">@OnWebSocketMessage</a></span></dt><dd><p>An optional method level annotation.</p><p>Flags up to 2 methods in the class as receiving On Message
events.</p><p>You can have 1 method for TEXT messages, and 1 method for
BINARY messages.</p><p>Method signature must be public, not abstract, and return
void.</p><p>The method parameters for Text messages:</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p><a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/Session.html" target="_top"><code class="code">Session</code></a>
(optional)</p></li><li class="listitem"><p><code class="code">String text</code> (required)</p></li></ol></div><p>The method parameters for Binary messages:</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p><a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/Session.html" target="_top"><code class="code">Session</code></a>
(optional)</p></li><li class="listitem"><p><code class="code">byte buf[]</code> (required)</p></li><li class="listitem"><p><code class="code">int offset</code> (required)</p></li><li class="listitem"><p><code class="code">int length</code> (required)</p></li></ol></div></dd><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/annotations/OnWebSocketError.html" target="_top">@OnWebSocketError</a></span></dt><dd><p>An optional method level annotation.</p><p>Flags one method in the class as receiving Error events from
the WebSocket implementation.</p><p>Method signatures must be public, not abstract, and return
void.</p><p>The method parameters:</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p><a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/Session.html" target="_top"><code class="code">Session</code></a>
(optional)</p></li><li class="listitem"><p><code class="code">Throwable cause</code> (required)</p></li></ol></div></dd><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/annotations/OnWebSocketFrame.html" target="_top">@OnWebSocketFrame</a></span></dt><dd><p>An optional method level annotation.</p><p>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>Method signatures must be public, not abstract, and return
void.</p><p>The method parameters:</p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p><a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/Session.html" target="_top"><code class="code">Session</code></a>
(optional)</p></li><li class="listitem"><p><a class="link" href="http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/websocket/api/extensions/Frame.html" target="_top"><code class="code">Frame</code></a>
(required)</p></li></ol></div><p>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></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="icon-chevron-left"></i> Previous</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="websocket-jetty.html"><i class="icon-chevron-up"></i> Top</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="jetty-websocket-api-listener.html">Next <i class="icon-chevron-right"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Send Messages to Remote Endpoint&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="icon-home"></i> Home</a></td><td width="40%" align="right" valign="top">&nbsp;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/jetty-project/jetty-documentation">Contribute to this documentation at
<span class="website"><i class="icon-github"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2015-03-13T11:47:55-05:00)</i></span></div></p><script 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" type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1149868-7']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script></body></html>