garbagetown/playdocja — pull request #553 by wm3 (2014-03-12 16:34:23+00:00)
Title: manual/javaGuide/main/async/JavaWebSockets.md

- https://github.com/garbagetown/playdocja/blob/2.2.0/documentation/2.2.0/manual/javaGuide/main/async/JavaWebSockets.md
```
--- /Users/garbagetown/Desktop/2.1.5/manual/javaGuide/main/async/JavaWebSockets.md	2013-09-19 22:53:02.000000000 +0900
+++ /Users/garbagetown/Desktop/2.2.0/manual/javaGuide/main/async/JavaWebSockets.md	2013-09-19 10:11:22.000000000 +0900
@@ -10,7 +10,7 @@
>
>WebSocket is designed to be implemented in web browsers and web servers, but it can be used by any client or server application. Because ordinary TCP connections to port numbers other than 80 are frequently blocked by administrators outside of home environments, it can be used as a way to circumvent these restrictions and provide similar functionality with some additional protocol overhead while multiplexing several WebSocket services over a single TCP port. Additionally, it serves a purpose for web applications that require real-time bi-directional communication. Before the implementation of WebSocket, such bi-directional communication was only possible using comet channels; however, a comet is not trivial to implement reliably, and due to the TCP Handshake and HTTP header overhead, it may be inefficient for small messages. The WebSocket protocol aims to solve these problems without compromising security assumptions of the web.
>
-> [[http://en.wikipedia.org/wiki/WebSocket]]
+>

## Handling WebSockets

@@ -18,40 +18,7 @@

To handle a WebSocket your method must return a `WebSocket` instead of a `Result`:

-'''
-public static WebSocket index() {
- return new WebSocket() {
-
- // Called when the Websocket Handshake is done.
- public void onReady(WebSocket.In in, WebSocket.Out out) {
-
- // For each event received on the socket,
- in.onMessage(new Callback() {
- public void invoke(String event) {
-
- // Log events to the console
- println(event);
-
- }
- });
-
- // When the socket is closed.
- in.onClose(new Callback0() {
- public void invoke() {
-
- println("Disconnected");
-
- }
- });
-
- // Send a single 'Hello!' message
- out.write("Hello!");
-
- }
-
- }
-}
-'''
+@[websocket](code/javaguide/async/JavaWebSockets.java)

A WebSocket has access to the request headers (from the HTTP request that initiates the WebSocket connection) allowing you to retrieve standard headers and session data. But it doesn't have access to any request body, nor to the HTTP response.

@@ -59,21 +26,10 @@

It this example, we print each message to console and we send a single **Hello!** message.

-> **Tip:** You can test your WebSocket controller on [[http://websocket.org/echo.html]]. Just set the location to `ws://localhost:9000`.
+> **Tip:** You can test your WebSocket controller on . Just set the location to `ws://localhost:9000`.

Let’s write another example that totally discards the input data and closes the socket just after sending the **Hello!** message:

-'''
-public static WebSocket index() {
- return new WebSocket() {
-
- public void onReady(WebSocket.In in, WebSocket.Out out) {
- out.write("Hello!");
- out.close();
- }
-
- };
-}
-'''
+@[discard-input](code/javaguide/async/JavaWebSockets.java)

-> **Next:** [[The template engine | JavaTemplates]]
\ No newline at end of file
+> **Next:** [[The template engine | JavaTemplates]]

```