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.sslserver;
17  
18  import java.net.InetSocketAddress;
19  import java.util.concurrent.Executors;
20  
21  import org.jboss.netty.bootstrap.ServerBootstrap;
22  import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
23  
24  /**
25   * A HTTP server which serves Web Socket requests at:
26   *
27   * https://localhost:8081/websocket
28   *
29   * Open your browser at https://localhost:8081/, then the demo page will be loaded and a Web Socket connection will be
30   * made automatically.
31   *
32   * This server illustrates support for the different web socket specification versions and will work with:
33   *
34   * <ul>
35   * <li>Safari 5+ (draft-ietf-hybi-thewebsocketprotocol-00)
36   * <li>Chrome 6-13 (draft-ietf-hybi-thewebsocketprotocol-00)
37   * <li>Chrome 14+ (draft-ietf-hybi-thewebsocketprotocol-10)
38   * <li>Chrome 16+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17)
39   * <li>Firefox 7+ (draft-ietf-hybi-thewebsocketprotocol-10)
40   * </ul>
41   */
42  public class WebSocketSslServer {
43  
44      private final int port;
45  
46      public WebSocketSslServer(int port) {
47          this.port = port;
48      }
49  
50      public void run() {
51          // Configure the server.
52          ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
53                  Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
54  
55          // Set up the event pipeline factory.
56          bootstrap.setPipelineFactory(new WebSocketSslServerPipelineFactory());
57  
58          // Bind and start to accept incoming connections.
59          bootstrap.bind(new InetSocketAddress(port));
60  
61          System.out.println("Web socket server started at port " + port + '.');
62          System.out.println("Open your browser and navigate to https://localhost:" + port + '/');
63      }
64  
65      public static void main(String[] args) {
66          int port;
67          if (args.length > 0) {
68              port = Integer.parseInt(args[0]);
69          } else {
70              port = 8443;
71          }
72  
73          String keyStoreFilePath = System.getProperty("keystore.file.path");
74          if (keyStoreFilePath == null || keyStoreFilePath.length() == 0) {
75              System.out.println("ERROR: System property keystore.file.path not set. Exiting now!");
76              System.exit(1);
77          }
78  
79          String keyStoreFilePassword = System.getProperty("keystore.file.password");
80          if (keyStoreFilePassword == null || keyStoreFilePassword.length() == 0) {
81              System.out.println("ERROR: System property keystore.file.password not set. Exiting now!");
82              System.exit(1);
83          }
84  
85          new WebSocketSslServer(port).run();
86      }
87  }