2 * Copyright 2012 The Netty Project
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package org.opendaylight.yangtools.websocket.server;
18 import io.netty.bootstrap.ServerBootstrap;
19 import io.netty.channel.Channel;
20 import io.netty.channel.EventLoopGroup;
21 import io.netty.channel.nio.NioEventLoopGroup;
22 import io.netty.channel.socket.nio.NioServerSocketChannel;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * A HTTP server which serves Web Socket requests at:
29 * http://localhost:8080/websocket
31 * Open your browser at http://localhost:8080/, then the demo page will be loaded and a Web Socket connection will be
34 * This server illustrates support for the different web socket specification versions and will work with:
37 * <li>Safari 5+ (draft-ietf-hybi-thewebsocketprotocol-00)
38 * <li>Chrome 6-13 (draft-ietf-hybi-thewebsocketprotocol-00)
39 * <li>Chrome 14+ (draft-ietf-hybi-thewebsocketprotocol-10)
40 * <li>Chrome 16+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17)
41 * <li>Firefox 7+ (draft-ietf-hybi-thewebsocketprotocol-10)
42 * <li>Firefox 11+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17)
45 public class WebSocketServer implements Runnable {
47 private final int port;
48 private final ServerBootstrap bootstrap = new ServerBootstrap();
49 private final EventLoopGroup bossGroup = new NioEventLoopGroup();
50 private final EventLoopGroup workerGroup = new NioEventLoopGroup();
51 private static final Logger logger = LoggerFactory.getLogger(WebSocketServer.class.toString());
53 public WebSocketServer(int port) {
60 } catch (Exception e) {
61 logger.info("Exception occured while starting webSocket server {}",e);
64 public void startServer() throws Exception {
66 bootstrap.group(bossGroup, workerGroup)
67 .channel(NioServerSocketChannel.class)
68 .childHandler(new WebSocketServerInitializer());
70 Channel ch = bootstrap.bind(port).sync().channel();
71 logger.info("Web socket server started at port " + port + '.');
72 logger.info("Open your browser and navigate to http://localhost:" + port + '/');
74 ch.closeFuture().sync();
76 bossGroup.shutdownGracefully();
77 workerGroup.shutdownGracefully();