json/src/main/java/net/java/html/json/doc-files/websockets.html
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 365 5c93ad8c7a15
permissions -rw-r--r--
Updating copyright headers to mention current year
     1 <!--
     2 
     3     DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4 
     5     Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     6 
     7     Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     8     Other names may be trademarks of their respective owners.
     9 
    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]"
    27 
    28     Contributor(s):
    29 
    30     The Original Software is NetBeans. The Initial Developer of the Original
    31     Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    32 
    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.
    43 
    44 -->
    45 <!DOCTYPE html>
    46 <html>
    47     <head>
    48         <title>WebSockets via @OnReceive</title>
    49         <meta charset="UTF-8">
    50         <link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
    51     </head>
    52     <body>
    53         <h1>Using <a href="../OnReceive.html">@OnReceive</a> to Communicate 
    54             via WebSockets Protocol</h1>
    55         
    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.
    62         
    63         <h3>Define JSON Class</h3>
    64         
    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:
    68     <pre>
    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>),
    80         })
    81         <b>static class</b> <em>RequestModel</em> {
    82         }
    83 
    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>),
    93         })
    94         <b>static class</b> <em>ResponseModel</em> {
    95         }
    96 
    97         <b>enum</b> <em>MsgType</em> {
    98             CreateGame, QueryGames, SendMove, JoinGame, UpdateGame;
    99         }
   100         <b>enum</b> <em>Color</em> {
   101             W, B;
   102         }
   103     }            
   104     </pre>
   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:
   108     <pre>
   109     <a href="../OnReceive.html">@OnReceive</a>(
   110         data = <em>Request</em>.<b>class</b>, 
   111         url = "{url}", 
   112         method = <em>"WebSocket"</em>, 
   113         onError = "anErrorHappened"
   114     ) 
   115     <b>static void</b> queryServer(Comm c, Response r) {
   116         <b>if</b> (r == null) {
   117             // <em>connection stablished!</em>
   118             <b>return</b>;
   119         }
   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>;
   127         }
   128     }
   129     <b>static void</b> anErrorHappened(Comm c, Exception t) {
   130         if (t == null) {
   131             // <em>OK, connection has been closed</em>
   132         } else {
   133             // <em>handle the error t somehow</em>
   134         }
   135     }
   136     </pre>
   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:
   140     <ul>
   141         <li><b>onopen</b> - the <em>queryServer</em> method is called with 
   142             <code>null</code> message
   143         </li>
   144         <li><b>onmessage</b> - the <em>queryServer</em> method is called with 
   145             non-null message
   146         </li>
   147         <li><b>onclose</b> - the <em>anErrorHappened</em> method is called with
   148             <code>null</code> exception
   149         </li>
   150         <li><b>onerror</b> - the <em>anErrorHappened</em> method is called with
   151             non-null exception
   152         </li>
   153     </ul>
   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:
   159     <pre>
   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
   166     }
   167 
   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);
   171     }
   172 
   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>);
   176     }
   177     </pre>
   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.
   182     <p>
   183     Enjoy <em>WebSocket</em>s via <a href="../OnReceive.html">@OnReceive</a>!
   184     </body>
   185 </html>