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 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 * http://localhost:8080/websocket 28 * 29 * Open your browser at http://localhost:8080/, 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 * <li>Firefox 11+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17) 41 * </ul> 42 */ 43 public class WebSocketServer { 44 45 private final int port; 46 47 public WebSocketServer(int port) { 48 this.port = port; 49 } 50 51 public void run() { 52 // Configure the server. 53 ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory( 54 Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); 55 56 // Set up the event pipeline factory. 57 bootstrap.setPipelineFactory(new WebSocketServerPipelineFactory()); 58 59 // Bind and start to accept incoming connections. 60 bootstrap.bind(new InetSocketAddress(port)); 61 62 System.out.println("Web socket server started at port " + port + '.'); 63 System.out.println("Open your browser and navigate to http://localhost:" + port + '/'); 64 } 65 66 public static void main(String[] args) { 67 int port; 68 if (args.length > 0) { 69 port = Integer.parseInt(args[0]); 70 } else { 71 port = 8080; 72 } 73 new WebSocketServer(port).run(); 74 } 75 }