1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
79 }
80 }