View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.example.http.websocketx.server;
17  
18  import io.netty5.buffer.api.Buffer;
19  import io.netty5.buffer.api.BufferAllocator;
20  
21  import static java.nio.charset.StandardCharsets.US_ASCII;
22  
23  /**
24   * Generates the demo HTML page which is served at http://localhost:8080/
25   */
26  public final class WebSocketServerIndexPage {
27  
28      private static final String NEWLINE = "\r\n";
29  
30      public static Buffer getContent(BufferAllocator allocator, String webSocketLocation) {
31          final String content = "<html><head><title>Web Socket Test</title></head>" + NEWLINE +
32                  "<body>" + NEWLINE +
33                  "<script type=\"text/javascript\">" + NEWLINE +
34                  "var socket;" + NEWLINE +
35                  "if (!window.WebSocket) {" + NEWLINE +
36                  "  window.WebSocket = window.MozWebSocket;" + NEWLINE +
37                  '}' + NEWLINE +
38                  "if (window.WebSocket) {" + NEWLINE +
39                  "  socket = new WebSocket(\"" + webSocketLocation + "\");" + NEWLINE +
40                  "  socket.onmessage = function(event) {" + NEWLINE +
41                  "    var ta = document.getElementById('responseText');" + NEWLINE +
42                  "    ta.value = ta.value + '\\n' + event.data" + NEWLINE +
43                  "  };" + NEWLINE +
44                  "  socket.onopen = function(event) {" + NEWLINE +
45                  "    var ta = document.getElementById('responseText');" + NEWLINE +
46                  "    ta.value = \"Web Socket opened!\";" + NEWLINE +
47                  "  };" + NEWLINE +
48                  "  socket.onclose = function(event) {" + NEWLINE +
49                  "    var ta = document.getElementById('responseText');" + NEWLINE +
50                  "    ta.value = ta.value + \"Web Socket closed\"; " + NEWLINE +
51                  "  };" + NEWLINE +
52                  "} else {" + NEWLINE +
53                  "  alert(\"Your browser does not support Web Socket.\");" + NEWLINE +
54                  '}' + NEWLINE +
55                  NEWLINE +
56                  "function send(message) {" + NEWLINE +
57                  "  if (!window.WebSocket) { return; }" + NEWLINE +
58                  "  if (socket.readyState == WebSocket.OPEN) {" + NEWLINE +
59                  "    socket.send(message);" + NEWLINE +
60                  "  } else {" + NEWLINE +
61                  "    alert(\"The socket is not open.\");" + NEWLINE +
62                  "  }" + NEWLINE +
63                  '}' + NEWLINE +
64                  "</script>" + NEWLINE +
65                  "<form onsubmit=\"return false;\">" + NEWLINE +
66                  "<input type=\"text\" name=\"message\" value=\"Hello, World!\"/>" +
67                  "<input type=\"button\" value=\"Send Web Socket Data\"" + NEWLINE +
68                  "       onclick=\"send(this.form.message.value)\" />" + NEWLINE +
69                  "<h3>Output</h3>" + NEWLINE +
70                  "<textarea id=\"responseText\" style=\"width:500px;height:300px;\"></textarea>" + NEWLINE +
71                  "</form>" + NEWLINE +
72                  "</body>" + NEWLINE +
73                  "</html>" + NEWLINE;
74          return allocator.copyOf(content, US_ASCII);
75      }
76  
77      private WebSocketServerIndexPage() {
78          // Unused
79      }
80  }