- java -jar nat-server.jar [port, defalut 8888]
- java -jar nat-client.jar
- 外网地址:http://www.littleredhat1997.com/
- 内网地址:127.0.0.1
- http://www.littleredhat1997.com:10000/ <=> 127.0.0.1:8080
机器 | 标识 | 端口(默认) |
---|---|---|
外网代理 | Remote | 10000 |
服务端 | Server | 8888 |
客户端 | Client | - |
内网代理 | Local | 8080 |
消息体
= 消息体总长度
+ TYPE
+ length(CHANNEL_ID)
+ CHANNEL_ID
+ length(DATA)
+ DATA
private MessageType type;
private String channelId;
private byte[] data;
UNKNOWN(0),
REGISTER(1),
REGISTER_SUCCESS(2),
REGISTER_FAILURE(3),
CONNECTED(4),
DISCONNECTED(5),
DATA(6),
KEEPALIVE(7);
Parameters:
- maxFrameLength - the maximum length of the frame. If the length of the frame is greater than this value, TooLongFrameException will be thrown.
- lengthFieldOffset - the offset of the length field
- lengthFieldLength - the length of the length field
- lengthAdjustment - the compensation value to add to the value of the length field
- initialBytesToStrip - the number of first bytes to strip out from the decoded frame
示例:
new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)
public class MessageEncoder extends MessageToByteEncoder<Message> {
@Override
protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception {
// TODO
}
}
public class MessageDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
// TODO
}
}
Parameters:
- readerIdleTimeSeconds - an IdleStateEvent whose state is IdleState.READER_IDLE will be triggered when no read was performed for the specified period of time. Specify 0 to disable.
- writerIdleTimeSeconds - an IdleStateEvent whose state is IdleState.WRITER_IDLE will be triggered when no write was performed for the specified period of time. Specify 0 to disable.
- allIdleTimeSeconds - an IdleStateEvent whose state is IdleState.ALL_IDLE will be triggered when neither read nor write was performed for the specified period of time. Specify 0 to disable.
示例:
new IdleStateHandler(60, 30, 0)
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent e = (IdleStateEvent) evt;
if (e.state() == IdleState.READER_IDLE) {
// TODO
} else if (e.state() == IdleState.WRITER_IDLE) {
// TODO
} else if (e.state() == IdleState.ALL_IDLE) {
// TODO
}
}
}