package com.lance.net.server.common;
import java.io.RandomAccessFile;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.lance.net.server.module.UserInfo;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.DefaultFileRegion;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.http.QueryStringDecoder;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.stream.ChunkedNioFile;
public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
private Logger loger = LogManager.getLogger();
private final String webUri;
private final String INDEX = "E:\\oworkspace\\test\\src\\main\\webapp\\index.html";
public HttpRequestHandler(String webUri) {
this.webUri = webUri;
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
loger.info("===========> {}, {}", webUri, request.uri());
String uri = StringUtils.substringBefore(request.uri(), "?");
if(webUri.equalsIgnoreCase(uri)) {//获取webSocket参数
QueryStringDecoder query = new QueryStringDecoder(request.uri());
Map<String, List<String>> map = query.parameters();
List<String> tokens = map.get("token");
//根据参数保存当前登录对象, 并把该token加入到channel中
if(tokens != null && !tokens.isEmpty()) {
String token = tokens.get(0);
ChatConstants.addOnlines(token, new UserInfo(token));
ctx.channel().attr(ChatConstants.CHANNEL_TOKEN_KEY).getAndSet(token);
}
request.setUri(uri);
ctx.fireChannelRead(request.retain());
}else {
if(HttpUtil.is100ContinueExpected(request)) {
send100ContinueExpected(ctx);
}
RandomAccessFile file = new RandomAccessFile(INDEX, "r");
HttpResponse response = new DefaultHttpResponse(request.protocolVersion(), HttpResponseStatus.OK);
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
boolean keepAlive = HttpUtil.isKeepAlive(request);
if(keepAlive) {
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, file.length());
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
ctx.write(response);
if(ctx.pipeline().get(SslHandler.class) == null) {
ctx.write(new DefaultFileRegion(file.getChannel(), 0, file.length()));
}else {
ctx.write(new ChunkedNioFile(file.getChannel()));
}
ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
if(!keepAlive) {
future.addListener(ChannelFutureListener.CLOSE);
}
file.close();
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
private void send100ContinueExpected(ChannelHandlerContext ctx) {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONFLICT);
ctx.writeAndFlush(response);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
springboot基础netty和websocket
共328个文件
gif:183个
jpg:60个
js:15个
4星 · 超过85%的资源 需积分: 48 67 下载量 191 浏览量
2018-07-02
22:54:47
上传
评论
收藏 961KB RAR 举报
温馨提示
基于springboot+netty+webscoket实现的及时通讯,封装netty相关接口
资源推荐
资源详情
资源评论
收起资源包目录
springboot基础netty和websocket (328个子文件)
HttpRequestHandler.class 7KB
TextWebSocketFrameHandler.class 5KB
ChatServer.class 4KB
ChatConstants.class 3KB
NettyWebSocketServerApplication.class 2KB
ChatServerInitializer.class 2KB
ChatController.class 2KB
ChatMessage.class 2KB
ChatHeartbeatHandler.class 2KB
UserInfo.class 2KB
LoginController.class 1KB
NettyWebSocketServerApplication$1.class 925B
ChatServer$1.class 678B
.classpath 1KB
org.eclipse.wst.common.component 770B
org.eclipse.wst.jsdt.ui.superType.container 49B
chat.css 9KB
chat.css 9KB
chat.css 9KB
emo_01.gif 10KB
emo_01.gif 10KB
emo_01.gif 10KB
emo_35.gif 10KB
emo_35.gif 10KB
emo_35.gif 10KB
emo_37.gif 8KB
emo_37.gif 8KB
emo_37.gif 8KB
emo_26.gif 7KB
emo_26.gif 7KB
emo_26.gif 7KB
emo_29.gif 7KB
emo_29.gif 7KB
emo_29.gif 7KB
emo_52.gif 6KB
emo_52.gif 6KB
emo_52.gif 6KB
emo_05.gif 6KB
emo_05.gif 6KB
emo_05.gif 6KB
emo_07.gif 6KB
emo_07.gif 6KB
emo_07.gif 6KB
emo_17.gif 6KB
emo_17.gif 6KB
emo_17.gif 6KB
emo_14.gif 5KB
emo_14.gif 5KB
emo_14.gif 5KB
emo_55.gif 5KB
emo_55.gif 5KB
emo_55.gif 5KB
emo_34.gif 5KB
emo_34.gif 5KB
emo_34.gif 5KB
emo_33.gif 5KB
emo_33.gif 5KB
emo_33.gif 5KB
emo_42.gif 5KB
emo_42.gif 5KB
emo_42.gif 5KB
emo_18.gif 4KB
emo_18.gif 4KB
emo_18.gif 4KB
emo_30.gif 4KB
emo_30.gif 4KB
emo_30.gif 4KB
emo_40.gif 4KB
emo_40.gif 4KB
emo_40.gif 4KB
emo_22.gif 4KB
emo_22.gif 4KB
emo_22.gif 4KB
emo_57.gif 4KB
emo_57.gif 4KB
emo_57.gif 4KB
emo_24.gif 4KB
emo_24.gif 4KB
emo_24.gif 4KB
emo_21.gif 4KB
emo_21.gif 4KB
emo_21.gif 4KB
emo_16.gif 4KB
emo_16.gif 4KB
emo_16.gif 4KB
emo_36.gif 4KB
emo_36.gif 4KB
emo_36.gif 4KB
emo_47.gif 4KB
emo_47.gif 4KB
emo_47.gif 4KB
emo_54.gif 4KB
emo_54.gif 4KB
emo_54.gif 4KB
emo_06.gif 3KB
emo_45.gif 3KB
emo_45.gif 3KB
emo_06.gif 3KB
emo_06.gif 3KB
emo_45.gif 3KB
共 328 条
- 1
- 2
- 3
- 4
资源评论
- 网哥6172018-09-26还要5分,不值得啊
- mynameisak472020-01-08有点旧了,但是可以用,不错
- bianleibis2021-08-09还可以,有参考价值.
风清扬鍀师傅
- 粉丝: 34
- 资源: 69
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功