com.fastchar.socket.client.websocket.FastWebSocketConvertHeaderHandler Maven / Gradle / Ivy
package com.fastchar.socket.client.websocket;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.HttpRequest;
public class FastWebSocketConvertHeaderHandler extends ChannelHandlerAdapter {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof HttpRequest) {
//此处做header二次添加,避免部分websocket服务器未返回响应的header照成netty验证失败!
HttpRequest httpRequest = (HttpRequest) msg;
httpRequest.headers()
.set("Upgrade", "websocket")
.set("Connection", "Upgrade")
.set("Sec-WebSocket-Key", httpRequest.headers().get("Sec-WebSocket-Key"))
.set("Sec-WebSocket-Version", "13")
;
}
super.write(ctx, msg, promise);
}
}