Начало нового протокола

This commit is contained in:
Gravit 2018-09-21 21:36:23 +07:00
parent 4e739a74b5
commit 5cbc81476f

View file

@ -1,27 +1,31 @@
package ru.gravit.launchserver.socket.websocket; package ru.gravit.launchserver.socket.websocket;
import io.netty.channel.ChannelHandler; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.WebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.util.concurrent.EventExecutorGroup; import io.netty.util.concurrent.GlobalEventExecutor;
import ru.gravit.utils.helper.IOHelper;
import java.util.Locale; import ru.gravit.utils.helper.LogHelper;
public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> { public class WebSocketFrameHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
static final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
@Override
public void channelActive(ChannelHandlerContext ctx) {
LogHelper.debug("New client %s", IOHelper.getIP(ctx.channel().remoteAddress()));
channels.add(ctx.channel());
}
@Override @Override
protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception { protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception {
// ping and pong frames already handled // ping and pong frames already handled
if (frame instanceof TextWebSocketFrame) { ByteBuf buf = frame.content();
// Send the uppercase string back. ByteBufInputStream input = new ByteBufInputStream(buf);
String request = ((TextWebSocketFrame) frame).text(); long handshake = input.readLong();
ctx.channel().writeAndFlush(new TextWebSocketFrame(request.toUpperCase(Locale.US))); long connection_flags = input.readLong();
} else { long type = input.readInt();
String message = "unsupported frame type: " + frame.getClass().getName(); LogHelper.debug("MessageHead: handshake %dl, flags %dl, type %d", handshake,connection_flags,type);
throw new UnsupportedOperationException(message);
}
} }
} }