11.4 结合在一起使用

    一如既往,我们要将它们结合在一起使用。使用Bootstrap引导服务器和设置正确的ChannelInitializer。看下面代码:

[java] view plaincopy

  1. package netty.in.action;
  2. import io.netty.bootstrap.ServerBootstrap;
  3. import io.netty.channel.Channel;
  4. import io.netty.channel.ChannelFuture;
  5. import io.netty.channel.ChannelInitializer;
  6. import io.netty.channel.EventLoopGroup;
  7. import io.netty.channel.group.ChannelGroup;
  8. import io.netty.channel.group.DefaultChannelGroup;
  9. import io.netty.channel.nio.NioEventLoopGroup;
  10. import io.netty.channel.socket.nio.NioServerSocketChannel;
  11. import io.netty.util.concurrent.ImmediateEventExecutor;
  12. import java.net.InetSocketAddress;
  13. /**
    • @author c.k
  14. */
  15. public class ChatServer {
  16. private final ChannelGroup group = new DefaultChannelGroup(
  17. ImmediateEventExecutor.INSTANCE);
  18. private final EventLoopGroup workerGroup = new NioEventLoopGroup();
  19. private Channel channel;
  20. public ChannelFuture start(InetSocketAddress address) {
  21. ServerBootstrap b = new ServerBootstrap();
  22. b.group(workerGroup).channel(NioServerSocketChannel.class)
  23. .childHandler(createInitializer(group));
  24. ChannelFuture f = b.bind(address).syncUninterruptibly();
  25. channel = f.channel();
  26. return f;
  27. }
  28. public void destroy() {
  29. if (channel != null)
  30. channel.close();
  31. group.close();
  32. workerGroup.shutdownGracefully();
  33. }
  34. protected ChannelInitializer<Channel> createInitializer(ChannelGroup group) {
  35. return new ChatServerInitializer(group);
  36. }
  37. public static void main(String[] args) {
  38. final ChatServer server = new ChatServer();
  39. ChannelFuture f = server.start(new InetSocketAddress(2048));
  40. Runtime.getRuntime().addShutdownHook(new Thread() {
  41. @Override
  42. public void run() {
  43. server.destroy();
  44. }
  45. });
  46. f.channel().closeFuture().syncUninterruptibly();
  47. }
  48. }

另外,需要将index.html文件放在项目根目录,index.html内容如下:

[html] view plaincopy

  1. <html>
  2. <head>
  3. <title>Web Socket Test</title>
  4. </head>
  5. <body>
  6. <script type="text/javascript">
  7. var socket;
  8. if (!window.WebSocket) {
  9. window.WebSocket = window.MozWebSocket;
  10. }
  11. if (window.WebSocket) {
  12. socket = new WebSocket("ws://localhost:2048/ws");
  13. socket.onmessage = function(event) {
  14. var ta = document.getElementById('responseText');
  15. ta.value = ta.value + '\n' + event.data
  16. };
  17. socket.onopen = function(event) {
  18. var ta = document.getElementById('responseText');
  19. ta.value = "Web Socket opened!";
  20. };
  21. socket.onclose = function(event) {
  22. var ta = document.getElementById('responseText');
  23. ta.value = ta.value + "Web Socket closed";
  24. };
  25. } else {
  26. alert("Your browser does not support Web Socket.");
  27. }
  28. function send(message) {
  29. if (!window.WebSocket) { return; }
  30. if (socket.readyState == WebSocket.OPEN) {
  31. socket.send(message);
  32. } else {
  33. alert("The socket is not open.");
  34. }
  35. }
  36. </script>
  37. <form onsubmit="return false;">
  38. <input type="text" name="message" value="Hello, World!"><input
  39. type="button" value="Send Web Socket Data"
  40. onclick="send(this.form.message.value)">
  41. <h3>Output</h3>
  42. <textarea id="responseText" style="width: 500px; height: 300px;"></textarea>
  43. </form>
  44. </body>
  45. </html>

最后在浏览器中输入:http://localhost:2048,多开几个窗口就可以聊天了。

results matching ""

    No results matching ""