1 /*
2 * Copyright 2012 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License, version
5 * 2.0 (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 under
14 * the License.
15 */
16 package org.jboss.netty.example.http.websocketx.html5;
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 import org.jboss.netty.example.http.websocketx.server.WebSocketServerPipelineFactory;
24
25 /**
26 * A WebSocket Server that respondes to requests at:
27 *
28 * <pre>
29 * http://localhost:8080/websocket
30 * </pre>
31 *
32 * The example differs from many of the other examples in Netty in that is does
33 * not have an acomponying client. Instead a html page is provided that
34 * interacts with this server. <br>
35 * Open up the following file a web browser that supports WebSocket's:
36 *
37 * <pre>
38 * netty/src/test/resources/websocketx/html5/websocket.html
39 * </pre>
40 *
41 * The html page is very simple were you simply enter some text and the server
42 * will echo the same text back, but in uppercase. You, also see status messages
43 * in the "Response From Server" area when client has connected, disconnected
44 * etc.
45 *
46 */
47 public class WebSocketServer {
48
49 private final int port;
50
51 public WebSocketServer(int port) {
52 this.port = port;
53 }
54
55 public void run() {
56 // Configure the server.
57 ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
58 Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
59
60 // Set up the event pipeline factory.
61 bootstrap.setPipelineFactory(new WebSocketServerPipelineFactory());
62
63 // Bind and start to accept incoming connections.
64 bootstrap.bind(new InetSocketAddress(port));
65
66 System.out.println("Web socket server started at port " + port + '.');
67 }
68
69 public static void main(String[] args) {
70 int port;
71 if (args.length > 0) {
72 port = Integer.parseInt(args[0]);
73 } else {
74 port = 8080;
75 }
76 new WebSocketServer(port).run();
77 }
78
79 }