3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5 Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
7 Oracle and Java are registered trademarks of Oracle and/or its affiliates.
8 Other names may be trademarks of their respective owners.
10 The contents of this file are subject to the terms of either the GNU
11 General Public License Version 2 only ("GPL") or the Common
12 Development and Distribution License("CDDL") (collectively, the
13 "License"). You may not use this file except in compliance with the
14 License. You can obtain a copy of the License at
15 http://www.netbeans.org/cddl-gplv2.html
16 or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
17 specific language governing permissions and limitations under the
18 License. When distributing the software, include this License Header
19 Notice in each file and include the License file at
20 nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
21 particular file as subject to the "Classpath" exception as provided
22 by Oracle in the GPL Version 2 section of the License file that
23 accompanied this code. If applicable, add the following below the
24 License Header, with the fields enclosed by brackets [] replaced by
25 your own identifying information:
26 "Portions Copyrighted [year] [name of copyright owner]"
30 The Original Software is NetBeans. The Initial Developer of the Original
31 Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
33 If you wish your version of this file to be governed by only the CDDL
34 or only the GPL Version 2, indicate your decision by adding
35 "[Contributor] elects to include this software in this distribution
36 under the [CDDL or GPL Version 2] license." If you do not indicate a
37 single choice of license, a recipient has the option to distribute
38 your version of this file under either the CDDL, the GPL Version 2 or
39 to extend the choice of license to its licensees as provided above.
40 However, if you add GPL Version 2 code and therefore, elected the GPL
41 Version 2 license, then the option applies only if the new code is
42 made subject to such option by the copyright holder.
48 <title>WebSockets via @OnReceive</title>
49 <meta charset="UTF-8">
50 <link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
53 <h1>Using <a href="../OnReceive.html">@OnReceive</a> to Communicate
54 via WebSockets Protocol</h1>
56 There is a simple, yet flexible way to communicate with a server
57 via <a href="http://www.w3.org/TR/websockets/">WebSockets protocol</a>.
58 The support can transfer any classes generated by
59 <a href="../Model.html">@Model</a> annotation and reuses already
60 existing <a href="../OnReceive.html">@OnReceive</a> infrastructure -
61 just defines detailed special behavior, which is described here.
63 <h3>Define JSON Class</h3>
65 The first step in using <em>WebSockets</em> is to create a model classes
66 to encapsulate communiation with the server. For example one for
67 sending requests and one for receiving replies:
69 <a href="../Model.html">@Model</a>(className = "Comm", properties={})
70 <b>final class</b> Communication {
71 <a href="../Model.html">@Model</a>(className = "Request", properties = {
72 <a href="../Property.html">@Property</a>(name = "msg", type = MsgType<b>.class</b>),
73 <a href="../Property.html">@Property</a>(name = "username", type = String<b>.class</b>),
74 <a href="../Property.html">@Property</a>(name = "password", type = String<b>.class</b>),
75 <a href="../Property.html">@Property</a>(name = "gameId", type = String<b>.class</b>),
76 <a href="../Property.html">@Property</a>(name = "color", type = Color<b>.class</b>),
77 <a href="../Property.html">@Property</a>(name = "from", type = String<b>.class</b>),
78 <a href="../Property.html">@Property</a>(name = "to", type = String<b>.class</b>),
79 <a href="../Property.html">@Property</a>(name = "observer", type = boolean<b>.class</b>),
81 <b>static class</b> <em>RequestModel</em> {
84 <a href="../Model.html">@Model</a>(className = "Response", properties = {
85 <a href="../Property.html">@Property</a>(name = "msg", type = MsgType<b>.class</b>),
86 <a href="../Property.html">@Property</a>(name = "turn", type = Color<b>.class</b>),
87 <a href="../Property.html">@Property</a>(name = "color", type = Color<b>.class</b>),
88 <a href="../Property.html">@Property</a>(name = "gameId", type = String<b>.class</b>),
89 <a href="../Property.html">@Property</a>(name = "status", type = String<b>.class</b>),
90 <a href="../Property.html">@Property</a>(name = "moves", type = String<b>.class</b>, array = <b>true</b>),
91 <a href="../Property.html">@Property</a>(name = "from", type = String<b>.class</b>),
92 <a href="../Property.html">@Property</a>(name = "to", type = String<b>.class</b>),
94 <b>static class</b> <em>ResponseModel</em> {
97 <b>enum</b> <em>MsgType</em> {
98 CreateGame, QueryGames, SendMove, JoinGame, UpdateGame;
100 <b>enum</b> <em>Color</em> {
105 And then it is just a matter of creating the communication end point. As
106 usual with <a href="../OnReceive.html">@OnReceive</a> annotation one starts
107 with defining the response handler:
109 <a href="../OnReceive.html">@OnReceive</a>(
110 data = <em>Request</em>.<b>class</b>,
112 method = <em>"WebSocket"</em>,
113 onError = "anErrorHappened"
115 <b>static void</b> queryServer(Comm c, Response r) {
116 <b>if</b> (r == null) {
117 // <em>connection stablished!</em>
120 // <em>message arrived!</em>
121 <b>switch</b> (r.getMsg()) {
122 <b>case</b> CreateGame: /* do something */ <b>break</b>;
123 <b>case</b> QueryGames: /* do something */ <b>break</b>;
124 <b>case</b> SendMove: /* do something */ <b>break</b>;
125 <b>case</b> JoinGame: /* do something */ <b>break</b>;
126 <b>case</b> UpdateGame: /* do something */ <b>break</b>;
129 <b>static void</b> anErrorHappened(Comm c, Exception t) {
131 // <em>OK, connection has been closed</em>
133 // <em>handle the error t somehow</em>
137 The <a href="http://www.w3.org/TR/websockets/">WebSockets</a> specification
138 usually defines what should happen <em>onopen, onmessage, onclose and onerror</em>.
139 All these messages are supported in the previous example as well:
141 <li><b>onopen</b> - the <em>queryServer</em> method is called with
142 <code>null</code> message
144 <li><b>onmessage</b> - the <em>queryServer</em> method is called with
147 <li><b>onclose</b> - the <em>anErrorHappened</em> method is called with
148 <code>null</code> exception
150 <li><b>onerror</b> - the <em>anErrorHappened</em> method is called with
154 Using the <a href="../OnReceive.html">@OnReceive</a> annotation instructs
155 its associated annotation processor to add appropriate send method
156 into the generated <code>Comm</code> class. One can use it to establish communication,
157 send messages and close the communication channel. Here are three methods
158 showing how to do it:
160 <b>static void </b>connect(Comm c) {
161 // open a websocket channel:
162 c.queryServer("ws://server/path", <b>null</b>);
163 // the method returns immediately and starts establishing the connection
164 // once that is finished either <b>onopen</b> or <b>onerror</b> type of
165 // message is delivered
168 <b>static void </b>sendMessage(Comm c, Request r) {
169 // sends the message to already openned websocket channel:
170 c.queryServer("ws://server/path", r);
173 <b>static void </b>disconnect(Comm c) {
174 // sending <code>null</code> again, closes the connection
175 c.queryServer("ws://server/path", <b>null</b>);
178 One cannot change the URL while a connection is open otherwise one
179 risks <code>IllegalStateException</code> runtime exceptions. To connect
180 to different web socket server, one needs to close the connection first and
181 only later open new one with different URL.
183 Enjoy <em>WebSocket</em>s via <a href="../OnReceive.html">@OnReceive</a>!