1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.example.http.websocket;
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
26
27
28
29
30
31 public class WebSocketServerIndexPage {
32
33 private static final String NEWLINE = "\r\n";
34
35 public static ChannelBuffer getContent(String webSocketLocation) {
36 return ChannelBuffers.copiedBuffer(
37 "<html><head><title>Web Socket Test</title></head>" + NEWLINE +
38 "<body>" + NEWLINE +
39 "<script type=\"text/javascript\">" + NEWLINE +
40 "var socket;" + NEWLINE +
41 "if (window.WebSocket) {" + NEWLINE +
42 " socket = new WebSocket(\"" + webSocketLocation + "\");" + NEWLINE +
43 " socket.onmessage = function(event) { alert(event.data); };" + NEWLINE +
44 " socket.onopen = function(event) { alert(\"Web Socket opened!\"); };" + NEWLINE +
45 " socket.onclose = function(event) { alert(\"Web Socket closed.\"); };" + NEWLINE +
46 "} else {" + NEWLINE +
47 " alert(\"Your browser does not support Web Socket.\");" + NEWLINE +
48 "}" + NEWLINE +
49 "" + NEWLINE +
50 "function send(message) {" + NEWLINE +
51 " if (!window.WebSocket) { return; }" + NEWLINE +
52 " if (socket.readyState == WebSocket.OPEN) {" + NEWLINE +
53 " socket.send(message);" + NEWLINE +
54 " } else {" + NEWLINE +
55 " alert(\"The socket is not open.\");" + NEWLINE +
56 " }" + NEWLINE +
57 "}" + NEWLINE +
58 "</script>" + NEWLINE +
59 "<form onsubmit=\"return false;\">" + NEWLINE +
60 "<input type=\"text\" name=\"message\" value=\"Hello, World!\"/>" +
61 "<input type=\"button\" value=\"Send Web Socket Data\" onclick=\"send(this.form.message.value)\" />" + NEWLINE +
62 "</form>" + NEWLINE +
63 "</body>" + NEWLINE +
64 "</html>" + NEWLINE,
65 CharsetUtil.US_ASCII);
66 }
67 }