我有一个使用代理来控制客户端互联网连接的客户端。如何将 Websocket 标头中的用户/通过身份验证值传递给代理。
我正在使用标准的 Netty 植入。
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
final String funcName = "messageReceived - ";
try {
Object msg = e.getMessage();
logger.debug(funcName + msg + "class" + msg.getClass().getName());
ThreadParams.setLoggingEnabled(true);
if (msg instanceof HttpRequest) {
handleHttpShakeHand(ctx, (HttpRequest) msg);
// TaUtils.sleep(5000);
} else if (msg instanceof WebSocketFrame) {
handleWebSocketFrame(ctx, (WebSocketFrame) msg);
}
} catch (Throwable t) {
logger.error(funcName + "error: ", t);
}
}
private void handleHttpShakeHand(ChannelHandlerContext ctx, HttpRequest req) throws Exception {
// Handshake
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
this.getWebSocketLocation(req), null, false);
this.handshaker = wsFactory.newHandshaker(req);
if (this.handshaker == null) {
wsFactory.sendUnsupportedWebSocketVersionResponse(ctx.getChannel());
} else {
this.handshaker.performOpeningHandshake(ctx.getChannel(), req);
}
return;
}
在客户端上,我使用 Chrome 的对象与服务器打开 Websocket。
ws = new WebSocket(localStorage["wsPath"]);
ws.onclose = onClose;
ws.onopen = onOpen;
ws.onerror = onError;
塔米尔