View Javadoc

    1   /*
    2    * Copyright 2010 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * 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.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   * Generates the demo HTML page which is served at http://localhost:8080/
    25   *
    26   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    27   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    28   *
    29   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    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  }