本文整理匯總了Java中io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory類的典型用法代碼示例。如果您正苦於以下問題:Java WebSocketServerHandshakerFactory類的具體用法?Java WebSocketServerHandshakerFactory怎麽用?Java WebSocketServerHandshakerFactory使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
WebSocketServerHandshakerFactory類屬於io.netty.handler.codec.http.websocketx包,在下文中一共展示了WebSocketServerHandshakerFactory類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: handleHttpRequest
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //導入依賴的package包/類
private void handleHttpRequest(ChannelHandlerContext ctx,
FullHttpRequest req) {
if (!req.getDecoderResult().isSuccess()
|| (!"websocket".equals(req.headers().get("Upgrade")))) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));
return;
}
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
"ws://localhost:7777/websocket", null, false);
socketServerHandshaker = wsFactory.newHandshaker(req);
if (socketServerHandshaker == null) {
WebSocketServerHandshakerFactory
.sendUnsupportedWebSocketVersionResponse(ctx.channel());
} else {
socketServerHandshaker.handshake(ctx.channel(), req);
}
}
示例2: channelRead
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //導入依賴的package包/類
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if(msg instanceof FullHttpRequest){
FullHttpRequest req = (FullHttpRequest) msg;
//check if websocket upgrade encountered
if(req.headers().contains("Upgrade") || req.headers().contains("upgrade")) {
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
getWebSocketLocation(req, ctx), null, true, 1024 * 1024 * 1024);
handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
handshaker.handshake(ctx.channel(), req);
}
return;
}
}
super.channelRead(ctx, msg);
}
示例3: handleHttpRequest
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //導入依賴的package包/類
private void handleHttpRequest(ChannelHandlerContext ctx,
FullHttpRequest req) throws Exception {
// 如果HTTP解碼失敗,返回HHTP異常
if (!req.getDecoderResult().isSuccess()
|| (!"websocket".equals(req.headers().get("Upgrade")))) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1,
BAD_REQUEST));
return;
}
// 構造握手響應返回,本機測試
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
"ws://localhost:8080/websocket", null, false);
handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
WebSocketServerHandshakerFactory
.sendUnsupportedWebSocketVersionResponse(ctx.channel());
} else {
handshaker.handshake(ctx.channel(), req);
}
}
示例4: handleHttpRequest
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //導入依賴的package包/類
private void handleHttpRequest(ChannelHandlerContext ctx, HttpRequest req)
throws Exception {
// Handle a bad request.
if (!req.getDecoderResult().isSuccess()) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
return;
}
// Allow only GET methods.
if (req.getMethod() != GET) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
return;
}
// Handshake
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
getWebSocketLocation(req), null, false, Integer.MAX_VALUE);
handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
handshaker.handshake(ctx.channel(), req);
}
}
示例5: handleHttpRequest
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //導入依賴的package包/類
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
if (!req.getDecoderResult().isSuccess() || (!"websocket".equals(req.headers().get("Upgrade")))) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));
return;
}
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory("ws://websocket.url", null, false);
handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
IPusherClient conn=(IPusherClient)ctx.channel();
String uri=req.getUri();
handshaker.handshake(ctx.channel(), req);
onOpen(conn, uri);
}
}
示例6: handleHttpRequest
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //導入依賴的package包/類
public void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
if (!req.getDecoderResult().isSuccess() || !"websocket".equals(req.headers().get("Upgrade"))) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));
return;
}
// 構造握手響應返回,本機測試
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory("ws://localhost:8080/websocket", null,
false);
handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedWebSocketVersionResponse(ctx.channel());
} else {
handshaker.handshake(ctx.channel(), req);
}
}
示例7: handleHttpRequest
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //導入依賴的package包/類
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
// Handle a bad request.
if (!req.getDecoderResult().isSuccess()) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
return;
}
// Allow only GET methods.
if (req.getMethod() != GET) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
return;
}
// Handshake
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
getWebSocketLocation(req), null, false);
handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
handshaker.handshake(ctx.channel(), req);
}
}
示例8: doHandshake
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //導入依賴的package包/類
public void doHandshake(ChannelHandlerContext ctx, FullHttpRequest message)
{
WebSocketServerHandshakerFactory handshakerFactory = new WebSocketServerHandshakerFactory("ws://" + message.headers().get(HOST) + "/" + WEBSOCKET_ROUTE, null, false);
this.handshaker = handshakerFactory.newHandshaker(message);
if (handshaker == null)
{
this.log.info("client is incompatible!");
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
return;
}
this.log.debug("handshaking now...");
this.handshaker.handshake(ctx.channel(), message).addListener((ChannelFutureListener) future -> {
if (future.isSuccess())
{
log.debug("Success!");
}
else
{
log.debug("Failed!");
}
});
}
示例9: handleHttpRequest
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //導入依賴的package包/類
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
if (HttpUtil.is100ContinueExpected(req)) {
ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
}
if (webSocketPath.equals(req.uri())) {
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
getWebSocketLocation(req), null, true, MAX_FRAME_PAYLOAD_LENGTH
);
handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
handshaker.handshake(ctx.channel(), req);
}
return;
}
requestProcessor.handleRequest(ctx, req);
}
示例10: handshake
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //導入依賴的package包/類
private void handshake(final ChannelHandlerContext ctx, final FullHttpRequest req, final String requestPath) {
WebSocketServerHandshakerFactory wsFactory =
new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, true, maxWebSocketFrameSize);
WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req);
if (handshaker != null) {
handshaker.handshake(ctx.channel(), req).addListener(
new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
final String sessionId = PipelineUtils.getSessionId(requestPath);
if (future.isSuccess()) {
ctx.channel().pipeline().addBefore(
SocketIOChannelInitializer.SOCKETIO_WEBSOCKET_HANDLER,
SocketIOChannelInitializer.WEBSOCKET_FRAME_AGGREGATOR,
new WebSocketFrameAggregator(maxWebSocketFrameSize));
connect(ctx, req, sessionId);
} else {
log.error("Can't handshake: {}", sessionId, future.cause());
}
}
});
} else {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
}
}
示例11: handleHttpRequest
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //導入依賴的package包/類
@Override
public void handleHttpRequest(final ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
if ("/api".equals(request.getUri().substring(0, 4))) {
getOrCreateTraveler(request);
// Handshake
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
getWebSocketLocation(request), null, false);
handshaker = wsFactory.newHandshaker(request);
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
handshaker.handshake(ctx.channel(), request).addListener(new GenericFutureListener<ChannelFuture>() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
nettyTraveler.registerWebsocket(ctx);
}
});
}
} else {
super.handleHttpRequest(ctx, request);
}
}
示例12: handleWebSocket
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //導入依賴的package包/類
/**
* handle WebSocket request,then, the the RPC could happen in WebSocket.
*
* @param ctx
* @param request
*/
protected void handleWebSocket(final ChannelHandlerContext ctx, FullHttpRequest request) {
if (logger.isDebugEnabled()) {
logger.debug("handleWebSocket request: uri={}", request.uri());
}
// Handshake
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(request.uri(), null, true);
WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(request);
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
return;
}
ChannelFutureListener callback = websocketHandshakeListener(ctx, request);
ChannelFuture future = handshaker.handshake(ctx.channel(), request);
if (callback != null) {
future.addListener(callback);
}
ChannelPipeline pipe = ctx.pipeline();
if (pipe.get(WebsocketFrameHandler.class) == null) {
pipe.addAfter(ctx.name(), "wsFrameHandler", new WebsocketFrameHandler(handshaker));
ChannelHandler handlerAws = pipe.get(AwsProxyProtocolDecoder.class);
if (handlerAws != null) {
pipe.remove(handlerAws);
}
pipe.remove(ctx.name());// Remove current Handler
}
}
示例13: channelRead0
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //導入依賴的package包/類
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
if (!req.decoderResult().isSuccess()) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
return;
}
// Allow only GET methods.
if (req.method() != GET) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
return;
}
/**
* 當且僅當uri為指定path的時候,進行websocket通訊的升級
* */
if (uri.equals(req.uri())
//CONNECTION 字段的值為 UPGRADE, firefox上存在多個值的情況
&& req.headers().get(HttpHeaderNames.CONNECTION).contains(HttpHeaderValues.UPGRADE)
//UPGRADE 字段的值為 WEBSOCKET
&& HttpHeaderValues.WEBSOCKET.contentEqualsIgnoreCase(req.headers().get(HttpHeaderNames.UPGRADE))
) {
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
uri, subprotocols, true, 5 * 1024 * 1024);
WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
//不支持的協議
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
//握手結束後補充如下協議
handshaker.handshake(ctx.channel(), req);
}
return;
}
//錯誤的情況
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND));
}
示例14: handleHandShake
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //導入依賴的package包/類
/**
* Handle hand shake.
*
* @param ctx the ctx
* @param req the req
*/
private void handleHandShake(ChannelHandlerContext ctx, FullHttpRequest req) {
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, true, (int) Config.getInstance().getMaxSize());
handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
handshaker.handshake(ctx.channel(), req);
dominoServer.onOpen(this.newWrapper(ctx), req);
}
}
示例15: handle
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; //導入依賴的package包/類
/**
* Handler to open the websocket for the real-time message websocket
*
* @param ChannelHandlerContext,
* FullHttpRequest
* @return void
*/
public void handle(ChannelHandlerContext ctx, HttpRequest req) throws Exception {
String uri = req.getUri();
uri = uri.substring(1);
String[] tokens = uri.split("/");
String publisherId;
if (tokens.length < 5) {
LoggingService.logWarning(MODULE_NAME, " Missing ID or ID value in URL ");
return;
} else {
publisherId = tokens[4].trim().split("\\?")[0];
}
// Handshake
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req),
null, true, Integer.MAX_VALUE);
handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
handshaker.handshake(ctx.channel(), req);
}
Hashtable<String, ChannelHandlerContext> messageSocketMap = WebSocketMap.messageWebsocketMap;
messageSocketMap.put(publisherId, ctx);
StatusReporter.setLocalApiStatus().setOpenConfigSocketsCount(WebSocketMap.messageWebsocketMap.size());
MessageBus.getInstance().enableRealTimeReceiving(publisherId);
LoggingService.logInfo(MODULE_NAME, "Handshake end....");
return;
}