package com.netty.wss;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;
import io.netty.util.CharsetUtil;
/**
* websocket 具体业务处理方法
*
* */
public class WebSocketHandler extends ChannelInboundHandlerAdapter{
private WebSocketServerHandshaker handshaker;
/**
* 当客户端连接成功,返回个成功信息
* */
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
}
/**
* 当客户端断开连接
* */
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
// 传统的HTTP接入
if(msg instanceof FullHttpRequest){
handleHttpRequest(ctx,(FullHttpRequest)msg);
}else if(msg instanceof WebSocketFrame){
handlerWebSocketFrame(ctx,(WebSocketFrame)msg);
}
super.channelRead(ctx, msg);
}
public void handlerWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception{
//关闭请求
if(frame instanceof CloseWebSocketFrame){
handshaker.close(ctx.channel(), (CloseWebSocketFrame)frame.retain());
return;
}
//只支持文本格式,不支持二进制消息
if(!(frame instanceof TextWebSocketFrame)){
throw new Exception("仅支持文本格式");
}
}
//第一次请求是http请求,请求头包括ws的信息
public void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req){
if(!req.decoderResult().isSuccess()){
sendHttpResponse(ctx,req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));
return;
}
// 构造握手响应返回
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory("ws:/"+ctx.channel()+ "/ws",null,false);
handshaker = wsFactory.newHandshaker(req);
// 请求头不合法, 导致handshaker没创建成功
if(handshaker == null){
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
}else{
handshaker.handshake(ctx.channel(), req);
}
}
public static void sendHttpResponse(ChannelHandlerContext ctx,FullHttpRequest req,DefaultFullHttpResponse res){
// 返回应答给客户端
if (res.status().code() != 200){
ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
res.content().writeBytes(buf);
buf.release();
}
// 如果是非Keep-Alive,关闭连接
ChannelFuture f = ctx.channel().writeAndFlush(res);
if (!isKeepAlive(req) || res.status().code() != 200){
f.addListener(ChannelFutureListener.CLOSE);
}
}
private static boolean isKeepAlive(FullHttpRequest req){
return false;
}
//异常处理,netty默认是关闭channel
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
//输出日志
cause.printStackTrace();
ctx.close();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
netty做服务端支持ssl协议实现websocket的wss协议(java)
共20个文件
class:5个
java:4个
jar:2个
3星 · 超过75%的资源 需积分: 5 393 下载量 17 浏览量
2018-05-16
15:57:45
上传
评论 7
收藏 5.89MB ZIP 举报
温馨提示
在网上查的资料,整理了一下 实现了wss协议的连接 和websocket的心跳,,在网上查的资料,整理了一下 实现了wss协议的连接 和websocket的心跳
资源推荐
资源详情
资源评论
收起资源包目录
WebsocketWss.zip (20个子文件)
WebsocketWss
src
com
netty
wss
SslUtil.java 1KB
AcceptorIdleStateTrigger.java 1KB
WebsocketServer.java 3KB
WebSocketHandler.java 4KB
bin
com
netty
wss
WebsocketServer.class 3KB
SslUtil.class 2KB
WebsocketServer$myChannelInitializer.class 3KB
WebSocketHandler.class 6KB
AcceptorIdleStateTrigger.class 2KB
.classpath 447B
index.js 4KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.core.resources.prefs 57B
org.eclipse.wst.jsdt.ui.superType.name 6B
.jsdtscope 680B
org.eclipse.jdt.core.prefs 598B
libs
netty-all-4.1.20.Final.jar 3.62MB
netty-all-4.1.20.Final-sources.jar 2.88MB
.project 576B
index.html 265B
共 20 条
- 1
invadersf
- 粉丝: 9
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页