1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.example.http.websocketx.server;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19 import org.jboss.netty.buffer.ChannelBuffers;
20 import org.jboss.netty.util.CharsetUtil;
21
22
23
24
25 public final class WebSocketServerIndexPage {
26
27 private static final String NEWLINE = "\r\n";
28
29 public static ChannelBuffer getContent(String webSocketLocation) {
30 return ChannelBuffers.copiedBuffer(
31 "<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, CharsetUtil.US_ASCII);
74 }
75
76 private WebSocketServerIndexPage() {
77
78 }
79 }