11.4 结合在一起使用
一如既往,我们要将它们结合在一起使用。使用Bootstrap引导服务器和设置正确的ChannelInitializer。看下面代码:
[java] view plaincopy
- package netty.in.action;
- import io.netty.bootstrap.ServerBootstrap;
- import io.netty.channel.Channel;
- import io.netty.channel.ChannelFuture;
- import io.netty.channel.ChannelInitializer;
- import io.netty.channel.EventLoopGroup;
- import io.netty.channel.group.ChannelGroup;
- import io.netty.channel.group.DefaultChannelGroup;
- import io.netty.channel.nio.NioEventLoopGroup;
- import io.netty.channel.socket.nio.NioServerSocketChannel;
- import io.netty.util.concurrent.ImmediateEventExecutor;
- import java.net.InetSocketAddress;
- /**
- @author c.k
- */
- public class ChatServer {
- private final ChannelGroup group = new DefaultChannelGroup(
- ImmediateEventExecutor.INSTANCE);
- private final EventLoopGroup workerGroup = new NioEventLoopGroup();
- private Channel channel;
- public ChannelFuture start(InetSocketAddress address) {
- ServerBootstrap b = new ServerBootstrap();
- b.group(workerGroup).channel(NioServerSocketChannel.class)
- .childHandler(createInitializer(group));
- ChannelFuture f = b.bind(address).syncUninterruptibly();
- channel = f.channel();
- return f;
- }
- public void destroy() {
- if (channel != null)
- channel.close();
- group.close();
- workerGroup.shutdownGracefully();
- }
- protected ChannelInitializer<Channel> createInitializer(ChannelGroup group) {
- return new ChatServerInitializer(group);
- }
- public static void main(String[] args) {
- final ChatServer server = new ChatServer();
- ChannelFuture f = server.start(new InetSocketAddress(2048));
- Runtime.getRuntime().addShutdownHook(new Thread() {
- @Override
- public void run() {
- server.destroy();
- }
- });
- f.channel().closeFuture().syncUninterruptibly();
- }
- }
另外,需要将index.html文件放在项目根目录,index.html内容如下:
[html] view plaincopy
- <html>
- <head>
- <title>Web Socket Test</title>
- </head>
- <body>
- <script type="text/javascript">
- var socket;
- if (!window.WebSocket) {
- window.WebSocket = window.MozWebSocket;
- }
- if (window.WebSocket) {
- socket = new WebSocket("ws://localhost:2048/ws");
- socket.onmessage = function(event) {
- var ta = document.getElementById('responseText');
- ta.value = ta.value + '\n' + event.data
- };
- socket.onopen = function(event) {
- var ta = document.getElementById('responseText');
- ta.value = "Web Socket opened!";
- };
- socket.onclose = function(event) {
- var ta = document.getElementById('responseText');
- ta.value = ta.value + "Web Socket closed";
- };
- } else {
- alert("Your browser does not support Web Socket.");
- }
- function send(message) {
- if (!window.WebSocket) { return; }
- if (socket.readyState == WebSocket.OPEN) {
- socket.send(message);
- } else {
- alert("The socket is not open.");
- }
- }
- </script>
- <form onsubmit="return false;">
- <input type="text" name="message" value="Hello, World!"><input
- type="button" value="Send Web Socket Data"
- onclick="send(this.form.message.value)">
- <h3>Output</h3>
- <textarea id="responseText" style="width: 500px; height: 300px;"></textarea>
- </form>
- </body>
- </html>
最后在浏览器中输入:http://localhost:2048,多开几个窗口就可以聊天了。