马大波 1 год назад
Родитель
Сommit
b1b176bb28

+ 1 - 0
src/main/java/com/xiaobao/gateway/protocol/dto/MediaServerDTO.java

@@ -36,6 +36,7 @@ public class MediaServerDTO {
         List<MediaServerDTO> serverList = new ArrayList<>();
         serverList.add(new MediaServerDTO("192.168.66.73", 1935, 8000)); // 本地服务器
         serverList.add(new MediaServerDTO("39.101.185.102", 1935, 8001)); // 公网服务器
+        // serverList.add(new MediaServerDTO("127.0.0.1", 8007, 8002)); // Echo服务器
         return serverList;
     }
 }

+ 44 - 0
src/main/java/com/xiaobao/gateway/protocol/echo/EchoServer.java

@@ -0,0 +1,44 @@
+package com.xiaobao.gateway.protocol.echo;
+
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.channel.*;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.handler.logging.LogLevel;
+import io.netty.handler.logging.LoggingHandler;
+
+public class EchoServer {
+    static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));
+
+    public static void main(String[] args) throws Exception {
+        // Configure the server.
+        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
+        EventLoopGroup workerGroup = new NioEventLoopGroup();
+        final EchoServerHandler serverHandler = new EchoServerHandler();
+        try {
+            ServerBootstrap b = new ServerBootstrap();
+            b.group(bossGroup, workerGroup)
+                .channel(NioServerSocketChannel.class)
+                .option(ChannelOption.SO_BACKLOG, 100)
+                .handler(new LoggingHandler(LogLevel.INFO))
+                .childHandler(new ChannelInitializer<SocketChannel>() {
+                    @Override
+                    public void initChannel(SocketChannel ch) throws Exception {
+                        ChannelPipeline p = ch.pipeline();
+                        p.addLast(serverHandler);
+                    }
+                });
+
+            // Start the server.
+            ChannelFuture f = b.bind(PORT).sync();
+
+            // Wait until the server socket is closed.
+            f.channel().closeFuture().sync();
+        } finally {
+            // Shut down all event loops to terminate all threads.
+            bossGroup.shutdownGracefully();
+            workerGroup.shutdownGracefully();
+        }
+    }
+}

+ 26 - 0
src/main/java/com/xiaobao/gateway/protocol/echo/EchoServerHandler.java

@@ -0,0 +1,26 @@
+package com.xiaobao.gateway.protocol.echo;
+
+import io.netty.channel.ChannelHandler;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+
+@ChannelHandler.Sharable
+public class EchoServerHandler extends ChannelInboundHandlerAdapter {
+
+    @Override
+    public void channelRead(ChannelHandlerContext ctx, Object msg) {
+        ctx.write(msg);
+    }
+
+    @Override
+    public void channelReadComplete(ChannelHandlerContext ctx) {
+        ctx.flush();
+    }
+
+    @Override
+    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
+        // Close the connection when an exception is raised.
+        cause.printStackTrace();
+        ctx.close();
+    }
+}