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    *   http://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 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   * Generates the demo HTML page which is served at http://localhost:8080/
    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          // Unused
    78      }
    79  }