Java tutorial
/** * Copyright (c) 2005-2012 https://github.com/javahuang * * Licensed under the Apache License, Version 2.0 (the "License"); */ package me.hrps.rp.preview.chat; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Channel; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import me.hrps.rp.preview.chat.service.WebSocketServerInitializer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; /** * * <p/>spring?,??????init?netty,netty,???web?? * <p/>???netty????chatServer???? * <p> * User: Huang rp * <p> * Date: 2015818 ?6:42:12 * <p> * Version: 1.0 */ public class ChatServer { Logger log = LoggerFactory.getLogger(getClass()); @Value("${chat.port}") private int PORT; @Autowired WebSocketServerInitializer socketServerInit; /** * ??? */ public void init() { new Thread(new Runnable() { @Override public void run() { log.info("???..."); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(socketServerInit); Channel ch = b.bind(PORT).sync().channel(); ch.closeFuture().sync(); } catch (Exception e) { log.error(e.getMessage()); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } }).start(); } // static final int PORT = Integer.parseInt(System.getProperty("port", // "9080")); }