MCSM: прототип WebSocket
This commit is contained in:
@@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <dimon550@gmail.com>
|
||||||
|
* 2017-05-09
|
||||||
|
*/
|
||||||
|
package asys.mcsmanager.websocket;
|
||||||
|
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.channel.SimpleChannelInboundHandler;
|
||||||
|
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
||||||
|
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class FrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(FrameHandler.class);
|
||||||
|
private ScheduledFuture<?> sesFuture;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
ctx.channel().writeAndFlush(new TextWebSocketFrame("<S:channelActive>"));
|
||||||
|
|
||||||
|
ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
|
||||||
|
sesFuture = ses.scheduleAtFixedRate(() -> ctx.channel().writeAndFlush(new TextWebSocketFrame("<S:ping>")),
|
||||||
|
1L, 1L, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
super.channelActive(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
sesFuture.cancel(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception {
|
||||||
|
if (frame instanceof TextWebSocketFrame) {
|
||||||
|
String requestText = ((TextWebSocketFrame)frame).text();
|
||||||
|
logger.debug("{} received {}", ctx.channel(), requestText);
|
||||||
|
ctx.channel().writeAndFlush(new TextWebSocketFrame("<S>"+requestText));
|
||||||
|
} else {
|
||||||
|
logger.warn("unsupport frame type: {}", frame.getClass().getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
/*
|
|
||||||
* DmitriyMX <dimon550@gmail.com>
|
|
||||||
* 2017-05-08
|
|
||||||
*/
|
|
||||||
package asys.mcsmanager.websocket;
|
|
||||||
|
|
||||||
import io.netty.buffer.Unpooled;
|
|
||||||
import io.netty.channel.ChannelFutureListener;
|
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
|
||||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
||||||
import io.netty.handler.codec.http.*;
|
|
||||||
|
|
||||||
public class IndexPageHandler extends ChannelInboundHandlerAdapter {
|
|
||||||
private static final byte[] CONTENT = "Hello".getBytes();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
|
||||||
ctx.flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
||||||
if (msg instanceof HttpRequest) {
|
|
||||||
HttpRequest req = (HttpRequest)msg;
|
|
||||||
|
|
||||||
if (HttpHeaders.is100ContinueExpected(req)) {
|
|
||||||
ctx.write(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE));
|
|
||||||
}
|
|
||||||
boolean keepAlive = HttpHeaders.isKeepAlive(req);
|
|
||||||
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
|
|
||||||
Unpooled.wrappedBuffer(CONTENT));
|
|
||||||
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain");
|
|
||||||
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
|
|
||||||
|
|
||||||
if (!keepAlive) {
|
|
||||||
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
|
|
||||||
} else {
|
|
||||||
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
|
|
||||||
ctx.write(response);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -10,7 +10,9 @@ import io.netty.channel.EventLoopGroup;
|
|||||||
import io.netty.channel.nio.NioEventLoopGroup;
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
import io.netty.channel.socket.SocketChannel;
|
import io.netty.channel.socket.SocketChannel;
|
||||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||||
|
import io.netty.handler.codec.http.HttpObjectAggregator;
|
||||||
import io.netty.handler.codec.http.HttpServerCodec;
|
import io.netty.handler.codec.http.HttpServerCodec;
|
||||||
|
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
|
||||||
|
|
||||||
public class Server {
|
public class Server {
|
||||||
private EventLoopGroup bossGroup, workerGroup;
|
private EventLoopGroup bossGroup, workerGroup;
|
||||||
@@ -44,7 +46,9 @@ public class Server {
|
|||||||
protected void initChannel(SocketChannel socketChannel) throws Exception {
|
protected void initChannel(SocketChannel socketChannel) throws Exception {
|
||||||
socketChannel.pipeline().addLast(
|
socketChannel.pipeline().addLast(
|
||||||
new HttpServerCodec(),
|
new HttpServerCodec(),
|
||||||
new IndexPageHandler()
|
new HttpObjectAggregator(65536),
|
||||||
|
new WebSocketServerProtocolHandler("/", null, true),
|
||||||
|
new FrameHandler()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user